一、异常日志:
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<LogReq> reqObj);
@GetMapping("/v1/base/log/req")
ResObj getObj(Map<String, Object> 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<LogEvent> reqObj);
@GetMapping("/v1/base/log/event")
ResObj getObj(Map<String, Object> 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、方案三:文章来源:https://www.toymoban.com/news/detail-588484.html
@FeignClient增加contextId属性,改为文章来源地址https://www.toymoban.com/news/detail-588484.html
@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<LogEvent> reqObj);
@GetMapping("/v1/base/log/event")
ResObj getObj(Map<String, Object> 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<LogReq> reqObj);
@GetMapping("/v1/base/log/req")
ResObj getObj(Map<String, Object> map);
@GetMapping("/v1/base/log/req/{id}")
ResObj getObjById(@PathVariable("id") Long id);
}
到了这里,关于The bean ‘xxx.FeignClientSpecification‘ could not be registered. A bean with that name has already b的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!