I have the project with following microservices:
- FE: Spring Boot 3 application which displays information to the user
- BE: Spring Boot 3 application which connects with DB and sends information to FE
- MySql: database which stores data
- Config Sever: Spring Cloud application which stores services configurations on Github
In FE and BE services I have configured property spring.config.import which connects these services with Config Server. For instance in BE project (springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE) I have following application.properties file:
# Port
server.port=8081
# Service Name
spring.application.name=be
# Config Server
spring.config.import=configserver:http://localhost:8888
I try to overwrite this property in docker-compose.yaml file (spring.config.import: configserver:http://config:8888):
be:
image: be-image:0.0.1
container_name: be-container
build:
context: ./springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE
dockerfile: Dockerfile
depends_on:
config:
condition: service_healthy
ports:
- 8081:8081
environment:
spring.config.import: configserver:http://config:8888
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8081/actuator/health"]
interval: 10s
timeout: 10s
retries: 5
start_period: 10s
networks:
- helloworld-network
I execute docker compose using following command:
docker-compose up -d --build
I get following error in docker desktop:
2025-01-20 17:02:36 Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/be/default": Connection refused
2025-01-20 17:02:36 at org.springframework.web.client.RestTemplate.createResourceAccessException(RestTemplate.java:926)
2025-01-20 17:02:36 at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:906)
It seems that BE service tries to connect to localhost instead of config sevice. The same happens wit FE service and Docker and Kubernetes environment properties.
Tested on:
- Java: 23
- Spring Boot: 3.4.1
- Spring Cloud: 2024.0.0
- Mvn: 3.9.6
Link to Github repository: https://github.com/wisniewskikr/chrisblog-it-cloud/tree/main/spring-cloud/config/springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config
Any idea why property spring.config.import is not overwritten by environment variables from Docker, Docker Compose or Kubernetes?
2
Answers
I found workaround for this problem. If there is NO property spring.config.import in application.properties file then environment variables from Docker, Docker Compose and Kubernetes work file.
Problem is when I try to run this service manually from command line tool. I have to use command with parameter spring-boot.run.arguments then:
The ‘.’ in an environment variable that should overwrite a matching application.properties value should be replaced by an ‘_’ and convert to uppercase, ie SPRING_CONFIG_IMPORT – see here