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

1. Exception log:

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

2. Reason:
In the same microservice, multiple feign interfaces use the @FeignClient annotation to call the microservice with the same name, and an exception is thrown at startup. For example
: two feign interface classes are defined in the project, but the annotations are all @FeignClient (value = "userinfo-service") and an error is reported for this.

@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);
}


3. Solutions:
1. Solution 1:
Write fegin interfaces that call the same service into a LogReqFeign interface class. But I am not suitable here, the methods I call remotely are all with the same name
2. Solution 2:
Add configuration to the configuration file:

spring:
   main:
      allow-bean-definition-overriding: true

Allow bean overriding by setting spring.main.allow-bean-definition-overriding=true, but this method is not recommended, because it may cause unintentional overriding, causing other problems in the application.

3. Option Three:

@FeignClient adds contextId attribute, change to

@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);

}

Guess you like

Origin blog.csdn.net/weixin_70144763/article/details/130406091