0

I have microservices application, and i'm using Spring cloud and Spring boot v2.3 . i'm depending on config server and eureka, everything is working fine on my local machine from IDE, but when I deployed the stack on docker, my app Auth-Service cannot fetch from spring config server container, and give me the following stack trace

2020-07-05 02:23:52.888  INFO [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-07-05 02:23:53.091  INFO [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2020-07-05 02:23:53.092  WARN [auth-service,,,] 1 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)

I thought the issue is just waiting until or container-network as mentioned here Spring config is no accessible and this Microservices can't reach config service, but I ensured the containers are within same network, and to simulate, I started the config first, but got same error.

Here are the configurations i'm using

version: '3.8'
services:
    eureka-service:
      build: 
        context: ./eureka
      image: eureka-service:latest
      container_name: eureka
      ports:
        - 8761:8761
      hostname: eureka
      networks: 
        - mynetwork
    config-service:
      build: 
        context: ./configServer
      image: config-server:latest
      ports:
        - 8888:8888 
      networks: 
        - mynetwork
    proxy-service:
      build: 
        context: ./web-cv-proxy
      image: zuul-service:latest
      ports:
        - 8081:8081
      depends_on: 
        - config-service
      networks: 
        - mynetwork
    auth-service:
      build: 
        context: ./web-based-cv/auth-service
      image: auth-service:latest
      ports:
        - 8060:8060
      depends_on: 
        - config-service
        - eureka-service
      restart: on-failure
      networks: 
        - mynetwork
    portal-service:
      build: 
        context: ./web-based-cv/cv-portal
      image: cv-portal:latest
      ports:
        - 9090:9090
      depends_on: 
        - config-service
        - eureka-service
      restart: on-failure
      networks: 
        - mynetwork
networks: 
  mynetwork:
    driver: bridge     

Config server docker file

FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="configuration server"
COPY ./target/configServer-0.0.1-SNAPSHOT.jar ./
EXPOSE 8888
CMD ["java", "-jar", "configServer-0.0.1-SNAPSHOT.jar"]

Application docker file

FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="cv authintication service"
COPY ./target/auth-service-1.0.0-exec.jar auth-service-1.0.0-exec.jar
EXPOSE 8060
CMD ["java", "-jar", "/auth-service-1.0.0-exec.jar"]

The application bootstrap file

spring:
  datasource:
    jpa:
      properties:
        hibernate:
          format_sql: true
          ddl-auto: none
  application:
    name: auth-service
    bus:
      refresh:
        enabled: true
    profiles:
      active: jdbc
  cloud:
    retry:
      initial-interval: 1500
      multiplier: 1.5
      max-attempts: 10000
      max-interval: 1000

server:
  servlet:
    context-path: /api/auth
  port: 8060

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

management:
  endpoints:
    web:
      exposure:
        include: ["health","info","refresh", "bus-refresh"]

Your advice is highly appreciated , and thanks in advance.

Mohamed Sweelam
  • 1,109
  • 8
  • 22

3 Answers3

3
  • Remember one thing, when you deploy your micro-services on docker, localhost won't work there as your micoservices run in a container within a different virtual network. So it won't be identified using localhost.
  • So in your docker compose file, you have to provide the name of your the service of your Eureka server.

For example -

services:
  discovery:
    image: <Eureka server image name>
    ports:
      - 8761:8761
  ConfigServerService:
    image: <Config server image name>
    environment:
      - JAVA_OPTS=
        -DEUREKA_SERVER=http://discovery:8761/eureka
    depends_on:
      - discovery
    links:
      - discovery   
    ports:
      - 8888:8888  
  Microservice1:
    image: <Microservice1 image name>
    environment:
      - JAVA_OPTS=
        -DEUREKA_SERVER=http://discovery:8761/eureka
    depends_on:
      - discovery
      - ConfigServerService
    links:
      - discovery
      - ConfigServerService
    ports:
      - 8000:8000
P.Sanjay
  • 303
  • 1
  • 7
1

You need to change auth-service configuration from localhost:8888 to http://config-service:8888, as the error seems not getting the correct endpoint.

docker-compose networking

error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused);

localhost:8888 means this container (auth-service) not the spring config server.

Adiii
  • 54,482
  • 7
  • 145
  • 148
0

For some reason my containers didn't be able to connect using direct uri configuration, although I added them to the same network.

I solved the issue with depending on Eureka itself, and connecting to Spring config through eureka discovery with the following config in client apps

spring 
  config:
    discovery:
      enabled: true
      service-id: configServer

I think it is a good way to give the control to eureka, but I hoped i would know why it didn't work with uri instead? by the way this way will give me more flexibility if I deployed on several clusters.

Mohamed Sweelam
  • 1,109
  • 8
  • 22