概述
我使用的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"); } }