概述
我使用的spring-cloud-starter-openfeign的版本是2.0.0,然后使用@FeignClient的时候是不能一个name多个配置类的,后来也是从网络查找了各种网友的方法,反正就是歪门邪道的各种都有。但是还是官网给的方法比较靠谱。
解决方案
- 添加配置(spring.main.allow-bean-definition-overriding=true)。这样允许同名的bean存在,但是不安全,不推荐。(来自网络,未测试)
- 在openfeign高版本2.2.1中@FeignClient里面添加了新属性ContextId,这样使用这个属性也是可以的,官网有这个例程。(https://cloud.spring.io/spring-cloud-openfeign/reference/html/#creating-feign-clients-manually)
- 官网提供的另外一种就是手动创建Feign客户端,如下就是,(官网:https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-hystrix-fallback)
@Import(FeignClientsConfiguration.class)
class FooController { private FooClient fooClient; private FooClient adminClient; @Autowired
public FooController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
this.fooClient = Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
.target(FooClient.class, "https://PROD-SVC"); this.adminClient = Feign.builder().client(client)
.encoder(encoder)
.decoder(decoder)
.contract(contract)
.requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
.target(FooClient.class, "https://PROD-SVC");
}
}