The bean ‘xxxx.FeignClientSpecification‘ could not be registered. A bean with that name has already

r囧r小猫 2024-04-07 13:35 226阅读 0赞

一、异常日志:

  1. The bean 'xxxx.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.
  2. Action:
  3. Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

二、原因:

在同于一个微服务中多个feign接口使用@FeignClient注解调用同一个名称的微服务,启动时引发的异常
比如:项目中定义了两个feign接口类,但是注解都是@FeignClient(value = "userinfo-service")为此报错了。

  1. @FeignClient(value = "userinfo-service")
  2. public interface DictionaryApi {
  3. /**
  4. * 字段查询接口
  5. *
  6. * @param requestDTO 请求类型
  7. * @return 字典信息
  8. */
  9. @PostMapping("/get-dictionary-list")
  10. public ResponseDTO<List<xxx>> getDictionary(@RequestBody DictionaryRequestDTO requestDTO);
  11. }
  12. @FeignClient(value = "userinfo-service" )
  13. public interface MyBaseInfoApi {
  14. /**
  15. * 根据企业id查询产品列表
  16. *
  17. * @param requestDTO 企业id
  18. * @return 产品列表
  19. */
  20. @PostMapping("/get-product-list-by-enterprise-id")
  21. public ResponseDTO<List<xxxx>> getProductListByEnterpriseId(@RequestBody EnterpriseIdFindProductDTO requestDTO);
  22. }

三、解决方案:

1、方案一:

将调用同一个服务的fegin接口都写到一个xxxApi接口类中。比如

  1. @FeignClient(value = "userinfo-service")
  2. public interface UserfinApi {
  3. /**
  4. * 字段查询接口
  5. *
  6. * @param requestDTO 请求类型
  7. * @return 字典信息
  8. */
  9. @PostMapping("/get-dictionary-list")
  10. public ResponseDTO<List<xxx>> getDictionary(@RequestBody DictionaryRequestDTO requestDTO);
  11. /**
  12. * 根据企业id查询产品列表
  13. *
  14. * @param requestDTO 企业id
  15. * @return 产品列表
  16. */
  17. @PostMapping("/get-product-list-by-enterprise-id")
  18. public ResponseDTO<List<xxxx>> getProductListByEnterpriseId(@RequestBody EnterpriseIdFindProductDTO requestDTO);
  19. }

2、方案二:

配置文件中添加配置:

  1. spring:
  2. main:
  3. allow-bean-definition-overriding: true

发表评论

表情:
评论列表 (有 0 条评论,226人围观)

还没有评论,来说两句吧...

相关阅读