一、問題現(xiàn)象
Description:
Field autoServiceRegistration in org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration required a single bean, but 2 were found:
- eurekaAutoServiceRegistration: defined by method 'eurekaAutoServiceRegistration' in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]
- nacosAutoServiceRegistration: defined by method 'nacosAutoServiceRegistration' in class path resource [com/alibaba/cloud/nacos/registry/NacosServiceRegistryAutoConfiguration.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
二、解決辦法
檢查pom文件中,是不是同時添加了Nacos和Eureka的依賴?如果是,選擇一個不必要的去掉即可。比如這次異常,我保留了Nacos,去掉了Eureka。
<!--Eureka客戶端-->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>-->
<!--nacos客戶端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
三、原因分析
Spring Boot項目啟動時,AutoServiceRegistrationAutoConfiguration類需要注入一個類型為AutoServiceRegistration的bean。但是,在容器中卻出現(xiàn)了兩個Bean,其父類均為AutoServiceRegistration,就是異常中打印出的nacosAutoServiceRegistration和eurekaAutoServiceRegistration。文章來源:http://www.zghlxwxcb.cn/news/detail-776176.html
異常中還順便給出了解決方案:即為其中一個bean添加注解@Primary或@Qualifier。其實,簡單的解決辦法就是去掉無用的依賴即可。如果其他大佬有不同看法,可以留言討論。文章來源地址http://www.zghlxwxcb.cn/news/detail-776176.html
到了這里,關(guān)于Nacos和Eureka沖突問題原因分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!