我们正在尝试在Docker容器(这是Spring Boot应用程序)上部署Netflix Eureka Server。我们将其作为Docker服务与Docker群一起部署,以进行容器编排。 当我们直接创建容器并运行应用程序时,一切正常。 Eureka服务器使用正确的IP地址注册所有客户端应用程序。 但是,当我们创建它并将其作为docker服务部署时,它会使用错误的IP地址注册应用程序。
我们已经按照spring文档尝试了以下解决方案,并在配置文件中更新了属性值。
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
eureka:
instance:
preferIpAddress: true
还尝试了以下解决方案:
https://github.com/spring-cloud/spring-cloud-netflix/issues/1820
https://github.com/spring-cloud/spring-cloud-netflix/issues/2084
我们在运行docker服务时,为容器分配了一个IP地址,例如172.16.1.3,而在docker内部启动的服务被分配了新的IP地址172.16.1.4,因为此问题,客户端应用程序使用容器的IP将其自身注册到Eureka服务器。但是可以通过172.16.1.4访问。
那么为什么将docker作为服务运行会分配两个IP地址?
答案 0 :(得分:0)
我们在使用docker swarm模式的生产环境中使用以下配置:
Eureka服务器配置
# Defines the Eureka server that is used by the Netflix OSS components to use as the registry
# for server discovery
eureka:
instance:
hostname: ${HOST_NAME:localhost}
appname: eureka-cluster
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 20
Eureka客户端配置
eureka:
client:
registerWithEureka: ${REGISTER_WITH_EUREKA:true}
fetchRegistry: ${FETCH_REGISTRY:false}
serviceUrl:
defaultZone: ${EUREKA_DEFAULT_ZONE:http://localhost:8761/eureka/}
instance:
hostname: ${HOST_NAME:${spring.application.name}} # work on swarm
preferIpAddress: ${PREFER_IP_ADDRESS:false}
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: ${LEASE_RENEWAL_INTERVAl_IN_SECONDS:10}
leaseExpirationDurationInSeconds: ${LEASE_EXPIRATION_DURATIONIN_SECONDS:20}
集群服务定义
rd-service:
image: my-eureka-server-microservice-image
ports:
- "8763:8763"
networks:
- backend
client-service:
image: my-eureka-client-microservice-image
networks:
- backend
networks:
environment:
- "EUREKA_DEFAULT_ZONE=http://rd-service:8763/eureka"
- "REGISTER_WITH_EUREKA=true"
- "FETCH_REGISTRY=true"
networks:
backend:
external: true
重要提示::服务必须位于eureka服务器docker服务上使用的同一docker overlay网络中。