2022-08-19

Spring Microservices Docker Issues (Connection Refused and Database Connection)

I'm trying to make an example of Spring Cloud. I have problem on Docker side. when I run docker-compose,

  1. Keycloak,rabbitmq,mysql and config server are running.

  2. Here is my error in Eureka Server

    Connect Timeout Exception

Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://eurekaserver:8761
/eureka/}, exception=I/O error on GET request for "http://eurekaserver:8761/eureka/apps/": Connect to eurekaserver:8761 [eurekaserver/172.22.0.6] failed: Connection refused (Connection
 refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to eurekaserver:8761 [eurekaserver/172.22.0.6] failed: Connection refused (Connection refused) sta
cktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://eurekaserver:8761/eureka/apps/": Connect to eurekaserver:8761 [eurekaserver/172.22.
0.6] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to eurekaserver:8761 [eurekaserver/172.22.0.6] failed:
Connection refused (Connection refused)

3 ) Here is my error in API Gateway

DiscoveryClient_API-GATEWAY/b522a17da5b3:api-gateway:8600: registering service...
2022-08-17 11:40:25.122  INFO 1 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eu
reka/}, exception=I/O error on POST request for "http://localhost:8761/eureka/apps/API-GATEWAY": Connect to localhost:8761 [localhost/127.0.0.1] failed:

4 ) Here is my error in Management Service

Request execution failed with message: I/O error on PUT request for "http://eurekaserve
r:8761/eureka/apps/MANAGEMENT-SERVICE/2c9b83d65d89:management-service:9002": Connect to eurekaserver:8761 [eurekaserver/172.18.0.5] failed: Connection refused (Connection refused); nes
ted exception is org.apache.http.conn.HttpHostConnectException: Connect to eurekaserver:8761 [eurekaserver/172.18.0.5] failed: Connection refused
  1. Here is my errors in User Service, Advertisement Service and Report Service
    java.lang.ClassCastException: class **org.hibernate.dialect.MySQL8Dialect cannot be cast to class java.sql.Driver** (org.hibernate.dialect.MySQL8Dialect is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @2096442d; java.sql.Driver is in module java.sql of loader 'platform')

Here is my docker compose file.

services:

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

  keycloak:
    image: quay.io/keycloak/keycloak:18.0.2
    environment:
      - KEYCLOAK_ADMIN=admin
      - KEYCLOAK_ADMIN_PASSWORD=admin
    ports:
      - "8181:8080"
    networks:
      - backend
    command:
      - start-dev

  rabbitmq:
    image: "rabbitmq:3-management"
    container_name: "rmq3"
    environment:
      RABBITMQ_DEFAULT_USER: "rabbitmq"
      RABBITMQ_DEFAULT_PASS: "123456"
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - backend

  configserver:
    image: configserver
    container_name: configServer
    build:
      context: ./configserver
      dockerfile: Dockerfile
    environment:
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
    ports:
      - "9191:9191"
    networks:
      - backend

  eurekaserver:
    image: eurekaserver
    ports:
      - "8761:8761"
    build:
      context: ./discoveryserver
      dockerfile: Dockerfile
    environment:
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      WAIT_HOSTS: configserver:9191
    depends_on:
      configserver:
        condition: service_started
    networks:
      - backend


  gatewayserver:
    image: gatewayserver
    ports:
      - "8600:8600"
    build:
      context: ./api-gateway
      dockerfile: Dockerfile
    environment:
      PROFILE: "default"
      SERVER_PORT: "8600"
      CONFIGSERVER_URI: "http://configserver:9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      CONFIGSERVER_PORT: "9191"
      WAIT_HOSTS: configserver:9191
    depends_on:
      configserver:
        condition: service_started
      eurekaserver:
        condition: service_started
    networks:
      - backend

  userservice:
    image: user-service
    build:
      context: ./user-service
      dockerfile: Dockerfile
    environment:
      SERVER_PORT: "9000"
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      WAIT_HOSTS: configserver:9191
      DATABASE_HOST: database
      DATABASE_USER: springmicroserviceuser
      DATABASE_PASSWORD: 111111
      DATABASE_NAME: springbootuser
      DATABASE_PORT: 3306
      SPRING_APPLICATION_JSON: '{
              "spring.jpa.hibernate.ddl-auto" : "update"
      }'
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
    ports:
      - "9000:9000"
    networks:
      - backend

  managementservice:
    image: management-service
    build:
      context: ./management-service
      dockerfile: Dockerfile
    environment:
      SERVER_PORT: "9002"
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      WAIT_HOSTS: configserver:9191
    depends_on:
      configserver:
        condition: service_started
      advertisementservice:
        condition: service_started
      reportservice:
        condition: service_started
    ports:
      - "9002:9002"
    networks:
      - backend

  advertisementservice:
    image: advertisement-service
    build:
      context: ./advertisement-service
      dockerfile: Dockerfile
    environment:
      SERVER_PORT: "9001"
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      WAIT_HOSTS: configserver:9191
      DATABASE_HOST: database
      DATABASE_USER: springmicroserviceuser
      DATABASE_PASSWORD: 111111
      DATABASE_NAME: springbootadvertisement
      DATABASE_PORT: 3306
      SPRING_APPLICATION_JSON: '{
                  "spring.jpa.hibernate.ddl-auto" : "update"
      }'
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
    ports:
      - "9001:9001"
    networks:
      - backend


  reportservice:
    image: report-service
    build:
      context: ./report-service
      dockerfile: Dockerfile
    environment:
      SERVER_PORT: "9003"
      CONFIGSERVER_URI: "http://configserver:9191"
      CONFIGSERVER_PORT: "9191"
      EUREKASERVER_URI: "http://eurekaserver:8761/eureka/"
      EUREKASERVER_PORT: "8761"
      WAIT_HOSTS: configserver:9191
      DATABASE_HOST: database
      DATABASE_USER: springmicroserviceuser
      DATABASE_PASSWORD: 111111
      DATABASE_NAME: springbootreport
      DATABASE_PORT: 3306
      SPRING_APPLICATION_JSON: '{
                      "spring.jpa.hibernate.ddl-auto" : "update"
      }'
    depends_on:
      database:
        condition: service_healthy
      configserver:
        condition: service_started
    ports:
      - "9003:9003"
    networks:
      - backend

networks:
  backend:
    driver: bridge


volumes:
  db-data:

Here is the project link : Link

Here are some screenshots for my issues : Link

1st Edited : I defined them for user, report and advertisement service.

SPRING_DATASOURCE_DRIVER_CLASS_NAME: java.sql.Driver
SPRING_DATASOURCE_DIALECT: org.hibernate.dialect.MySQL8Dialect

I got errors shown below.

Caused by: java.lang.RuntimeException: Failed to instantiate class java.sql.Driver
java.lang.NoSuchMethodException: java.sql.Driver.<init>()

2nd Editted : Here is the logs of eureka server : Link

3rd Editted : Updated docker-compose.yml

4th Editted : Here is the logs of eureka server : Link



No comments:

Post a Comment