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

相关推荐
朱皮皮呀4 小时前
Spring Cloud——服务注册与服务发现原理与实现
运维·spring cloud·eureka·服务发现·php
朱皮皮呀15 小时前
微服务流量分发核心:Spring Cloud 负载均衡解析
spring cloud·微服务·负载均衡
源码宝2 天前
【智慧工地源码】智慧工地云平台系统,涵盖安全、质量、环境、人员和设备五大管理模块,实现实时监控、智能预警和数据分析。
java·大数据·spring cloud·数据分析·源码·智慧工地·云平台
2301_793086872 天前
SpringCloud 07 微服务网关
java·spring cloud·微服务
创码小奇客3 天前
架构师私藏:SpringBoot 集成 Hera,让日志查看从 “找罪证” 变 “查答案”
spring boot·spring cloud·trae
2301_793086874 天前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud
甄超锋4 天前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
甄超锋4 天前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
sniper_fandc5 天前
Spring Cloud系列—Alibaba Sentinel授权与规则管理及推送
spring cloud·sentinel
泉城老铁5 天前
Spring Boot中实现多线程6种方式,提高架构性能
spring boot·后端·spring cloud