Search code examples
spring-bootdocker-composedocker-network

Cannot connect to Eureka server - Spring Boot | Eureka | Docker


I'm building the microservices system using Spring 3.0 and I want to setup Eureka with Docker. I have 2 services called "AA service" and "Order service", and "Eureka server".

Setting for Eureka server is below:

application.properties

spring.application.name=eureka-server

server.port=8761

eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

EurekaServerApplication.class:

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

dependency:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'

Setting for AA service (similar wih Order service) - I tried both ways but none worked

application.properties The first way:

spring.application.name=aa-service

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.prefer-ip-address=true

The second way:

spring.application.name=aa-service

eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.prefer-ip-address=true

dependency:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.0.0'

docker-compose.yml:

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: khoadev
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - 5432:5432
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    container_name: pdadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped

  eureka-server:
    container_name: eurekaserver
    build:
      dockerfile: ./docker/eurekaserver.Dockerfile
    environment:
      SPRING_PROFILES_ACTIVE: docker
    ports:
      - "8761:8761"
    networks:
      - spring

  aaservice:
    container_name: aaservice
    build:
      dockerfile: ./docker/aaservice.Dockerfile
    environment:
      HOST_NAME: postgres
      DB_PORT: 5432
      USERNAME: khoadev
      PASSWORD: password
      DATABASE_NAME: user-service
      SECRET_KEY: secret_key
      SPRING_PROFILES_ACTIVE: docker
    ports:
      - "8080:8080"
    networks:
      - spring
      - postgres
    depends_on:
      - postgres

  orderservice:
    container_name: orderservice
    build:
      dockerfile: ./docker/orderservice.Dockerfile
    environment:
      HOST_NAME: postgres
      DB_PORT: 5432
      USERNAME: khoadev
      PASSWORD: password
      DATABASE_NAME: order-service
      SPRING_PROFILES_ACTIVE: docker
    ports:
      - "8181:8181"
    networks:
      - spring
      - postgres
    depends_on:
      - postgres


networks:
  postgres:
    driver: bridge

  spring:
    driver: bridge

volumes:
  postgres:
  pgadmin:

When I run 2 services, I got this error:

2023-03-04 22:59:50 2023-03-04T15:59:50.717Z  INFO 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_AA-SERVICE/d3e4061f76ba:aa-service: registering service...
2023-03-04 22:59:50 2023-03-04T15:59:50.722Z  INFO 1 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=I/O error on POST request for "http://localhost:8761/eureka/apps/AA-SERVICE": Connect to http://localhost:8761 [localhost/127.0.0.1] failed: Connection refused stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8761/eureka/apps/AA-SERVICE": Connect to http://localhost:8761 [localhost/127.0.0.1] failed: Connection refused

Anyone had the same problem? Is there any configuration missing? Thanks in advance!


Solution

  • This is neither spring problem nor Eureka.
    Simple docker hostname issue. Your networkings are correct.

    your container ports are exposed to localhost, so from your local machine you can connect to eureka server on localhost:8761. But when containers are talking to other container in the same network it's not localhost anymore.

    Because for aaservice localhost will point to itself not eurekaserver container. So to fix it, there is 2 way I could think of -

    1. [Recomended] Instead of localhost:8761 use container hostname (same as container name) i.e. eurekaserver:8761. In this case networking happens internally. So don't have to expose port (it's optional)
    2. OR you can go via host machine networking as you have exposed port 8761 to your local machine 8761. Use hostname as host.docker.internal:8761 instead of localhost. For more info on this follow this