Spring Cloud Consul如何注册外部IP?

Posted

技术标签:

【中文标题】Spring Cloud Consul如何注册外部IP?【英文标题】:How to make Spring Cloud Consul register with external IP? 【发布时间】:2017-01-20 13:07:30 【问题描述】:

如何让Spring Cloud Consul注册外部IP?

我在 docker 中使用 spring cloud consul 运行 spring boot 应用程序。 该应用程序在 docker swarm 上的 docker 网络中运行。

这意味着如果 spring consul 使用主机名或 IP 地址注册,它将获得 docker swarm 内部的主机名或 IP 地址。我的领事服务器在 docker swarm 之外。

【问题讨论】:

【参考方案1】:

您可以在外部设置spring.cloud.consul.discovery.ipAddress,并设置属性spring.cloud.consul.discovery.preferIpAddress=true

或者,您可以control which network interfaces 从中派生 IP 地址。

在 Brixton 中,您可以忽略接口:

spring:
  cloud:
    inetutils:
      ignoredInterfaces:
        - docker0
        - veth.*

在 Camden(目前为 RC1)中,您还可以将网络列入白名单:

spring:
  cloud:
    inetutils:
      preferredNetworks:
        - 192.168
        - 10.0

【讨论】:

不幸的是,主机上没有具有外部 IP 的接口。此外,无法知道 docker swarm 将在哪台机器上运行 docker 映像。我将尝试在 Spring Boot 开始之前运行代码来检测外部 IP,并设置您指定的参数。遗憾的是,领事不能只获取注册人的外部 IP,或者 Spring Boot 没有解析外部 IP 的机制。 我不明白人们什么时候说这很悲伤。 Spring Cloud 试图猜测而不仅仅是 docker,否则操作员必须告诉它。如果您有除悲伤之外的想法,欢迎 PR。 对不起,我的意思是说我会喜欢这个功能。我知道这不是一项简单的任务,并且在这样的功能中需要考虑很多事情。如果我有时间,我肯定会发送 PR。另外我想说我非常感谢 Spring Cloud Consul。 NP。如果我听起来很沮丧,我很抱歉。感谢您的反馈,如果您没有时间进行 PR,您可以记录问题 我刚刚在 bootstrap.properties 中使用了属性 spring.cloud.consul.discovery.ipAddress=42.42.42.42spring.cloud.consul.discovery.preferIpAddress=false 并且 spring consul 仍然使用 hotsname 注册:Registering service with consul: NewServiceid='config-core-8082', name='config-core', tags=[], address='nlacombe-ai', port=8082, check=Checkscript='null', interval=10s, ttl=null, http=http://nlacombe-ai:8082/health, tcp=null, timeout=null @org.springframework.cloud.consul.discovery.ConsulLifecycle【参考方案2】:

使用 -p 12345:12345 运行容器以发布端口,以便您可以通过指定端口从主机访问容器

【讨论】:

这就是我所做的。我的问题是应用程序没有注册好的 IP。它使用私有 IP 或虚拟主机名注册。这使得 consul 服务器和 docker swarm 网络之外的任何计算机都无法访问它。 发布开放端口后,您需要使用主机 ip 而不是 docker one Spring Cloud Consul 是自动检测 IP/Host 而不是我手动检测的应用程序组件。我的目标是让 spring cloud consul 注册到主机 IP。你建议我怎么做?

以上是关于Spring Cloud Consul如何注册外部IP?的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud(13)——服务注册与发现工具Consul

Spring Cloud Gateway注册到服务器中心(Consul)

服务注册发现配置中心集一体的 Spring Cloud Consul

服务注册发现配置中心集一体的 Spring Cloud Consul

SpringBoot + Spring Cloud Consul 服务注册和发现

Consul集群加入网关服务(Spring Cloud Gateway)