@feignclient名字沖突的解決方案
@feignclient名字沖突
在啟動(dòng)springcloud項(xiàng)目是遇到
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
這樣一個(gè)異常
經(jīng)過(guò)研究,解決方法如下
1、在配置文件中增加配置
spring.main.allow-bean-definition-overriding=true
2、在feignclient注解中加個(gè)字段,紅色部分
@FeignClient(value = “provider-demo3-ribbon”, path = “/dev”, contextId=“tt1”)
@FeignClient同一個(gè)name,多個(gè)配置類(lèi)
我使用的spring-cloud-starter-openfeign的版本是2.0.0,然后使用@FeignClient的時(shí)候是不能一個(gè)name多個(gè)配置類(lèi)的,后來(lái)也是從網(wǎng)絡(luò)查找了各種網(wǎng)友的方法,反正就是歪門(mén)邪道的各種都有。但是還是官網(wǎng)給的方法比較靠譜。
解決方案
1、添加配置
spring.main.allow-bean-definition-overriding=true
2、這樣允許同名的bean存在,但是不安全,不推薦。(來(lái)自網(wǎng)絡(luò),未測(cè)試)在openfeign高版本2.2.1中@FeignClient里面添加了新屬性ContextId,這樣使用這個(gè)屬性也是可以的,官網(wǎng)有這個(gè)例程。
3、官網(wǎng)提供的另外一種就是手動(dòng)創(chuàng)建Feign客戶(hù)端,如下就是,(官網(wǎng))
@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"); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章

Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)

Java基礎(chǔ)之Integer使用的注意事項(xiàng)及面試題

JAVA使用動(dòng)態(tài)代理對(duì)象進(jìn)行敏感字過(guò)濾代碼實(shí)例

JAVA對(duì)象JSON數(shù)據(jù)互相轉(zhuǎn)換的四種常見(jiàn)情況

Spring事務(wù)注解@Transactional失效的八種場(chǎng)景分析