@FeignClient同一个name,多个配置类的解决方案
概述
我使用的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"); } }
分类:
spring cloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了