Eureka client in docker not connecting with Eureka server
Asked Answered
S

4

5

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?

Schottische answered 28/11, 2017 at 19:4 Comment(0)
T
4
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

Torpor answered 28/11, 2017 at 19:22 Comment(5)
Thanks for the reply. I have tried to put your changes in my eureka server yml file. But eclipse is saying that "ports" is not correct. And if I give port: 8761:8761 eureka server is not taking it. Server is getting exposed with the 8080 port of the embedded tomcat. Actually I am new to all these. May be I am not getting properly what you are saying. Can you please give me the exact values I can put?Schottische
Sorry I thought it's docker compose file, where it's actually a eureka config file, use port : 8761 like earlier. But in client which in docker connect to server using hostip:8761/eureka, before it's trying to connect with container itselfTorpor
can you accept the answer so it would be helpful for othersTorpor
Updated answer you accept it's solved you question, and other can use the solution if they face similar issueTorpor
how can I figure out hostip ?Vasti
G
4

Make sure you are running in Swarm mode.(Single node can also run Swarm)

$ docker swarm init

An overlay network is created so services can ping each other.

$ docker network create -d overlay mybridge

Set application.property for eurika client as below

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

Now create first discovery service (Eureka discover server)

$ docker service create  -d --name discovery --network mybridge \
    --replicas 1 -p 8761:8761 server-discovery

Open your browser and hit any node with port 8761

Now create client service:

$ docker service create  -d --name goodbyeapp --network mybridge \
    --replicas 1 -p 2222:2222 goodbye-service

This will register to the discovery service.

Getty answered 12/4, 2018 at 4:35 Comment(0)
G
1

In the container world, the eureka server ip address can change each time the eureka server is restarted. Hence specifying the host ip address for eureka server url doesn't work all the time.

In the docker-compose.yml, I had to link the eureka client service to the eureka server container. Until I link the services, eureka client couldn't connect to the server.

This is already answered in another post recently: Applications not registering to eureka when using docker-compose

Glaswegian answered 31/5, 2020 at 19:44 Comment(0)
J
0

Create Network

docker network create springboot-net

Note:- use container name in small letter case only

docker-compose.yml

  version: '3'
services:
  openeyeserviceregistry:
    container_name: openeyeserviceregistry
    build:
      context: ./ServiceRegistry/OpenEyeServiceRegistry/
      dockerfile: Dockerfile
    ports:
      - "8761:8761"
    networks:
      - springboot-net
    restart: on-failure

  openeyeimageservice:
    container_name: openeyeimageservice
    build:
      context: ./ImageService/OpenEyeImageService/
      dockerfile: Dockerfile
    ports:
      - "8083:8083"
    environment:
      - eureka.client.service-url.defaultZone=http://openeyeserviceregistry:8761/eureka/
      - eureka.client.enabled=true
      - eureka.host=OpenEyeServiceRegistry
      - eureka.instance.preferIpAddress=true

    depends_on:
      openeyeserviceregistry:
        condition: service_started
    networks:
      - springboot-net
    restart: on-failure
    

networks:
  springboot-net:
    name: springboot-net
    driver: bridge
    external: true

application.yml for eureka server

server:
  port: 8761
spring:
  application:
    name: ServiceRegistry
eureka:
  instance:
    prefer-ip-address: true
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

application for eureka client

    server:
  port: 8083
spring:
  application:
    name: IMAGE-SERVICE
eureka:
  instance:
    prefer-ip-address: true
    hostname: localhost    
  client:
    registerWithEureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/
  server:
    waitTimeInMsWhenSyncEmpty: 0

Docker file for eureka server

FROM openjdk:17-jdk-alpine
WORKDIR /opt
ENV PORT 8082
EXPOSE 8082
MAINTAINER dheeraj198922
COPY ./target/OpenEye_Service_Registry.jar /opt
ENTRYPOINT ["java","-jar","OpenEye_Service_Registry.jar"]

Dockerfile fore eureka client

 FROM openjdk:17-jdk-alpine
WORKDIR /opt
ENV PORT 8083
EXPOSE 8083
MAINTAINER dheeraj198922
COPY ./target/OpenEye_Image_Service.jar /opt
ENTRYPOINT ["java","-jar","OpenEye_Image_Service.jar"]
Jamshedpur answered 30/6 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.