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.
I am not sure why my services are not able to fetch the link which I can easily fetch using URL.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With