Open hi-sb opened 7 years ago
package org.springframework.cloud.netflix.feign;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.support.ResponseEntityDecoder;
import org.springframework.cloud.netflix.feign.support.SpringDecoder;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;
import com.netflix.hystrix.HystrixCommand;
import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;
/**
* @author Dave Syer
* @author Venil Noronha
*/
@Configuration
public class FeignClientsConfiguration {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Autowired(required = false)
private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();
@Autowired(required = false)
private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();
@Autowired(required = false)
private Logger logger;
@Bean
@ConditionalOnMissingBean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(this.messageConverters));
}
@Bean
@ConditionalOnMissingBean
public Encoder feignEncoder() {
return new SpringEncoder(this.messageConverters);
}
@Bean
@ConditionalOnMissingBean
public Contract feignContract(ConversionService feignConversionService) {
return new SpringMvcContract(this.parameterProcessors, feignConversionService);
}
。。。。。。
这是 feign 的做法 也可以参考这个
@hi-sb 感谢反馈,相应修改将在0.4.0-beta3版本中发布,敬请关注!
该问题也可以先通过自定义一个feign.Contract来解决。若业务系统自定义了feign.Contract,SpringCloudConfiguration中的feignContract将不再生效(有ConditionalOnMissingBean注解定义)。
@org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean(feign.Contract.class)
@org.springframework.context.annotation.Bean
public feign.Contract feignContract() {
return new SpringMvcContract();
}
我已经先这样解决了
原始的 是这样的@See org.bytesoft.bytetcc.supports.springcloud.SpringCloudConfiguration
我们查看 SpringMvcContract 的构造器 不难看出,实际上他是有一个可以传入List annotatedParameterProcessors 的构造器,也就是说,允许我们 扩展List annotatedParameterProcessors
在我们的项目中就扩展了一个一个AnnotatedParameterProcessor
feign 原生也是支持这样扩展的,但是在TCC 中没有调用这个构造器,导致扩展丢失了。建议修改为:
其中public List annotatedParameterProcessors () 方法创建的其实就是 SpringMvcContract 中的getDefaultAnnotatedArgumentsProcessors(),参考代码:
只不过他是private 的所以只能自己写一遍了。如果框架不做修改,那么我就只有去多创建一个 feign.Contract来覆盖你的了,但是我觉得这样不好,容易有歧义毕竟我只想扩展List ,既然feign 原生都支持这样扩展,那么TCC 也应该支持。