Nacos和Eureka冲突问题原因分析

目录

一、问题现象

bash 复制代码
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。

bash 复制代码
<!--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。但是,在容器中却出现了两个Bean,其父类均为AutoServiceRegistration,就是异常中打印出的nacosAutoServiceRegistration和eurekaAutoServiceRegistration。

异常中还顺便给出了解决方案:即为其中一个bean添加注解@Primary或@Qualifier。其实,简单的解决办法就是去掉无用的依赖即可。如果其他大佬有不同看法,可以留言讨论。

相关推荐
爱笑的源码基地21 小时前
智慧工地云平台源码,采用Java+SpringCloud+UniApp+MySql技术,支持多端展示,具备集团级多级权限管理。
人工智能·后端·spring·spring cloud·源码·智慧工地·工地智能管理
小坏讲微服务1 天前
SpringBoot4.0整合knife4j 在线文档完整使用
java·spring cloud·在线文档·knife4j·文档·接口文档·swagger-ui
杀死那个蝈坦2 天前
Caffeine
java·jvm·spring cloud·tomcat
w***H6502 天前
SpringCloud-持久层框架MyBatis Plus的使用与原理详解
spring·spring cloud·mybatis
Mr.朱鹏2 天前
RocketMQ可视化监控与管理
java·spring boot·spring·spring cloud·maven·intellij-idea·rocketmq
5***r9352 天前
SpringBoot 与 SpringCloud的版本对应详细版
spring boot·后端·spring cloud
8***a8152 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
心灵宝贝2 天前
sw_64架构 docker-ce-cli rpm 安装方法(附安装包)
后端·spring·spring cloud
l***91472 天前
SpringCloud 整合 Dubbo
spring·spring cloud·dubbo
n***78682 天前
SpringCloud-持久层框架MyBatis Plus的使用与原理详解
spring·spring cloud·mybatis