spring 启动应用程序的 Consul 服务发现问题

Consul service discovery issue with spring boot applications

根据此博客https://spring.io/blog/2015/07/14/microservices-with-spring,它基于 eureka 服务发现并且服务发现工作正常。

但是当切换到使用 Consul 而不是 Eureka 时,服务发现无法正常工作并出现此错误:

java.lang.IllegalStateException: No instances available for ACCOUNTS-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:79)
at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:46) ...

更新:在通过提供正确的健康检查端点(参见下面的答案)修复了之前的错误后,在 [=27= 中使用正确提供的 Consul 服务器主机和端口将服务部署到 Cloud Foundry 时](基于 Consul 的 PropertySource 在 'bootstrap' 阶段加载):

---
spring:
  profiles: cloud
  cloud:
    consul:
      host: <consul host or ip>
      port: 8500

Consul 正在注册服务,但处于严重状态(失败)!

非常感谢任何帮助或指导。

谢谢

该问题与设置为 /health 端点的 Consul 健康检查默认路径有关。 因此,在所有客户端应用程序(Web 服务器和微服务)中启用 spring-actuator 后,此问题得到解决。

或者您可以更改 bootstrap.yml 文件中的默认 Consul 健康检查端点:

  cloud:
    consul:
      discovery:
        healthCheckPath: /test

注意。为了在 Maven 中启用 spring-actuator,将以下依赖项添加到 pom.xml 文件中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

有关详细信息,请参阅:http://cloud.spring.io/spring-cloud-consul/spring-cloud-consul.html

部署(推送)到 CF (CloudFoundry) 时,部署应用程序的 URI 应提供给 Consul 以进行服务发现过程(CF 在 vcap.application.uris 环境变量中提供应用程序的 URI),因此应进行以下配置添加到 bootsrap.yml 文件:

---
spring:
  profiles: cloud
  cloud:
    consul:
      host: <consul host or ip>
      port: 8500
      discovery:
        instanceId: ${spring.application.name}:${vcap.application.application_name}:${vcap.application.instance_id}
        hostname: ${vcap.application.uris[0]}
        port: 80

NB. instanceId is used by Consul to register the application (microservice) instance.