docker化的微服务无法注册到尤里卡服务器

我使用春季启动,春季云netfix和docker运行微服务。

在没有docker的环境中,一切都很好,但是一旦我将尤里卡服务器和微服务(例如用户服务)docker化后,我发现用户服务无法注册到尤里卡服务器。

我可以通过http:// {Ubuntu服务器}:8761 / eureka /或者通过http:// {Ubuntu服务器}:8088 / user-service访问dockerized eureka服务器。

但在docker-compose日志中,我发现错误请参阅附件,我不知道为什么它保持说未知的服务器。

而在尤里卡服务器网站中,没有显示应用程序实例。 这个错误信息已经让我困惑了好几天,而且我已经调查过我能想到的所有可能性。 请告诉我任何线索。 谢谢。

背景:

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 

尤里卡服务器configuration:

 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 

微服务configuration:

 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 profiles定义。

我从一开始就忽略了这个定义,我的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中定义的dockerconfiguration文件。

一旦容器被初始化,它将使用默认configuration文件,而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/得到了这个想法