购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

6.3 自定义Feign配置

很多场景下,我们需要自定义Feign的配置,例如配置日志级别、定义拦截器等。Spring Cloud Edgware允许使用Java代码或属性自定义Feign的配置,两种方式是等价的。

6.3.1 使用Java代码自定义Feign配置

本小节来讨论如何使用Java代码自定义Feign的配置。

在Spring Cloud中,Feign的默认配置类是FeignCl ientsConf iguration,该类定义了Feign默认使用的编码器、解码器、所使用的契约等。

Spring Cloud允许通过注解@FeignClient的configuration属性自定义Feign的配置,自定义配置的优先级比FeignClientsConfiguration要高。

在SpringCloud文档中可看到以下段落,描述了SpringCloud提供的默认配置。另外,有的配置尽管没有提供默认值,但是Spring也会扫描其中列出的类型(也就是说,这部分配置也能自定义)。

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

●Decoder feignDecoder:ResponseEntityDecoder(whichw rapsa SpringDecoder)

●Encoder feignEncoder:SpringEncoder

●Logger feignLogger:Slf4jLogger

●Cont ract feignContract:SpringMvcCont ract

●Feign.Builder feignBuilder:Hyst rixFeign.Builder

●Client feignClient:ifRibbon isenabled itisa LoadBalancerFeignCl ient,otherwise thedefault feign client isused.

The OkHttpClientand ApacheHttpClient feign clients can beused by setting feign.okhttp.enabled or feign.httpclient.enabled to true,respectively,and having them on the classpath.You can customize the HTTP client used by providing a bean of either ClosableHttpClient when using Apache or OkHttpClientwhe using OK HTTP.

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:

●Logger.Level

●Retryer

●ErrorDecoder

●Request.Options

●Collection<Request Interceptor>

●SetterFactory

6.3.1.1 配置指定名称的Feign Client

由此可知,在Spring Cloud中,Feign默认使用的契约是SpringMvcCont ract,因此它可以使用SpringMVC的注解。下面来自定义Feign的配置,让它使用Feign自带的注解进行工作。

1.复制项目microservice-consumer-movie-feign,将ArtifactId修改为microservice-consumer-movie-feign-customizing。

2.创建Feign的配置类。

img
img

3.Feign接口修改为如下,使用@FeignClient的configuration属性指定配置类,同时,将findById上的SpringMVC注解修改为Feign自带的注解。

img

类似地,还可自定义Feign的编码器、解码器、日志打印,甚至为Feign添加拦截器。

例如,一些接口需要进行基于Http Basic的认证后才能调用,配置类可以这样写:

img
img
img

测试

1.启动microservice-discovery-eureka。

2.启动microservice-provider-user。

3.启动microservice-consumer-movie-feign-custom izing。

4.访问 http://localhost:8010/user/1 ,可获得如下结果。

img

说明已实现Feign配置的自定义。

img

在Spring Cloud Edgware中,Feign的配置类(例如本例中的FeignConfiguration类) 无须 添加@Configuration注解;如果加了@Configuration注解,那么该类不能存放在主应用程序上下文@ComponentScan所扫描的包中。否则,该类中的配置的feign.Decoder、feign.Encoder、feign.Contract等配置就会被所有的@FeignClient共享。

为避免造成问题,笔者认为, 最佳实践 是不在指定名称的Feign配置类上添加@Configuration注解。

6.3.1.2 全局配置

注解@EnableFeignClients为我们提供了defaul tConf iguration属性,用来指定默认的配置类,例如:

img

6.3.2 使用属性自定义Feign配置

从Spring Cloud Netflix 1.4.0开始(即从Spring Cloud Edgware开始,读者可前往第2.3.2节查看Spring Cloud各版本所使用的子项目版本),Feign支持使用属性自定义。这种方式比使用Java代码配置的方式更加方便。

6.3.2.1 配置指定名称的Feign Client

对于指定名称的Feign Client(本例中Feign Client的名称为feignName),配置如下。

img
6.3.2.2 通用配置

上面讨论了如何配置指定名称的Feign Client,如果想配置所有的Feign Client,该怎么办呢?只需做如下配置即可:

img
img

属性配置的方式比Java代码配置的方式 优先级更高 。如果你想让Java代码配置方式优先级更高,可使用这个属性:feign.client.defaul t-to-proper ties=false。 D6oiITOSDJzMsisIbHJnsfXZbvKIWZj+hSlIdKEd7VnCT7+Du90V5RFz0Od8enRR

点击中间区域
呼出菜单
上一章
目录
下一章
×