Consul and Spring Boot services in Docker - not deregistering

时间:2017-04-10 00:36:46

标签: java docker spring-boot microservices consul

So we have Java microservices written with Spring-Boot, using Consul for service discovery and config management and running in Docker containers. All of it is working, but when a container dies or a service restarts the old service-id never goes away in Consul and the service forever after shows as "Failing" in the Consul UI, even though the new container has registered and shows all Green.

We are not using heartbeat - but I cannot find much documentation on what the difference between heartbeat and healthcheck are for Consul.

Here's my bootstrp.yml

spring:
  application:
    name: my-service
  cloud:
    config:
      enabled: false
    consul:
      host: ${discovery.host:localhost}
      port: ${discovery.port:8500}
      config:
        watch:
          wait-time: 30
          delay: 10000 
        profile-separator: "-"
        format: FILES
      discovery:
        prefer-ip-address: true
        instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

There are other settings to enable heartbeat, but the docs say something about this putting more stress on the Consul cluster.

Has anyone managed to get Consul and Spring Boot/Docker services to actually de-register automatically? It actually doesn't cause any real problems, but it makes the Consul UI pretty useless to actually monitor for up/down services.

2 个答案:

答案 0 :(得分:1)

Consul不会自动取消注册服务。

有关同一问题的提示,请参阅https://groups.google.com/forum/#!topic/consul-tool/slV5xfWRpEE。根据该线程,您需要更新配置或执行Agent API call。由于代理是事实的来源,因此您不应尝试通过Catalog API进行更新。有关详细信息,请参阅GitHub。他们还在Google小组中提到,如果节点优雅地关闭,您不必取消注册服务,但这似乎不是您的用例。

请参阅Consul not deregistering zombie services,了解有关使用api或registrator等工具自动取消注册服务的提示。

答案 1 :(得分:1)

您已经提到过使用docker容器来运行微服务。您是否在docker容器中的入口点脚本中捕获SIGTERM?如果发送了SIGTERM,启动应用程序将获得它,您将看到下面的日志显示微服务已与Consul取消注册。

2017-04-27 09:20:19.854  INFO 6852 --- [on(6)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
2017-04-27 09:20:19.857  INFO 6852 --- [on(6)-127.0.0.1] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@afb5821: startup date [Thu Apr 27 09:20:00 EDT 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@130c12b7
2017-04-27 09:20:19.859  INFO 6852 --- [on(6)-127.0.0.1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2017-04-27 09:20:19.863  INFO 6852 --- [on(6)-127.0.0.1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2017-04-27 09:20:19.863  INFO 6852 --- [on(6)-127.0.0.1] o.s.c.c.s.ConsulServiceRegistry          : Deregistering service with consul: xxxxxxxxxxxxx

This blog post discusses this.