Home > Software engineering >  Docker Connection Issue in Spring Boot Microservice - nested exception is java.net.ConnectException:
Docker Connection Issue in Spring Boot Microservice - nested exception is java.net.ConnectException:

Time:12-19

I revised an example of Spring Boot Microservices and I have a problem about running all services on docker.

After creating the image file of each services, I devised a docker-compose.yml to run all services on Docker. I noticed some of services throw ConnectException regarding configserver but I have no idea how to fix it. Here are the code snippets to create Docker image for each service.

1 ) service-registry
   - mvn clean install
   - docker build -t microservicedailybuffer/serviceregistry:0.0.1 .

2 ) configserver
   - mvn clean install
   - docker build -t microservicedailybuffer/configserver:0.0.1 .

3 ) apigateway
   - mvn clean install -D skipTests
   - docker build -t microservicedailybuffer/apigateway:0.0.1 .

4 ) auth-service
   - mvn clean install -D skipTests
   - docker build -t microservicedailybuffer/authservice:0.0.1 .

5 ) orderservice
   - mvn clean install -D skipTests
   - docker build -t microservicedailybuffer/orderservice:0.0.1 .

6 ) productservice
   - mvn clean install -D skipTests
   - docker build -t microservicedailybuffer/productservice:0.0.1 .

7 ) paymentservice
   - mvn clean install -D skipTests
   - docker build -t microservicedailybuffer/paymentservice:0.0.1 .

Here is my docker-compose.yml shown below

services:

  serviceregistry:
    image: 'microservicedailybuffer/serviceregistry:0.0.1'
    container_name: serviceregistry
    ports:
      - '8761:8761'
    networks:
      - backend

  configserver:
    image: 'microservicedailybuffer/configserver:0.0.1'
    container_name: configserver
    ports:
      - '9296:9296'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
    depends_on:
      - serviceregistry
    networks:
      - backend

  apigateway:
    image: 'microservicedailybuffer/apigateway:0.0.1'
    container_name: apigateway
    ports:
      - '9090:9090'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - ZIPKIN_URL=http://zipkin:9411
      - REDIS_URL=redis://redis:6379
    depends_on:
      - configserver
      - zipkin
      - redis
      - serviceregistry
    networks:
      - backend


  zipkin:
    image: openzipkin/zipkin
    ports:
      - "9411:9411"
    networks:
      - backend

  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - backend


  authservice:
    image: 'microservicedailybuffer/authservice:0.0.1'
    container_name: authservice
    ports:
      - '7777:7777'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/userdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
    networks:
      - backend


  productservice:
    image: 'microservicedailybuffer/productservice:0.0.1'
    container_name: productservice
    ports:
      - '8081:8081'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/productdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend

  orderservice:
    image: 'microservicedailybuffer/orderservice:0.0.1'
    container_name: orderservice
    ports:
      - '8082:8082'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/orderdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend


  paymentservice:
    image: 'microservicedailybuffer/paymentservice:0.0.1'
    container_name: paymentservice
    ports:
      - '8083:8083'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - spring.datasource.url=jdbc:mysql://database:3306/paymentdb?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Turkey
      - spring.datasource.username=root
      - spring.datasource.password=ippavlova_1990
      - ZIPKIN_URL=http://zipkin:9411
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
      zipkin:
        condition: service_healthy
    networks:
      - backend


  database:
    container_name: mysql-database
    image: 'mysql:latest'
    ports:
      - "3306:3306"
    restart: always
    environment:
      #MYSQL_USER: root
      MYSQL_PASSWORD: ippavlova_1990
      MYSQL_ROOT_PASSWORD: ippavlova_1990
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - backend
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10


networks:
  backend:
    driver: bridge

volumes:
  db-data:

When I run the docker-compose file through docker-compose up, I get some problems in api gateway.

Api Gateway Issue

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

How can I fix all these issue?

Here is the repo : Link

Here is the git based system file : Link

CodePudding user response:

You have to update your application.yml and docker-compose.yml files.

These lines should be present to override the EUREKA_SERVER_ADDRESS

eureka:
  instance:
    prefer-ip-address: true
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: ${EUREKA_SERVER_ADDRESS:http://localhost:8761/eureka}

and to communicate within the container you need to use a common network. So it should look like this.


  serviceregistry:
    image: 'microservicedailybuffer/serviceregistry:0.0.1'
    container_name: serviceregistry
    ports:
      - '8761:8761'
    networks:
      - backend

  configserver:
    image: 'microservicedailybuffer/configserver:0.0.1'
    container_name: configserver
    ports:
      - '9296:9296'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
      interval: 10s
      timeout: 5s
      retries: 5
    depends_on:
      - serviceregistry
    networks:
      - backend

CodePudding user response:

After revising the api gateway shown below, the issue disappeared.

  apigateway:
    image: 'microservicedailybuffer/apigateway:0.0.1'
    container_name: apigateway
    ports:
      - '9090:9090'
    environment:
      - EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
      - CONFIG_SERVER_URL=configserver
      - ZIPKIN_URL=http://zipkin:9411
      - REDIS_URL=redis://redis:6379
    depends_on:
      configserver:
        condition: service_healthy
      zipkin:
        condition: service_healthy
      redis:
        condition: service_started
    networks:
      - backend
  • Related