@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");
    }
}
  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值