您的位置:首页 > 编程语言 > Java开发

SpringCloud+Consul 服务注册与服务发现

2018-09-19 11:37 2291 查看

1. 服务注册:

在spring-cloud-consul-discovery-1.3.3.RELEASE.jar中的Spring.factories有一段:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.consul.discovery.RibbonConsulAutoConfiguration,\
org.springframework.cloud.consul.discovery.configclient.ConsulConfigServerAutoConfiguration,\
org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration,\
org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration,\
org.springframework.cloud.consul.discovery.ConsulDiscoveryClientConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.consul.discovery.configclient.ConsulDiscoveryClientConfigServiceBootstrapConfiguration

 

这是SpringCloud时Consul实现服务注册的关键。
发现有一个ConsulLifecycle的bean注入:

@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public ConsulLifecycle consulLifecycle(ConsulDiscoveryProperties discoveryProperties,
HeartbeatProperties heartbeatProperties) {
ConsulLifecycle lifecycle = new ConsulLifecycle(consulClient, discoveryProperties, heartbeatProperties);
if (this.ttlScheduler != null) {
lifecycle.setTtlScheduler(this.ttlScheduler);
}
if (this.servletContext != null) {
lifecycle.setServletContext(this.servletContext);
}
if (this.serverProperties != null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) {
// no need to wait for events for this to start since the user has explicitly set the port.
lifecycle.setPort(this.serverProperties.getPort());
}
return lifecycle;
}

ConsulLifecycle继承自AbstractDiscoveryLifecycle,而AbstractDiscoveryLifecycle实现了ApplicationListener

@Override
@Retryable(interceptor = "consulRetryInterceptor")
public void start() {
super.start();
}

是支持重新注册的。

//第一个配置
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
tags: dev
instance-id: cosulservice
service-name: app
application:
name: cosulservice
server:
port: 8088
//第二个配置
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
#        health-check-path: /health
#       health-check-interval: 15s
tags: dev
instance-id: cosulservice2
service-name: app
application:
name: cosulservice
server:
port: 8088

运行两个应用,注册,查看consul中相关服务:

app
Tags

dev
Nodes
node-client-v-5-1 172.17.0.8 2 passing

Service 'app' check service:cosulservice2
passing
Serf Health Status serfHealth
passing

node-client-v-5-1 172.17.0.8 2 passing

Service 'app' check service:cosulservice
passing
Serf Health Status serfHealth

注册两个服务。服务均为app。至此已经两个服务已经注册。

2. 服务发现:

@Bean
@ConditionalOnMissingBean
public ConsulDiscoveryClient consulDiscoveryClient(ConsulLifecycle consulLifecycle,
ConsulDiscoveryProperties discoveryProperties) {
ConsulDiscoveryClient discoveryClient = new ConsulDiscoveryClient(consulClient,
consulLifecycle, discoveryProperties);
discoveryClient.setServerProperties(serverProperties); //null ok
return discoveryClient;
}
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("app");
}

简单的单元测试:

@Test
public void testServicess() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/services").contentType(MediaType.APPLICATION_JSON_UTF8)).andDo(new ResultHandler() {
@Override
public void handle(MvcResult result) throws Exception {
System.out.println(result.getResponse().getContentAsString());
}
});
}
[{"serviceId":"app","host":"192.168.1.103","port":8088,"secure":false,"metadata":{"dev":"dev"},"uri":"http://192.168.1.103:8088"},{"serviceId":"app","host":"192.168.1.103","port":8080,"secure":false,"metadata":{"dev":"dev"},"uri":"http://192.168.1.103:8080"}]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐