【问题标题】:Consul service discovery issue with spring boot applicationsSpring Boot 应用程序的 Consul 服务发现问题
【发布时间】:2016-11-28 17:42:30
【问题描述】:

根据这篇博客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) ...

更新:通过提供正确的运行状况检查端点(请参阅下面的答案)修复了先前的错误后,在将服务部署到 Cloud Foundry 时,在 bootstrap.yml 中正确提供了 Consul 服务器的主机和端口(基于 Consul PropertySource 在“引导”阶段加载):

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

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

不胜感激任何帮助或指导。

谢谢

【问题讨论】:

  • 如果服务注册成功,你检查过 Consul UI 吗?只是为了消除您是否发出注册或发现问题。
  • 是的,服务失败(标记为关键)。
  • 您是否调整了您的健康检查?
  • 是的,谢谢它与设置为 /health 端点的“Consul”健康检查默认路径有关,因此在启用 spring-actuator 后问题得到解决。
  • 但是在将服务部署到 Cloud Foundry 时,Consul 正在注册它们,但具有 /health 端点的临界状态(失败)!知道是什么原因造成的吗?

标签: spring-boot cloud-foundry spring-cloud consul


【解决方案1】:

问题与设置为 /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

注意。 Consul 使用 instanceId 来注册应用程序 (微服务)实例。

【讨论】:

  • 对于spring-boot 2,健康检查端点为/actuator/health,需要在配置文件中显式暴露,以便consul可以访问。
猜你喜欢
  • 2019-10-06
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-01-07
  • 2014-02-25
  • 2019-07-25
  • 2020-10-19
  • 2020-07-13
相关资源
最近更新 更多