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"}]
相关文章推荐
- SpringCloud之consul(服务注册发现,客户端安装) |第十四章 -yellowcong
- 服务注册发现consul之一:spring cloud consul介绍及安装
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(二)服务注册与发现(Consul)
- Spring Cloud Spring Boot mybatis 企业分布式微服务云(二)服务注册与发现(Consul)
- spring cloud搭建微服务second-fiberhome(二):结合consul实现服务注册与服务发现
- 详解使用Spring Cloud Consul实现服务的注册和发现
- Spring Cloud 服务注册与发现 [ consul ]
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Boot + Spring Cloud 构建微服务系统(一):服务注册和发现(Consul)
- Spring Cloud Consul 实现服务注册和发现
- Spring Cloud构建微服务架构:Consul服务注册与发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- 服务注册发现consul之二:使用Spring Cloud Consul实现服务的注册和发现
- 使用Spring Cloud Consul实现服务的注册和发现
- springcloud consul +consul 实现服务注册及发现
- 详解Spring Cloud Consul 实现服务注册和发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- 使用Spring Cloud Consul实现服务的注册和发现
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- SpringCloud之consul (服务注册发现)客户端的安装与简单使用|第十三章-yellowcong