环境
<spring-boot.version>2.5.3</spring-boot.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
报错
项目启动时报错,报错如下:
2021-12-22 16:55:56.766 WARN 21922 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'oauth-client.FeignClientSpecification' defined in null: Cannot register bean definition [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'oauth-client.FeignClientSpecification': There is already [Generic bean: class [org.springframework.cloud.openfeign.FeignClientSpecification]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
2021-12-22 16:55:56.776 INFO 21922 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-22 16:55:56.799 ERROR 21922 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'xxx-client.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
原因
一个项目中存在多个接口使用@FeignClient调用同一个服务,意思是说一个服务只能用@FeignClient使用一次。
解决
1)用 contextId来区分
但我这块用了不知道为什么没起作用
@FeignClient(value = “xxx-server”, contextId = “oauth-client”)
public interface OAuthClientFeignClient {
@GetMapping("/api/v1/oauth-clients/{clientId}")
Result<SysOauthClient> getOAuthClientById(@PathVariable String clientId);
}
2)在application.properties文件加入以下代码。
#允许存在多个Feign调用相同Service的接口
spring.main.allow-bean-definition-overriding=true
3) 在application.yml文件加入以下代码。
spring:
main:
#允许存在多个Feign调用相同Service的接口
allow-bean-definition-overriding: true
以上代码可以使多个接口使用@FeignClient调用同一个服务。
说明
在SpringBoot 2.1之前,这个配置默认就是true,而在2.1做了更改。
设置为true后,因为FeignClientSpecification的原因,FeignClient注解的configuration参数会被覆盖。
解决配置覆盖参考:
https://blog.csdn.net/neosmith/article/details/82349449
个人觉得Stackoverflow中有位大佬说的不错:
https://stackoverflow.com/questions/53103991/cant-stat-app-after-updating-springboot-from-2-0-6-release-to-2-1-0-release
别的情况下也可能会因为Bean名重复引起该错误,更佳的解决方案是尽可能避免去定义相同的Bean名。
而Feign远程调用服务因为业务需求出现在不同在接口上使用相同的服务名,将allow-bean-definition-overriding设置为true,如果在其他情况下没有重复Bean名,那么受影响的应该就只是Feign的configuration(然而configuration我都没另外做配置,覆盖也不影响- -)。
参考
OpenFeign 出现The bean xxx.FeignClientSpecification could not be registered问题解决
https://blog.csdn.net/qq_38974638/article/details/106269627
Feign报错’xx.FeignClientSpecification’, defined in null, could not be registered.