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

相关推荐
蓝色天空的银码星40 分钟前
Springcloud Alibaba自定义负载均衡详解
spring·spring cloud·负载均衡
田超凡13 小时前
M4Pro安装ELK(ElasticSearch+LogStash+Kibana)踩坑记录
spring cloud·微服务·java-ee
徐子童18 小时前
《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》
java·开发语言·spring cloud·nacos·gateway
movie__movie2 天前
springcloud openfeign 请求报错 java.net.UnknownHostException:
java·spring cloud·openfeign
人生导师yxc2 天前
SpringCloud基础知识
后端·spring·spring cloud
意倾城3 天前
Nacos注册中心原理
spring cloud·nacos
SoFlu软件机器人4 天前
重构开发范式!飞算JavaAI革新Spring Cloud分布式系统开发
spring·spring cloud·重构
zkmall4 天前
B2C商城架构对比:ZKmall模板商城为何选择 Spring Cloud
spring·spring cloud·架构
亚林瓜子5 天前
Springboot引入Spring Cloud for AWS的配置中心(Parameter Store和Secrets)
spring boot·spring cloud·aws·secrets manager·parameter store
枫super5 天前
Spring Cloud 详解:2025 最新技术与最佳实践
后端·spring·spring cloud·eureka·nacos·gateway·openfeign