Applications not registering to eureka when using docker-compose

◇◆丶佛笑我妖孽 提交于 2019-12-11 04:43:34

问题


I took this example https://github.com/paulc4/microservices-demo and I created 3 docker images from it, with the following Dockerfiles:

springdocker-registration:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 1111
ENTRYPOINT exec java -jar /app.jar registration

springdocker-accounts:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 2222
ENTRYPOINT exec java -jar /app.jar accounts

springdocker-web:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 3333
ENTRYPOINT exec java -jar /app.jar web

If I run the three images separately everything works ok, the web and accounts services register to the registration service (which is an implementation of the eureka registry) and I can use my application. However when using docker-compose with the following docker-compose.yml file

version: "3.4"
services:
 registration:
  image: springdocker-registration
  ports:
   - "1111:1111"

 accounts:
  image: springdocker-accounts
  ports:
   - "2222:2222"
  links:
   - registration
  depends_on:
   - registration

 web:
  image: springdocker-web
  ports:
   - "3333:3333"
  depends_on:
   - registration
   - accounts
  links:
   - registration

the services web and accounts are not able to register to the registration service. Here are the configuration files for the applications:

registration-server.yml:

eureka:
  instance:
    hostname: localhost
  client:  
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
     defaultZone: http://localhost:1111/eureka/

server:
  port: 1111   

spring:
  thymeleaf:
    enabled: false 

accounts-server.yml:

spring:
  application:
     name: accounts-service  
  freemarker:
    enabled: false           
  thymeleaf:
    cache: false            
    prefix: classpath:/accounts-server/templates/    

error:
  path: /error

server:
  port: 2222   

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka
  instance:
    leaseRenewalIntervalInSeconds: 5   
    preferIpAddress: true

web-server.yml

spring:
  application:
    name: web-service 
  freemarker:
    enabled: false     
  thymeleaf:
    cache: false       
    prefix: classpath:/web-server/templates/   

error:
  path: /error

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka
  instance:
    leaseRenewalIntervalInSeconds: 5 
    preferIpAddress: true

server:
  port: 3333  

I can post the full console log of docker-compose up but I think this is the interesting point:

1: ERROR RedirectingEurekaHttpClient - Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)

1: ERROR DiscoveryClient - DiscoveryClient_WEB-SERVICE/e3b5e6b3396c:web-service:3333 - was unable to refresh its cache! status = Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

回答1:


Since its running in docker, don't use localhost. Docker compose lets you refer to container names.

eureka:
  client:
    serviceUrl:
      defaultZone: http://registration:1111/eureka

Side note: defaultZone must be exact, I spent 2 days wondering why it wouldn't work since intellij auto completes to do default-zone which wont work.



来源:https://stackoverflow.com/questions/47266795/applications-not-registering-to-eureka-when-using-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!