如何向Eureka和Consul注册Spring云应用程序

时间:2017-06-26 15:04:52

标签: spring spring-cloud spring-cloud-netflix spring-cloud-consul

在spring cloud v1.1中,我可以使用@EnableDiscoveryClient@EnableEurekaClient向Eureka和Consul正确注册一个spring cloud应用程序,因为我想使用Eureka / Zuul和key的断路器 - 领事的价值配置。但是当使用spring cloud v1.2.1时,对于相同的代码,我得到以下异常:

***************************
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中的一个新类,其目的是

  

服务发现,负载平衡和断路器等模式适用于所有Spring Cloud客户端可以使用的通用抽象层,与实现无关(例如通过Eureka或Consul发现)。

那么,这是不是意味着使用spring cloud v1.2,我可以将应用注册到只有领事但可以使用键值配置和断路器?

2 个答案:

答案 0 :(得分:0)

我刚刚做到了。

关键是排除默认自动配置 application.yml喜欢这样:

spring.autoconfigure.exclude: - org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration - org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration

然后你可以编写自己的实现,或者只是忽略那些bean。

请参阅:https://github.com/cloud-ready/spring-cloud-service-discovery/tree/develop/spring-cloud-multi-registration了解详情。

答案 1 :(得分:0)

也将不可能显式地使用每个包中的ServiceRegistry接口实现(例如,参见EurekaRegistry)并显式注册:

class Example:

    @Autowired
    EurekaRegistry eurekaRegistry; 

    @Autowired
    ConsulRegistry consulRegistry;

    void register(Registration registration){
        eurekaRegistry(registration);
        consulRegistry(registration);
     }