Spring-Boot

跟我学Spring Cloud(Finchley版)-10-Fe

2019-01-14  本文已影响2人  周立_itmuch

上一节( 跟我学Spring Cloud(Finchley版)-09-Feign )讲了Feign的入门姿势并深入对比了RestTemplate,本节来深入探讨Feign的高级特性。总的来说,Feign是一个相对简单的组件,但细节还是比较多的,一不小心就可能入坑,注意点我会以WARINING的形式标记出来,便于读者查阅。

Feign配置自定义【细粒度配置】

方式一、代码配置方式

Spring Cloud Netflix provides the following beans by default for feign (BeanType beanName: ClassName):

The OkHttpClient and ApacheHttpClient feign clients can be used by setting feign.okhttp.enabledorfeign.httpclient.enabled to true, respectively, and having them on the classpath.

Spring Cloud Netflix does not provide the following beans by default for feign, but still looks up beans of these types from the application context to create the feign client:

代码示例:自定义日志级别

默认Feign是不打印任何日志的,下面我们来开启Feign的日志,Feign有四种日志级别:

插科打诨 & 恶意揣测

跟我学Spring Cloud(Finchley版)-09-Feign说过,Feign的性能中等,可能官方对自己的性能也是知道的,索性全部关闭日志了,哈哈

配套代码

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-config-java

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-config-java

方法二、属性配置方式【Edgware开始提供】

从Spring Cloud Edgware开始,Feign支持使用属性自定义Feign。对于一个指定名称的Feign Client(例如该Feign Client的名称为feignName ),Feign支持如下配置项:

feign:
  client:
    config:
      feignName:
        connectTimeout: 5000  # 相当于Request.Options
        readTimeout: 5000     # 相当于Request.Options
        # 配置Feign的日志级别,相当于代码配置方式中的Logger
        loggerLevel: full
        # Feign的错误解码器,相当于代码配置方式中的ErrorDecoder
        errorDecoder: com.example.SimpleErrorDecoder
        # 配置重试,相当于代码配置方式中的Retryer
        retryer: com.example.SimpleRetryer
        # 配置拦截器,相当于代码配置方式中的RequestInterceptor
        requestInterceptors:
          - com.example.FooRequestInterceptor
          - com.example.BarRequestInterceptor
        decode404: false

TIPS

个人并不建议配置retryer,Spring Cloud Camden以及之后的版本中,Spring Cloud关闭了Feign的重试,而是使用Ribbon的重试。如果自己再定义Feign的重试后,那么可能会造成重试特性的混乱。笔者已在https://github.com/spring-cloud/spring-cloud-netflix/issues/2330 提出该问题。

代码示例:自定义日志级别

要想用属性配置方式来达到上面Java代码方式的效果,只需在application.yml 中添加如下内容即可:

feign:
  client:
    config:
      microservice-provider-user:
        loggerLevel: full
logging:
  level:
    com.itmuch.cloud.study.user.feign.UserFeignClient: debug

配套代码

GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-config-properties

Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-config-properties

Feign配置自定义【通用配置】

上面讨论了如何配置特定名称的Feign Client,那么如果想为所有的Feign Client都进行配置,该怎么办呢?我们知道,@EnableFeignClients 注解上有个defaultConfiguration 属性,我们可以将默认配置写成一个类,然后用defaultConfiguration 来引用,例如:

@EnableFeignClients(defaultConfiguration = DefaultRibbonConfig.class)

如果想使用配置属性的方式,只需使用类似如下的写法即可。

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic

配置优先级

如果你不小心又使用了Java代码配置Feign,同时又使用了配置属性配置Feign,那么使用配置属性的优先级更高。配置属性配置的方式将会覆盖Java代码配置。如果你想修改代码配置方式的优先级,可使用如下属性:feign.client.default-to-properties=false

压缩

一些场景下,我们可能需要对请求或响应进行压缩,此时可使用以下属性启用Feign的压缩功能。

feign.compression.request.enabled=true
feign.compression.response.enabled=true

对于请求的压缩,Feign还提供了更为详细的设置,例如:

feign.compression.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

其中,feign.compression.request.mime-types 用于支持的媒体类型列表,默认是text/xml、application/xml以及application/json。

feign.compression.request.min-request-size 用于设置请求的最小阈值,默认是2048。

继承

比较重要,业界对继承特性看法非常不一样,喜欢的特别喜欢,讨厌的特别讨厌(例如我)。将以番外形式体现,并对比两者优缺点,以及最佳实践,明天或后天更新,敬请期待。

其他特性

Feign其他特性我已经写了很多了,知识体系已经完备了。懒得再在这个系列里凑字数,这不是我的风格,直接贴地址吧:

本文首发

http://www.itmuch.com/spring-cloud/finchley-10/

干货分享

全是干货
上一篇下一篇

猜你喜欢

热点阅读