Spring Cloud 应用如何注册到多个注册中心

https://blog.51cto.com/u_15127675/2819286

Spring Cloud 应用中引入两个服务注册组件的依赖,启动失败:

引入 nacos 和 consul 两个注册中心

image

启动报错信息
***************************
APPLICATION FAILED TO START
***************************

Description:

Field autoServiceRegistration in org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration required a single bean, but 2 were found:
	- nacosAutoServiceRegistration: defined by method 'nacosAutoServiceRegistration' in class path resource [com/alibaba/cloud/nacos/NacosDiscoveryAutoConfiguration.class]
	- consulAutoServiceRegistration: defined by method 'consulAutoServiceRegistration' in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.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

Disconnected from the target VM, address: '127.0.0.1:65056', transport: 'socket'

Process finished with exit code 1
原因

因为 AutoServiceRegistrationAutoConfiguration 这个类需要自动注入一个类型为AutoServiceRegistration 的 bean。但是在 Spring 容器中,发现了两个父类为 AutoServiceRegistration 的 bean,分别是nacosAutoServiceRegistration 和 eurekaAutoServiceRegistration。这样就导致了自动注入时不知道应该选择使用哪个 bean,进而导致了应用启动失败。

提示的解决方案

将其中的一个 bean 标记为 @Primary

posted @ 2022-06-13 18:29  西门长海  阅读(915)  评论(0编辑  收藏  举报