spring cloud feign在2.x版本中feign client名称重复无法启动的解决方案

解决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<String, Object> 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

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容