使用自定义IP从Docker注册到Eureka

我在Docker虚拟机中运行Spring Cloud Eureka。 我有服务注册到它,但他们从Docker虚拟机内使用他们的IP地址,但为了能够正确使用它们,我需要他们使用我可以从虚拟机外部访问的IP地址。

例如,在我的虚拟机内使用172.xxx的寄存器,我可以从我的浏览器使用192.168.xxx访问REST接口,我需要它们注册为192.168.xxx

我如何告诉我的服务注册一个特定的IP地址?

你可以在application.ymlconfiguration它:

 eureka: instance: ipAddress: 192.168.xx 

您可以在application.yml的尤里卡configuration中使用环境variables

在下面的例子中,我使用$HOST$PORT环境variables来告诉eureka客户端使用什么值。 在我的情况下,这些variables是由Mesos / Marathon设置的。 您可能会发现由Docker设置的其他有用的variables。

以下为我工作:

 eureka: client: serviceUrl: defaultZone: http://discovery.marathon.mesos:31444/eureka/ registerWithEureka: true fetchRegistry: true instance: appname: configserver health-check-url: /health prefer-ip-address: true ip-address: "${HOST}" # mesos/marathon populates this in the environment non-secure-port: "${PORT}" # mesos/marathon populates this in the environment 

两个以前的答案都是正确的,但我会使复制粘贴更容易。

你应该做的是添加一个环境variables与主机IP启动你的容器时,在你的Spring Boot application.yml文件中包含它。

application.yml

 eureka: instance: # Necessary for Docker as it doesn't have DNS entries prefer-ip-address: true # Necessary for Docker otherwise you will get 127.0.0.x ip ip-address: "${HOST}" client: serviceUrl: # Location of your eureka server defaultZone: http://192.168.0.107:8761/eureka/ 

运行Docker

 docker run -p <port>:<port> -e HOST='192.168.0.106' <image name> 

用docker-compose运行

 my_service: image: image_name environment: - HOST=192.168.0.106 ports: - your_port:container_port