一、异常日志:
The bean 'xxxx.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.Action: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”)为此报错了。
@Component@FeignClient(value = "base-log")public interface LogReqFeign {@PostMapping("/v1/base/log/req")ResObj postObj(@RequestBody ReqObj reqObj);@GetMapping("/v1/base/log/req")ResObj getObj(Map map);@GetMapping("/v1/base/log/req/{id}")ResObj getObjById(@PathVariable("id") Long id);}
@Component@FeignClient(value = "base-log")public interface LogEventFeign {@PostMapping("/v1/base/log/event")ResObj postObj(@RequestBody ReqObj reqObj);@GetMapping("/v1/base/log/event")ResObj getObj(Map map);@GetMapping("/v1/base/log/event/{id}")ResObj getObjById(@PathVariable("id") Long id);}
三、解决方案:
1、方案一:
将调用同一个服务的fegin接口都写到一个LogReqFeign接口类中。但是我这里不合适,我远程调用的方法都是同名的
2、方案二:
配置文件中添加配置:
spring:
main:
allow-bean-definition-overriding: true
通过设置spring.main.allow-bean-definition-overriding=true
来允许bean覆盖,但是这种方式并不推荐,因为可能会导致意外的覆盖,导致应用程序出现其他问题。
3、方案三:
@FeignClient增加contextId属性,改为
@FeignClient(name = "xxx", path = API.PATH,contextId="xxx1")
@FeignClient(name = "xxx", path = API.PATH,contextId="xxx2")
@Component@FeignClient(value = "base-log",contextId = "LogEvent")public interface LogEventFeign {@PostMapping("/v1/base/log/event")ResObj postObj(@RequestBody ReqObj reqObj);@GetMapping("/v1/base/log/event")ResObj getObj(Map map);@GetMapping("/v1/base/log/event/{id}")ResObj getObjById(@PathVariable("id") Long id);}
@Component@FeignClient(value = "base-log",contextId = "LogReq")public interface LogReqFeign {@PostMapping("/v1/base/log/req")ResObj postObj(@RequestBody ReqObj reqObj);@GetMapping("/v1/base/log/req")ResObj getObj(Map map);@GetMapping("/v1/base/log/req/{id}")ResObj getObjById(@PathVariable("id") Long id);}