@FeignClient同一個name,多個配置類的解決方案


概述

  我使用的spring-cloud-starter-openfeign的版本是2.0.0,然后使用@FeignClient的時候是不能一個name多個配置類的,后來也是從網絡查找了各種網友的方法,反正就是歪門邪道的各種都有。但是還是官網給的方法比較靠譜。

解決方案

    1. 添加配置(spring.main.allow-bean-definition-overriding=true)。這樣允許同名的bean存在,但是不安全,不推薦。(來自網絡,未測試)
    2. 在openfeign高版本2.2.1中@FeignClient里面添加了新屬性ContextId,這樣使用這個屬性也是可以的,官網有這個例程。(https://cloud.spring.io/spring-cloud-openfeign/reference/html/#creating-feign-clients-manually
      在這里插入圖片描述
    3. 官網提供的另外一種就是手動創建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");
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM