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

相关推荐
洁辉19 小时前
Spring Cloud 全栈指南:构建云原生微服务的终极武器
spring cloud·微服务·云原生
roc_lab1 天前
Spring Cloud Feign默认不支持重定向解决方案
spring cloud
重生之后端学习1 天前
day08-Elasticsearch
后端·elasticsearch·spring cloud·中间件·全文检索·jenkins
也许明天y1 天前
Spring Cloud Gateway 自定义分布式限流
redis·后端·spring cloud
你喜欢喝可乐吗?1 天前
RuoYi-Cloud ruoyi-gateway 网关模块
java·spring cloud·gateway
你喜欢喝可乐吗?1 天前
RuoYi-Cloud 验证码处理流程
java·spring cloud·微服务·vue
麦兜*1 天前
Spring Boot秒级冷启动方案:阿里云FC落地实战(含成本对比)
java·spring boot·后端·spring·spring cloud·系统架构·maven
nightunderblackcat1 天前
新手向:实现ATM模拟系统
java·开发语言·spring boot·spring cloud·tomcat·maven·intellij-idea
麦兜*1 天前
Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
java·spring boot·后端·spring·spring cloud·性能优化·springcloud
何苏三月2 天前
SpringCloud系列 - Seata 分布式事务(六)
分布式·spring·spring cloud