我使用spring cloud consur作为注册服务,spring cloud config+git作为配置服务器。微服务应用程序使用consur发现配置服务器,然后通过propery密钥获取值,还需要向consur注册服务以供其他消费者调用。
dependencypom.xml:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.SR4</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-consul-dependencies</artifactId>
<version>1.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
bootstarp.yml:
cloud:
config:
enabled: true
label: master
name: demo
discovery:
enabled: true
serviceId: CONFIG-SERVER
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
healthCheckPath: /health
healthCheckInterval: 10s
tags: dev
在应用程序启动成功后,可以通过密钥从configserver获取值,也可以向consul成功注册服务,但关闭应用程序(control+C(,ConsulLifecycle取消注册不会执行。日志如下:
2016-08-05 13:22:59.675 INFO 86816 --- [ Thread-3] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@c00fff0: startup date [Fri Aug 05 13:22:43 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6989da5e
2016-08-05 13:22:59.678 INFO 86816 --- [ Thread-3] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0
2016-08-05 13:22:59.684 INFO 86816 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2016-08-05 13:22:59.685 INFO 86816 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans
所以我按照如下方式删除依赖项,重新启动应用程序并关闭,服务注销成功。但无法获取config属性。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2016-08-05 13:24:03.671 INFO 86820 ationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@52066604: startup date [Fri Aug 05 13:23:51 CST 2016]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@3315d2d7
2016-08-05 13:24:03.674 INFO 86820 o.s.c.support.DefaultLifecycleProcessor - Stopping beans in phase 0
2016-08-05 13:24:03.675 INFO 86820 o.s.c.consul.discovery.ConsulLifecycle - Deregistering service with consul: demo-server-ed3e50ef60d3f5b1295f4866c46386d1
2016-08-05 13:24:03.777 INFO 86820 o.s.b.a.e.jmx.EndpointMBeanExporter - Unregistering JMX-exposed beans on shutdown
2016-08-05 13:24:03.785 INFO 86820 o.s.b.a.e.jmx.EndpointMBeanExporter - Unregistering JMX-exposed beans
2016-08-05 13:24:03.786 INFO 86820 o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans on shutdown
2016-08-05 13:24:03.787 INFO 86820 o.s.j.e.a.AnnotationMBeanExporter - Unregistering JMX-exposed beans
我不知道为什么,什么都是冲突?期待任何答案,thks@spencergib
我遇到了同样的问题,并在github上打开了一个关于spring cloud consult的问题:https://github.com/spring-cloud/spring-cloud-consul/issues/213