默认feign.client.default-to-properties配置为true,其作用是初始化对象获取属性的优先级顺序;
一、默认配置如下
feign:
client:
# 指定配置文件加载的顺序(全局上下文->默认上下文->指定FeignClient上下文),默认:true
# 否则按照 默认上下文->指定FeignClient上下文->全局上下文顺序加载
# org.springframework.cloud.openfeign.FeignClientFactoryBean.configureFeign
default-to-properties: true
# Feign Clients contextId 默认配置名
default-config: default
# 默认情况下,Feign Clients不编码斜杠'/'字符,默认:true
decode-slash: true
# 配置
config:
# feign.Request.Options.Options()
default:
#logger-level: FULL
# 请求超时时间,默认:10*1000 毫秒
connect-timeout: 10000
# 读取超时时间, 默认:60*1000 毫秒
read-timeout: 60000
# 是否自动处理重定向,默认:true -- 测试该属性无效
follow-redirects: true
上述配置中注解说明中讲到了三个配置上下文对象,全局上下文即FeignContext、默认上下文即配置为default的配置,指定FeignClients上下文即contextId指定的上下文配置;
二、org.springframework.cloud.openfeign.FeignClientFactoryBean#configureFeign方法
protected void configureFeign(FeignContext context, Feign.Builder builder) {
FeignClientProperties properties = beanFactory != null ? beanFactory.getBean(FeignClientProperties.class)
: applicationContext.getBean(FeignClientProperties.class);
FeignClientConfigurer feignClientConfigurer = getOptional(context, FeignClientConfigurer.class);
setInheritParentContext(feignClientConfigurer.inheritParentConfiguration());
if (properties != null && inheritParentContext) {
//默认配置
if (properties.isDefaultToProperties()) {
//通过全局上下文初始化builder对象
configureUsingConfiguration(context, builder);
//通过默认配置上下文初始化builder对象
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
//通过指定contextId初始化builder对象
configureUsingProperties(properties.getConfig().get(contextId), builder);
}
else {
//通过默认配置上下文初始化builder对象
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
//通过指定contextId初始化builder对象
configureUsingProperties(properties.getConfig().get(contextId), builder);
//通过全局上下文初始化builder对象
configureUsingConfiguration(context, builder);
}
}
else {
//通过全局上下文初始化builder对象
configureUsingConfiguration(context, builder);
}
}
上述方法中用到了两个方法configureUsingConfiguration、configureUsingProperties,其作用我整理了一下如下:
configureUsingConfiguration方法
level->Retryer->ErrorDecoder->Request.Options->RequestInterceptor->QueryMapEncoder->decode404->ExceptionPropagationPolicy->Capability
configureUsingProperties方法:
level->Request.Options->Retryer->ErrorDecoder->RequestInterceptor->decode404->encoder->DefaultRequestHeaders->DefaultQueryParameters->decoder->contract->exceptionPropagationPolicy->Capabilities