swarm 中的 spring boot consul 服务发现

问题描述

嘿,我在集群模式下使用 consul 和 2 个 spring 云应用程序进行服务发现
由于健康检查失败,spring 应用程序在 consul 中失败.. 见下文

获取“http://10.0.0.12:8091/health”:超出上下文截止时间(等待标题时超出Client.Timeout)

容器外无法访问ip地址,不知道如何指定容器间可路由的主机名

但是一些容器随机正确解析主机名并且健康检查通过

HTTP GET http://9931386f2354:8091/health: 200 输出:Hi api-employee 起来了!!!

在某些 stackoverflow 中使用答案对我没有帮助

spring.cloud.inetutils.ignoredInterfaces=docker0,veth.* 或 spring.cloud.consul.discovery.preferIpAddress=true

请找到consul集群的docker服务命令

docker service create --name consul-seed \
-p 8300:8300 \
-p 8500:8500 \
-p 8600:8600 \
--network backend \
-e 'CONSUL_BIND_INTERFACE=eth0' \
consul agent -server -ui -bootstrap-expect=3 -disable-host-node-id -client 0.0.0.0 -retry-join=consul-cluster

docker service create --name consul-cluster \
--network backend \
--replicas 2 \
-e 'CONSUL_BIND_INTERFACE=eth0' \
consul agent -server -disable-host-node-id -client 0.0.0.0 -retry-join=consul-seed

docker service create --name consul-agents \
--network backend \
--replicas 3 \
-e 'CONSUL_BIND_INTERFACE=eth0' \
consul agent -disable-host-node-id -client 0.0.0.0 -retry-join=consul-seed

我能够通过在其中一个容器中使用 consul members 命令来验证集群中的所有成员

我有一个 spring cloud consul 应用程序属性 application.yml 如下

server:
  port: 8091
spring:
  application:
    name: api-employee
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        instance-id: ${spring.application.name}:${random.value}
        health-check-path: /health
        service-name: ${spring.application.name}
        health-check-interval: 10s

然后我使用

启动服务
docker service create --name api-employee \
-p 8081:8091 \
--replicas 3 \
--network backend -e spring.cloud.consul.host=consul-agents \
-e management.endpoints.web.exposure.include=* \
sysout1kk/api-employee

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)