@FeignClient注解中屬性contextId的使用說明
一、概述
如果我們使用Feign定義了兩個接口,但是目標(biāo)服務(wù)是同一個,那么在SpringBoot啟動時就會遇到一個問題:
Description:
The bean 'xxxxxxxx.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
二、解決方案
2.1 方案1
修改yml配置:spring.main.allow-bean-definition-overriding=true
spring:
main:
allow-bean-definition-overriding: true2.2 方案2
在每個Feign的接口中,在注解上加 contextId屬性
contextId在Feign Client的作用是在注冊Feign Client Configuration的時候需要一個名稱,名稱是通過getClientName方法獲取的
@FeignClient(name = "sale-service",contextId= "saleservice1")
public interface saleClient{
@RequestMapping(value = "/sale/add", method = RequestMethod.GET)
String add(@RequestParam("saleNum") String queryStr);
}備注:contextId= "名稱" 中的名稱,不能用“_”會報錯,可以用“-”
三、源代碼分析
- 包名:spring-cloud-openfeign-core-2.2.5.RELEASE.jar
- 類路徑:org.springframework.cloud.openfeign.FeignClientsRegistrar
相關(guān)代碼1
private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) {
String className = annotationMetadata.getClassName();
BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class);
this.validate(attributes);
definition.addPropertyValue("url", this.getUrl(attributes));
definition.addPropertyValue("path", this.getPath(attributes));
String name = this.getName(attributes);
definition.addPropertyValue("name", name);
String contextId = this.getContextId(attributes);
definition.addPropertyValue("contextId", contextId);
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", attributes.get("decode404"));
definition.addPropertyValue("fallback", attributes.get("fallback"));
definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory"));
definition.setAutowireMode(2);
String alias = contextId + "FeignClient";
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
beanDefinition.setAttribute("factoryBeanObjectType", className);
boolean primary = (Boolean)attributes.get("primary");
beanDefinition.setPrimary(primary);
String qualifier = this.getQualifier(attributes);
if (StringUtils.hasText(qualifier)) {
alias = qualifier;
}
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias});
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
}代碼截圖:

相關(guān)代碼2
可以看到, name應(yīng)該是從注解中的屬性取值來的, 再看看getClientName()方法.
private String getClientName(Map<String, Object> client) {
if (client == null) {
return null;
} else {
String value = (String)client.get("contextId");
if (!StringUtils.hasText(value)) {
value = (String)client.get("value");
}
if (!StringUtils.hasText(value)) {
value = (String)client.get("name");
}
if (!StringUtils.hasText(value)) {
value = (String)client.get("serviceId");
}
if (StringUtils.hasText(value)) {
return value;
} else {
throw new IllegalStateException("Either 'name' or 'value' must be provided in @" + FeignClient.class.getSimpleName());
}
}
}代碼截圖:

一目了然了, 我們聲明@FeignClient注解時, 只使用了value屬性, 所以產(chǎn)生了沖突, 只要加上contextId就好了.
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼
這篇文章主要介紹了Java編程實現(xiàn)調(diào)用com操作Word方法實例代碼,代碼注釋很詳細(xì),在這里分給大家,需要的朋友可以參考下。2017-09-09
java實現(xiàn)定制數(shù)據(jù)透視表的示例詳解
數(shù)據(jù)透視表(Pivot?Table)是一種數(shù)據(jù)分析工具,通常用于對大量數(shù)據(jù)進(jìn)行匯總、分析和展示,本文主要介紹了如何使用Java將計算項添加到數(shù)據(jù)透視表中,感興趣的可以了解下2023-12-12
java中Hashtable和HashMap的區(qū)別分析
java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下2013-04-04
java8中parallelStream性能測試及結(jié)果分析
本篇文章給大家用代碼實例做了segmentfaultjava8中parallelStream性能測試,并對測試結(jié)果做了說明,需要的朋友學(xué)習(xí)下吧。2018-01-01
Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計算機應(yīng)用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下2018-08-08

