解决spring cloud feign在2.x版本中feign client名称重复的问题
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'trip-operation-core.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
Disconnected from the target VM, address: '127.0.0.1:56071', transport: 'socket'
Process finished with exit code 1
解决办法:
1.设置beanName可以重复。不推荐,springboot2.x也不推荐这种做法,会覆盖已存在的bean
spring.main.allow-bean-definition-overriding=true
2.设置@FeignClient的contextId属性,推荐使用。
示例:
@FeignClient(value = "trip-operation-core", contextId = "trip-operation-core-TenantClient")
@FeignClient(value = "trip-operation-core", contextId = "trip-operation-core-tenant-temp-client")
注意contextId要尽量独一无二,不要和其他服务的client的contextId重复,推荐服务名加类名,例如:服务是trip-operation-core, 接口类名是TenantClient,则全名取
trip-operation-core-tenant-client或者trip-operation-core-TenantClient
feign源码:
org.springframework.cloud.openfeign.FeignClientsRegistrar#getClientName
private String getClientName(Map client) {
if (client == null) {
return null;
}
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;
}
throw new IllegalStateException("Either 'name' or 'value' must be provided in @"
+ FeignClient.class.getSimpleName());
}
参考教程: https://juejin.im/post/5e13e8116fb9a0481e2796a3
官方教程:
https://cloud.spring.io/spring-cloud-openfeign/2.1.x/single/spring-cloud-openfeign.html#spring-cloud-feign-overriding-defaults