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

Posted wangmj518

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于FeignClient的使用大全——使用篇相关的知识,希望对你有一定的参考价值。

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

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

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>9.7.0</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-slf4j</artifactId>
    <version>9.7.0</version>
</dependency>

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<String, Object> params);

4,直接使用FeignClient

@Autowired
MyFeignClient myFeignClient;

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

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

添加bean注解配置

@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) 
    return new SpringFormEncoder(new SpringEncoder(messageConverters));

定义文件上传接口

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

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

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>9.7.0</version>
</dependency>

添加属性配置

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熔断降级,则可作如下配置
添加依赖

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-hystrix</artifactId>
    <version>9.7.0</version>
</dependency>

添加属性配置

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<ImageVO> 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<MyFeignClient> 
    @Override
    public MyFeignClient create(final Throwable cause) 
        return new MyFeignClient() 
            @Override
            public ReturnResult<ImageVO> 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-



作者:一曲畔上
链接:https://www.jianshu.com/p/0834508b7a6d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

以上是关于关于FeignClient的使用大全——使用篇的主要内容,如果未能解决你的问题,请参考以下文章

关于feign client触发熔断的异常

那天晚上和@FeignClient注解的深度交流

微服务|openfeign@FeignClient详解

随手记录关于FeignClient发https请求

java架构之路-(面试篇)Mysql面试大全

java架构之路-(面试篇)Mysql面试大全