Analysis of the causes of conflicts between Nacos and Eureka

1. Problem phenomenon

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

2. Solution

Check the pom file to see if the dependencies of Nacos and Eureka have been added at the same time? If so, just select an unnecessary one and remove it. For example, in this exception, I kept Nacos and removed Eureka.

<!--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>

3. Cause analysis

When the Spring Boot project starts, the AutoServiceRegistrationAutoConfiguration class needs to inject a bean of type AutoServiceRegistration. However, two Beans appeared in the container, and their parent classes are both AutoServiceRegistration, which are nacosAutoServiceRegistration and eurekaAutoServiceRegistration printed in the exception.

The exception also gives a solution by the way: add annotation @Primary or @Qualifier to one of the beans. In fact, the simple solution is to remove useless dependencies. If other bosses have different opinions, you can leave a message to discuss.

Guess you like

Origin blog.csdn.net/shinyolive/article/details/134833995