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

相关推荐
乐观的Terry27 分钟前
9、发布系统-Webhook自动发布
java·spring boot·spring·spring cloud·mybatis
乐观的Terry1 天前
8、发布系统-完整流水线的核心
java·spring boot·spring·spring cloud
野蛮人6号4 天前
黑马天机学堂系列问题之域名无法正常使用
java·spring cloud·天机学堂
爱学习的小可爱卢4 天前
SpringCloud——微服务实战:OpenFeign与Nacos服务调用详解
spring·spring cloud·微服务
野蛮人6号5 天前
黑马天机学堂Day01-08.搭建项目环境-本地开发部署方式——不知道nacos的密码是什么
java·spring cloud·黑马程序员·天机学堂·黑马天机学堂
dyonggan5 天前
IDEA从零搭建SpringCloud Alibaba完整工程
java·spring cloud·intellij-idea
tryxr6 天前
Spring Cloud Gateway
java·开发语言·数据库·网关·spring cloud·gateway
码农学院6 天前
建筑机械行业AI搜索优化技术方案:基于Spring Cloud微服务架构的文件管理与智能化内容推荐系统
人工智能·spring cloud·架构
Devin~Y6 天前
互联网大厂Java面试实战:Spring Boot、MyBatis、Redis、Kafka、JWT、Spring Cloud 与 AI 场景追问
java·spring boot·redis·spring cloud·kafka·mybatis·spring security
_waylau6 天前
Spring Framework HTTP服务客户端详解
java·后端·网络协议·spring·http·spring cloud