如何在 Consul 中正确注册 Spring Boot app(部署到 Cloud Foundry)

How to correctly register Spring Boot app (deployed to Cloud Foundry) in Consul

我遵循了此文档:http://cloud.spring.io/spring-cloud-consul/1.0.x/index.html 并按如下方式配置了我的 Spring 启动应用程序:

spring:
  cloud:
    consul:
      enabled: true
      host: consul.domain.tld
      port:  443
      discovery:
        instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
        fail-fast: false

当部署到 Cloud Foundry 时,应用程序可以连接到 Consul 并尝试注册,但 Consul 的 "service check" 立即失败。

读取配置后,我在 Spring 启动应用程序中得到以下实例:

list = discoveryClient.getInstances("myApp");
list.get(0) => "http://05c81764-18a4-4905-41a8-b5b8:8080"

我看到UUID很可能是Cloud Foundry生成的instanceId。当然,URL 不起作用,无论是在 Cloud Foundry 内部还是外部 Consul 服务器尝试定期检查服务的健康状况。

我必须如何正确配置我的 Spring 引导应用程序,以便在 Cloud Foundry 中使用 Consul 进行服务注册?

您需要将 spring.cloud.consul.discovery.hostname 设置为 vcap 环境变量返回的值。

类似于:

spring.cloud.consul.discovery.hostname=${vcap.application.uris[0]:localhost}
spring.cloud.consul.discovery.port=80