关于FeignClient的使用大全——使用篇

完整代码参照:
ocean-sea(https://github.com/biticcf/ocean-sea-platform.git)

一个最简单的使用FeignClient的例子如下:
1,添加maven依赖


    org.springframework.cloud
    spring-cloud-starter-openfeign
    2.0.2.RELEASE


    io.github.openfeign
    feign-core
    9.7.0


    io.github.openfeign
    feign-slf4j
    9.7.0

2,在main主入口类添加FeignClients启用注解

@EnableFeignClients
......

3,编写FeignClient代码

@FeignClient(name = "myFeignClient", url = "http://127.0.0.1:8001")
public interface MyFeignClient {
    @RequestMapping(method = RequestMethod.GET, value = "/participate")
    String getCategorys(@RequestParam Map params);
}

4,直接使用FeignClient

@Autowired
MyFeignClient myFeignClient;

到此,FeignClient便可正常使用一般的Http接口了~
5,如果想使用文件上传接口或者post的x-www-form-urlencoded接口,那需要做如下配置
添加依赖包


    io.github.openfeign.form
    feign-form
    3.4.1


    io.github.openfeign.form
    feign-form-spring
    3.4.1


    commons-fileupload
    commons-fileupload
    1.3.3

添加bean注解配置

@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder(ObjectFactory messageConverters) {
    return new SpringFormEncoder(new SpringEncoder(messageConverters));
}

定义文件上传接口

@RequestMapping(value = {"/demo/v1/upload"}, 
    method = {RequestMethod.POST}, 
    consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
ReturnResult uploadFile(
    @RequestPart(value = "file") MultipartFile file, 
    @RequestParam(value = "bucketName", required = false) String bucketName);

6,如果想使用Apache的httpclient的连接池,可以做如下配置
添加依赖


    org.apache.httpcomponents
    httpclient
    4.5.6


    io.github.openfeign
    feign-httpclient
    9.7.0

添加属性配置

feign:
  okhttp: 
    enabled: false
  httpclient:
    enabled: true
    maxConnections: 20480
    maxConnectionsPerRoute: 512
    timeToLive: 60
    connectionTimeout: 10000
    userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0'

引入FeignAutoConfiguration配置

@Import(FeignAutoConfiguration.class)
@Configuration
public class FeignConfig {
    ...
}

经过这几步操作后,便可启用Apache的httpclient替换其内嵌httpclient。
7,如果想启用hystrix熔断降级,则可作如下配置
添加依赖


    io.github.openfeign
    feign-hystrix
    9.7.0

添加属性配置

feign:
  hystrix: 
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 15000
  threadpool:
    default:
      coreSize: 40
      maximumSize: 100
      maxQueueSize: 100

添加降级策略

public class MyFeignClientFallback implements MyFeignClient {
    @Override
    public ReturnResult uploadFile(MultipartFile file, String bucketName) {
        return new ReturnResult<>(5001);
    }
}

添加bean配置

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
    return HystrixFeign.builder();
}

@Bean
public MyFeignClientFallback fb() {
    return new MyFeignClientFallback();
}

更新@FeignClient代码

@FeignClient(
    name = "myFeignClient", 
    url = "http://127.0.0.1:8001",
    fallback = MyFeignClientFallback.class,
    configuration = {FeignConfig.class})

8,如果想处理熔断的具体原因,可以做如下更新
更新熔断策略代码实现FallbackFactory接口

public class MyFeignClientFallback implements FallbackFactory {
    @Override
    public MyFeignClient create(final Throwable cause) {
        return new MyFeignClient() {
            @Override
            public ReturnResult uploadFile(MultipartFile file, String bucketName) {
                // 处理cause
                
                return new ReturnResult<>(5001);
            }
        };
    }
}

更新bean配置

@Bean
public MyFeignClientFallback fbf() {
    return new MyFeignClientFallback();
}

更新@FeignClient代码

@FeignClient(
    name = "myFeignClient", 
    url = "http://127.0.0.1:8001",
    fallbackFactory = MyFeignClientFallback.class,
    configuration = {FeignConfig.class})

-End-

你可能感兴趣的:(关于FeignClient的使用大全——使用篇)