EurekaServer com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server on Docker

Viewed 1638

Eureka server not working on Docker compose

Here is the docker-compose for the Eureka server and config server

version: '3'
services:
    fetebird-eurekaservice:
        container_name: FeteBird-EurekaService
        build:
            context: ../../Eureka-Service-Registry/
            dockerfile: Dockerfile
        image: fetebird/eurekaservice
        ports:
            - "8761:8761"
        networks:
            - spring-cloud-network
        volumes:
            - ./fetebird-eurekaservice/data:/data
        logging:
            driver: json-file
    
    fetebird-configserver:
        container_name: FeteBird-ConfigServer
        build:
            context: ../../FeteBird-ConfigServer
            dockerfile: Dockerfile
        image: fetebird/configserver
        ports:
            - "8085:8085"
        networks:
            - spring-cloud-network
        volumes:
            - ./fetebird-configserver/data:/data
        logging:
            driver: json-file        

networks:
    spring-cloud-network:
        driver: bridge

I tried with the expose command as well but no luck

Eureka server docker file

FROM openjdk:14

WORKDIR /fetebird-eurekaservice/service

ADD build/libs/fete-bird-eureka-service-registry-0.0.1-SNAPSHOT.jar fete-bird-eureka-service-registry-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java", "-jar", "fete-bird-eureka-service-registry-0.0.1-SNAPSHOT.jar"]

Config server Client Docker file

FROM openjdk:14

WORKDIR /fetebird-eurekaservice/service

ADD build/libs/fete-bird-configuration-server-0.0.1-SNAPSHOT.jar fete-bird-configuration-server-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java", "-jar", "fete-bird-configuration-server-0.0.1-SNAPSHOT.jar"]

Config Server

@SpringBootApplication
@EnableEurekaServer
public class FeteBirdEurekaServiceRegistryApplication {
    public static void main(String[] args) {
        SpringApplication.run(FeteBirdEurekaServiceRegistryApplication.class, args);
    }
}

Configuration of Eureka server

application.yml

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
spring:
  profiles:
    active: dev

bootstrap.yml

spring:
  application:
    name: CONFIG-SERVER
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

Config Server

server: port: 8085

Discovery Server Access

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost

Errors

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
FeteBird-ConfigServer     |     at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:857) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) ~[eureka-client-1.9.21.jar!/:1.9.21]
FeteBird-ConfigServer     |     at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
FeteBird-ConfigServer     |     at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
FeteBird-ConfigServer     |     at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
FeteBird-ConfigServer     |     at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
FeteBird-ConfigServer     |     at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
FeteBird-ConfigServer     |     at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
FeteBird-ConfigServer     | 
FeteBird-ConfigServer     | 2020-07-20 14:30:19.268  WARN 1 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator
2 Answers

The problem is this

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

It is pointing to localhost and Eureka is no longer running on localhost, localhost in this case is the individual containers. The containers are linked together so you can just change this to

eureka:
  client:
    serviceUrl:
      defaultZone: http://fetebird-eurekaservice:8761/eureka/
    instance:
      hostname: fetebird-eurekaservice

Each docker file

ENTRYPOINT ["java", "-jar", "fete-bird-configuration-server-0.0.1-SNAPSHOT.jar"] fetebird-eurekaservice

Docker compose file (Add links and depends_on)

fetebird-configserver:
        container_name: FeteBird-ConfigServer
        build:
            context: ../../FeteBird-ConfigServer
            dockerfile: Dockerfile
        image: fetebird/configserver
        ports:
            - "8085:8085"
        links:
            - fetebird-eurekaservice
        depends_on:
            - fetebird-eurekaservice
        networks:
            - spring-cloud-network
        volumes:
            - ./fetebird-configserver/data:/data
        logging:
            driver: json-file  

Reference - https://github.com/spring-cloud/spring-cloud-netflix/issues/2442

As pointed out by OP's answer: Eureka is no longer running on localhost, localhost in this case is the individual containers.

I'm here to introduce two addtional solutions:

Solution 1. Using docker environment

Dev environment (in application.properties):

eureka.client.service-url.defaultZone = http://localhost:8761/eureka 

Docker environment (override defaultZone settings in docker-compose.yml file):

myservice:
    container_name: myservice
    environment:
      eureka.client.service-url.defaultZone: http://<your-eureka-service-name>:8761/eureka

This will override application.properties or application.yml values.

If you'd like to override a bunch stuff in docker environment like jdbc connection, you can use SPRING_APPLICATION_JSON (spring will read it as it is embedded in an environment variable or system property):

myservice:
    container_name: myservice
    environment:
      SPRING_APPLICATION_JSON: '{
        "spring.datasource.url=jdbc:mysql": "//jdbc:mysql:3306/mydb",
        "spring.datasource.username": "username",
        "spring.datasource.password": "password",
        "spring.jpa.hibernate.ddl-auto": "update",
        "eureka.client.service-url.defaultZone": "http://<your-eureka-service-name>:8761/eureka"
        }'

Solution 2. Setting up different profiles:

Step one, prepare two versions of application.properties, if you prefer application.yml, your choise:

application-default.properties for dev environment:

eureka.client.service-url.defaultZone = http://localhost:8761/eureka 

application-docker.properties for docker environment:

eureka.client.service-url.defaultZone = http://<your-eureka-service-name>:8761/eureka

Step two (docker-compose file):

myservice:
    container_name: myservice
    environment:
      SPRING_PROFILES_ACTIVE: docker

By setting up SPRING_PROFILES_ACTIVE to docker, it will load application-docker.properties as active profiles.

References:

  1. Spring Profiles

  2. Spring Environment-Specific Properties File

  3. How to load the SPRING_PROFILES_ACTIVE value in the client service dynamically?

Related