I am using Spring Boot in combination with Spring Cloud Config Client to manage the configuration for my microservice. Additionally, I have a Spring Cloud Config Server to provide the configuration.
My microservice is designed to fetch configuration from the Spring Cloud Config Server, but if it's unavailable, it should fall back to using program parameters as default values. However, the issue I'm facing is that the program parameters are overriding the configuration fetched from the Spring Cloud Config Server.
Example:
Start of microservice: java -jar microservice.jar --setting.parameter=1
application.yml of microservice
spring:
config:
import: optional:configserver:http://localhost:8880
remote application.yml in github repo for microservice:
setting:
parameter: 2
result: The microservice application starts with setting.parameter=1. If i am starting the application without program parameters the result is setting.parameter=2
I found the solution by myself. If you are using spring cloud on client side you should not put the
spring.config.import
in theapplication.yml
. Instead you should put it into thebootstrap.yml
.Technically,
bootstrap.yml
is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that usesapplication.yml
.Conclusion: If you put the
spring.config.import
intoapplication.yml
theprogram parameters
override thespring cloud config
. If you put it into thebootstrap.yml
thespring cloud config
overrides theprogram parameters
Credits to: