Asked 1 month ago by NebulousCaptain281
Why Does spring.config.import Not Override Environment Variables in a Spring Boot 3 Microservice?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by NebulousCaptain281
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a project with the following microservices:
In both FE and BE services, I configured the property spring.config.import to connect with the Config Server. For example, in the BE project (springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE) the application.properties file is as follows:
PROPERTIES# Port server.port=8081 # Service Name spring.application.name=be # Config Server spring.config.import=configserver:http://localhost:8888
I attempted to override this property using the docker-compose.yaml file by setting spring.config.import: configserver:http://config:8888:
YAMLbe: 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 run the setup with the command:
BASHdocker-compose up -d --build
The following error appears in Docker Desktop:
PLAINTEXT2025-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 the BE service is still connecting to localhost instead of the intended config service. This issue also occurs in the FE service and when using Docker and Kubernetes environment variables.
Tested on:
Why is the spring.config.import property not being overwritten by environment variables from Docker, Docker Compose, or Kubernetes?
Comments 0
•Answers 3
•Views 43
Answer by TychoAI Agent • 1 month ago
The issue is that Spring Boot expects environment variable overrides to use the standard naming convention. In Docker (or Kubernetes) you need to provide the property key in uppercase with dots replaced by underscores. In your compose file you’re setting
YAMLenvironment: spring.config.import: configserver:http://config:8888
but Spring Boot will not map that to the property override. Instead, you should set:
YAMLenvironment: SPRING_CONFIG_IMPORT: configserver:http://config:8888
This change will allow Spring Boot to recognize the environment variable correctly so that it overrides the value specified in application.properties.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by MercurialGuide900 • 1 month ago
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
No comments yet.
Answer by CelestialKeeper133 • 1 month ago
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:
BASHmvn -f ./springcloud-fe-thymeleaf-be-springboot-db-sql-mysql-config_BE spring-boot:run -Dspring-boot.run.arguments="--spring.config.import=configserver:http://localhost:8888"
No comments yet.
No comments yet.