4

I am trying to shift my spring-boot microservices to dockers. My microservices are running absolutely fine when implemented on STS on the local system. But when I dockerize them I get a connection timeout error.

I am sharing my code snippets below:

Docker-compose:

version: '3.6'
services:
  db:
    image: 'mysql:8.0.18'
    container_name: mysqldb
    ports:
      - '3300:3300'
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=root
    networks:
      - truyum-nw
      - movie-cruiser-nw
    volumes:
      - './mysqldb:/var/lib/mysql'
      - './dbscripts:/docker-entrypoint-initdb.d'
  config-server:
    image: spring-cloud-config-server
    build: ./spring-cloud-config-server
    container_name: spring-cloud-config-server
    ports:
      - '8888:8888'
    networks:
      - truyum-nw
      - movie-cruiser-nw
  eureka:
    image: eureka-discovery-service
    build: ./eureka-discovery-service
    container_name: eureka-discovery
    ports:
      - '8761:8761'
    depends_on:
      - config-server
      - db
    networks:
      - truyum-nw
      - movie-cruiser-nw
  zuul:
    image: zuul-service
    build: ./zuul-gateway
    container_name: zuul-bridge
    ports:
      - '8762:8762'
    depends_on:
      - eureka
      - config-server
    networks:
      - truyum-nw
      - movie-cruiser-nw
  auth-service:
    image: auth-service
    build: ./Authentication-service
    container_name: auth-service
    ports:
      - '9100:9100'
    depends_on:
      - eureka
      - config-server
    networks:
      - truyum-nw
      - movie-cruiser-nw
  menu-item-service:
    image: menu-item-service
    build: ./menuitem-service
    container_name: menu-item-service
    ports:
      - '34000:34000'
    depends_on:
      - eureka
      - config-server
      - zuul
      - db
      - auth-service
    networks:
      - truyum-nw
  cart-service:
    image: cart-service
    build: ./cart-service
    container_name: cart-service
    ports:
      - '34001:34001'
    depends_on:
      - eureka
      - config-server
      - zuul
      - db
      - menu-item-service
      - auth-service
    networks:
      - truyum-nw
  movie-service:
    image: movie-service
    build: ./movie-service
    container_name: movie-service
    ports:
      - '35000:35000'
    depends_on:
      - eureka
      - config-server
      - db
      - zuul
      - auth-service
    networks:
      - movie-cruiser-nw
  favourite-service:
    image: favourite-service
    build: ./favorite-service
    container_name: favourite-service
    ports:
      - '35001:35001'
    depends_on:
      - eureka
      - config-server
      - db
      - zuul
      - auth-service
      - movie-service
    networks:
      - movie-cruiser-nw
networks:
  truyum-nw:
    name: truyum-nw
    driver: bridge
  movie-cruiser-nw:
    name: movie-cruiser-nw
    driver: bridge

application.properties of spring-cloud-config-server:

spring.cloud.config.server.git.uri = https://github.com/satyamthedeveloper/Stage4_Week2_841418_SatyamKumar
server.port=8888

application.properties of Eureka-Discovery-Server:

spring.application.name=discoveryservice
spring.cloud.config.uri = http://spring-cloud-config-server:8888

 

When I do docker-compose up and I check http://localhost:8888/discoveryservice/default I get the result as

{
    "name": "discoveryservice",
    "profiles": [
        "default"
    ],
    "label": null,
    "version": "8450532e432fb103ef30d0002fa254b23d2158d6",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/satyamthedeveloper/Stage4_Week2_841418_SatyamKumar/discoveryservice.properties",
            "source": {
                "server.port": "8761",
                "eureka.client.register-with-eureka": "false",
                "eureka.client.fetch-registry": "false",
                "info.app.name": "Spring Eureka Server Application"
            }
        },
        {
            "name": "https://github.com/satyamthedeveloper/Stage4_Week2_841418_SatyamKumar/application.yml",
            "source": {
                "eureka.client.service-url.defaultZone": "http://eureka-discovery:8761/eureka",
                "logging.level.org.springframework.web": "DEBUG",
                "management.endpoints.web.exposure.include": "*"
            }
        }
    ]
}

But still, my Eureka discovery service starts on 8080 which is not accessible as I have not exposed it. I have tried a few of these steps which didn't help.

  1. tried stopping and restarting discovery service when my cloud-config is up and ready.
  2. tried it separately without docker-compose by creating network still it not worked.

I am not sure why my services are not able to fetch the link which I can easily fetch using URL.

This is a screenshot of my log which I get every time, no matter whatever I try.

SaKu.
  • 389
  • 4
  • 8

3 Answers3

2

The name that you must refer in your eureka configuration for config server is only config-server since this is the service name in your compose yaml.

Sergio Santiago
  • 1,316
  • 1
  • 11
  • 19
  • 1
    Nevertheless, it is wrong in your configuration. The `config-server` will be discovered by the name given in compose file. What you can do is to ssh into your container and try to ping and connect to config-server manually. the command: `docker exec -it bash` Check as well the file `/etc/resolve.conf` inside the container – Sergio Santiago Jul 09 '20 at 08:29
  • I was able to connect with config-server manually using Postman and Browser. somewhere my microservices was not able to connect to it. I finally got to resolve the issue by given method below. – SaKu. Jul 10 '20 at 01:52
2

After spending two days on it, I got to resolve this issue following way:

I shifted the properties which I was defining in application.properties to bootstrap.yml. The changes are as followed.

bootstrap.yml of spring-cloud-config-server:

spring:
    application:
        name: config-server
    cloud:
        config:
            server:
                git:
                    uri: https://github.com/satyamthedeveloper/Stage4_Week2_841418_SatyamKumar
                    clone-on-start: true

bootstrap.yml of Eureka-Discovery-Server:

spring:
    application:
        name: discoveryservice
    cloud:
        config:
            fail-fast: true
            retry:
                maxAttempts: 200
                maxInterval: 10000 
            uri: http://spring-cloud-config-server:8888

The reason behind such error was understood after getting to know the difference between bootstrap.yml and application.properties:

Use of application.properties is:

We use application.yml or application.properties for configuring the application context.

When a Spring Boot application starts, it creates an application context that doesn't need to be explicitly configured – it's already autoconfigured. However, Spring Boot offers different ways to override these properties.

And use of bootstrap.yml is:

We use bootstrap.yml or bootstrap.properties for configuring the bootstrap context. This way we keep the external configuration for bootstrap and main context nicely separated.

The bootstrap context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files.

When the Spring Cloud application starts, it creates a bootstrap context. The first thing to remember is that the bootstrap context is the parent context for the main application.

Another key point to remember is that these two contexts share the Environment, which is the source of external properties for any Spring application. In contrast with the application context, the bootstrap context uses a different convention for locating the external configuration.

You can refer the following blog for more specific information: Difference between Application.properties and Bootstrap.yml with example

SaKu.
  • 389
  • 4
  • 8
0

actually while running docker-compose.yml one image(for ex localhost:8761) doesn't know about localhost:8888(your config server).

your application.properties/ yml file is giving back localhost:port to eureka image which needs to get connect with other image/instance but one image is expecting other image uri i.e http://service_name:port so you need to pass it externally as environment variable

Add enviroment variable config server uri under eureka image i.e in docker-compose.yml:

 eureka:
image: eureka-discovery-service
build: ./eureka-discovery-service
container_name: eureka-discovery
ports:
  - '8761:8761'
depends_on:
  - config-server
environment: 
# Important for clients to register with config server
  
  - spring.config.import=optional:configserver:http://config-server:8888
  - db
networks:
  - truyum-nw
  - movie-cruiser-nw

also if you need to pass eureka in any eureka client you need to pass it as environment variable under eureka client image

 environment: 
# Important for clients to register with eureka
   eureka.client.serviceUrl.defaultZone=http://discovery-service:8761/eureka/

in the above code discover-service is the service name of eureka server (in port 8761).

Denil Nair
  • 86
  • 5
  • I have a same problem running Spring Boot Microservices on Kubernetes. Can you help me if you have any idea about it? Here is the link : https://stackoverflow.com/questions/75173708/kubernetes-issue-in-spring-boot-microservices-java-net-sockettimeoutexception-c – S.N Jan 22 '23 at 01:15