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。其实,简单的解决办法就是去掉无用的依赖即可。如果其他大佬有不同看法,可以留言讨论。

相关推荐
Demon1_Coder1 天前
跨域问题CORS
spring cloud
worilb1 天前
Spring Cloud 学习与实践(6):Nacos 配置中心
数据库·学习·spring cloud
v***59831 天前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
JAVA社区2 天前
Java高级全套教程(十四)—— SpringData超详细实战详解
java·开发语言·spring cloud·面试·职场和发展
javahongxi2 天前
Spring Cloud Trace 链路实现
java·spring boot·spring cloud
小旭95272 天前
Spring Cloud 集成分布式日志 ELK+Swagger 接口文档实战
java·分布式·后端·elk·spring cloud
霸道流氓气质2 天前
Spring Cloud Nacos 服务注册 IP 选择机制与配置详解
tcp/ip·spring cloud·php
接着奏乐接着舞2 天前
springcloud xxl-job
后端·spring·spring cloud
jasnet_u2 天前
SpringCloud中Feign透传traceId及日志切面配置
java·spring cloud·feign·日志系统
JAVA面经实录9172 天前
SpringCloud 完整体系学习文档
java·spring cloud