Feign源码解析之自定义配置和注意点

Feign源码解析之注入IOC容器
Feign源码解析之生成jdk动态代理
Feign源码解析之代理类的处理逻辑
Feign源码解析之使用Hystrix
前面几篇文章都是针对的默认配置进行分析,这一篇文章我们将着重分析Feign的自定义配置,常见的主要有以下几种方式。
一. 对feign属性的覆盖
主要可以覆盖的属性及其默认值如下:

//feign拦截器
private final List requestInterceptors =
    new ArrayList(); 
//日志级别
private Logger.Level logLevel = Logger.Level.NONE;
//对方法和接口上的注解进行解析
private Contract contract = new Contract.Default();
//Http客户端接口,默认为Client.Default,也可以是OkHttp,ApacheHttpClient
private Client client = new Client.Default(null, null);
//失败重试
private Retryer retryer = new Retryer.Default();
//日志
private Logger logger = new NoOpLogger();
//编码,由feign方法数据到请求数据
private Encoder encoder = new Encoder.Default();
//解码,由请求响应到feign方法返回值
private Decoder decoder = new Decoder.Default();
//错误时的解码,请求返回错误时的处理
private ErrorDecoder errorDecoder = new ErrorDecoder.Default();
//请求相关的设置,如connectTimeoutMillis连接超时和readTimeoutMillis读超时
private Options options = new Options();
//请求返回404时是否进行编码
private boolean decode404;
protected Feign.Builder feign(FeignContext context) {
	FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory.class);
	Logger logger = loggerFactory.create(this.type);

	// @formatter:off
	Feign.Builder builder = get(context, Feign.Builder.class)
			// required values
			.logger(logger)
			.encoder(get(context, Encoder.class))
			.decoder(get(context, Decoder.class))
			.contract(get(context, Contract.class));
	// @formatter:on

	configureFeign(context, builder);

	return builder;
}

protected void configureFeign(FeignContext context, Feign.Builder builder) {
	FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
	if (properties != null) {
		if (properties.isDefaultToProperties()) {
			configureUsingConfiguration(context, builder);
			configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
			configureUsingProperties(properties.getConfig().get(this.name), builder);
		} else {
			configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
			configureUsingProperties(properties.getConfig().get(this.name), builder);
			configureUsingConfiguration(context, builder);
		}
	} else {
		configureUsingConfiguration(context, builder);
	}
}

如上面的代码,具体内容我们已经在Feign源码解析之生成jdk动态代理进行分析过。
有以下几种方式可以对feign的属性进行覆盖。

  1. EnableFeignClients#defaultConfiguration属性和FeignClient#configuration属性
    EnableFeignClients#defaultConfigu

你可能感兴趣的:(feign,自定义配置,注意点,源码)