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

相关推荐
rebibabo1 天前
Java进阶(1) | 微服务入门:从单体到 Spring Cloud 全家桶
spring cloud·微服务·nacos·openfeign·服务注册与发现·java进阶·单体架构
乐观的Terry2 天前
3、数据库设计与领域实体
java·数据库·spring boot·spring cloud·ai编程
吃饱了得干活2 天前
Spring Cloud Gateway限流:从Redis到Sentinel的三种方案
后端·spring cloud
Java小白笔记4 天前
Docker 命令大全:从入门到精通的完整指令集
spring cloud·docker
光学补偿4 天前
微服务学习 --- Spring Cloud概述
学习·spring cloud·微服务
不能只会打代码4 天前
校享云 — 高校智慧后勤平台微服务架构设计与实践
spring cloud·docker·微服务·nacos·vue·校享云
LiaoWL1234 天前
【SpringCloud合集-06】Spring Cloud Gateway网关学习笔记
笔记·学习·spring cloud
乐观的Terry5 天前
1、为什么要自己造发布系统
java·spring boot·后端·spring cloud·架构
HZZD_HZZD5 天前
从单体到微服务:能源计费系统的领域拆分、数据一致性与Spring Cloud实战落地
spring cloud·微服务·能源
LiaoWL1236 天前
【SpringCloud合集-04】Sentinel 流量控制与熔断降级 学习笔记
学习·spring cloud·sentinel