我正在使用Spring Boot,Spring Cloud Netfix和Docker运行微服务。
在非docker化环境中,一切都很好,但是一旦我对eureka服务器和微服务(例如,用户服务)进行了docker处理,我发现该用户服务无法注册到eureka服务器。
我可以通过http:// {Ubuntu服务器}:8761 / eureka /访问dockerized Eureka 服务器,也可以通过http:// {Ubuntu服务器}:8088 / user-service访问dockerized服务。
但是在docker-compose日志中,我发现了错误see the attachment
我不确定为什么它一直说那个未知的服务器。
在eureka服务器网站上,没有显示应用程序实例。此错误消息已经使我困惑了几天,而且我已经在调查我能想到的所有可能性。请给我任何线索。谢谢。
背景:
Virtualbox: 5.1.20
Ubuntu VM: ubuntu-16
docker installed: 17.06.0
Registry: localhost:5000
Network: Bridge mode in Virtualbox for Ubuntu VM, so that the VM has a standalone ip that could be accessed by other computer
Eureka服务器配置:
server:
port: ${vcap.application.port:8761} #Http port
eureka:
instance:
hostname: eureka
#prefer-ip-address:true
client:
registerWithEureka: false
fetchRegistry: false
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
#server:
waitTimeInMsWhenSyncEmpty: 0
微服务配置:
spring:
application:
## Define the service name for registering on Eureka
name: user-service
profiles: docker
eureka:
client:
serviceUrl:
defaultZone: http://eureka:8761/eureka/
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true
metadataMap:
instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
endpoints:
restart:
enabled: true
shutdown:
enabled: true
health:
sensitive: true
Docker撰写:
discovery-eureka:
image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
ports:
- "8761:8761"
hostname: eureka
user-service:
image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
ports:
- "8088:8088"
links:
- discovery-eureka
最佳答案
问题解决了,神奇的是用户服务的application.yml中的spring profile定义。
我从一开始就忽略了这个定义,之前是我的docker-compose.yml定义:
discovery-eureka:
image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
ports:
- "8761:8761"
hostname: eureka
user-service:
image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
ports:
- "8088:8088"
links:
- discovery-eureka
在此文件中,没有地方告诉容器(或JVM)使用用户服务的application.yml中定义的docker配置文件。
初始化容器后,它将使用默认配置文件,并且application.yml中的元数据将不流行。
之后,我将docker-compose.yml更改为以下内容,一切正常。
discovery-eureka:
image: localhost:5000/nicolas/eureka:0.0.1-SNAPSHOT
ports:
- "8761:8761"
hostname: eureka
user-service:
image: localhost:5000/nicolas/user:0.0.1-SNAPSHOT
ports:
- "8088:8088"
environment:
- "SPRING_PROFILES_ACTIVE=docker"
links:
- discovery-eureka
我是从http://www.littlebigextra.com/use-spring-profiles-docker-containers/得到这个想法的
关于docker - Dockerized微服务无法注册到Eureka服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46399153/