Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applications not registering to eureka when using docker-compose

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
like image 628
gazzo Avatar asked Dec 07 '22 16:12

gazzo


2 Answers

For those who are working with docker-compose and if in your services yml file you have the following:

eureka:
  client:
    serviceUrl:
      # Will get overridden in docker-compose
      defaultZone: http://localhost:1111/eureka

You can override this in your docker-compose file like below while running docker-compose up

version: "3.3"
services:

 # Other services registered above

 web:
  image: springdocker-web
  environment: 
   # Important for clients to register with eureka
   - eureka.client.serviceUrl.defaultZone=http://registration:8761/eureka/
  ports:
   - "3333:3333"
  depends_on:
   - registration
   - accounts
  links:
   - registration

Tested on Docker version 19.03.8

like image 188
Sandeep K Nair Avatar answered May 13 '23 14:05

Sandeep K Nair


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.

like image 33
reversebind Avatar answered May 13 '23 15:05

reversebind