如何同时向 Eureka 和 Consul 注册 Spring Cloud 应用程序 [英] How to register a spring cloud app to both Eureka and Consul
问题描述
在 spring cloud v1.1 中,我可以使用 @EnableDiscoveryClient
和 @EnableEurekaClient
将 spring cloud 应用正确注册到 Eureka 和 Consul,因为我想使用Eureka/Zuul 的断路器和 consul 的键值配置.但是在使用spring cloud v1.2.1时,对于同样的代码,出现如下异常:
In spring cloud v1.1, I can use @EnableDiscoveryClient
and @EnableEurekaClient
to register a spring cloud app to both Eureka and Consul properly, because I want to use circuit breakers of Eureka/Zuul and key-value configurations of consul. But when using spring cloud v1.2.1, for the same code, I got the following exception:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method serviceRegistryEndpoint in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:
- eurekaServiceRegistry: defined by method 'eurekaServiceRegistry' in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]
- consulServiceRegistry: defined by method 'consulServiceRegistry' in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulServiceRegistryAutoConfiguration.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
我发现org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration
这个类是spring cloud v1.2.0的一个新类,它的目的是
I found the class of org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration
is a new class from spring cloud v1.2.0, and its purpose is
服务发现、负载平衡和断路器等模式适用于一个公共抽象层,所有 Spring Cloud 客户端都可以使用该抽象层,独立于实现(例如,通过 Eureka 或 Consul 进行发现).
Patterns such as service discovery, load balancing and circuit breakers lend themselves to a common abstraction layer that can be consumed by all Spring Cloud clients, independent of the implementation (e.g. discovery via Eureka or Consul).
那么,这是否意味着使用 spring cloud v1.2,我可以只向 consul 注册应用程序,但可以使用键值配置和断路器?
So, does it mean using spring cloud v1.2, I could register the app to only consul but could use key-value configurations and circuit breakers?
推荐答案
是否也不能显式使用每个包中的 ServiceRegistry
接口实现(例如,请参阅 EurekaRegistry) 并显式注册:
Would it also not be possible to explicitly use the ServiceRegistry
interface implementations from each package (eg. see EurekaRegistry) and explicitly register:
class Example:
@Autowired
EurekaRegistry eurekaRegistry;
@Autowired
ConsulRegistry consulRegistry;
void register(Registration registration){
eurekaRegistry(registration);
consulRegistry(registration);
}
这篇关于如何同时向 Eureka 和 Consul 注册 Spring Cloud 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!