Search code examples
dockerspring-bootnetflix-eureka

Eureka client in docker not connecting with Eureka server


I have one eureka server.

server:
  port: 8761
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

I have one eureka client.

spring:
  application:
    name: mysearch
server:
  port: 8020
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

My eureka client is running in a docker container.

FROM java:8
COPY ./mysearch.jar /var/tmp/app.jar
EXPOSE 8180
CMD ["java","-jar","/var/tmp/app.jar"]

I am starting the eureka server by java -jar eureka-server.jar After that I am starting the docker instance of the eureka client using sudo docker build -t web . and sudo docker run -p 8180:8020 -it web. I am able to access the eureka client and server from browser but the client is not connecting with Eureka server. I am not able to see the client in the eureka server dashboard. I am getting below errors and warnings.

WARN 1 --- [tbeatExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connection refused (Connection refused)
ERROR 1 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_FLIGHTSEARCH/98b0d95fd668:flightsearch:8020 - was unable to send heartbeat!
INFO 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_FLIGHTSEARCH/98b0d95fd668:flightsearch:8020: registering service...
ERROR 1 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error
WARN 1 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connection refused (Connection refused)
WARN 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_FLIGHTSEARCH/98b0d95fd668:flightsearch:8020 - registration failed Cannot execute request on any known server
WARN 1 --- [nfoReplicator-0] c.n.discovery.InstanceInfoReplicator     : There was a problem with the instance info replicator

I am doing it in an AWS EC2 Ubuntu instance. Can anyone please tell me what I am doing wrong here?


Solution

  • server:
      ports:
          - "8761:8761"
    eureka:
      client:
        registerWithEureka: false
        fetchRegistry: false
    

    with the above changes port 8761 will expose on host and can connect to server. as your connecting using localhost "http://localhost:8761/eureka" which is searching for port 8761 on host.

    In Eureka client config use host ip instead of localhost , because if localhost used it's search for port 8761 within container

    http://hostip:8761/eureka