Search code examples
spring-bootspring-cloud-configspring-cloud-config-serverspring-cloud-config-client

Spring Cloud Config does not override program arguments


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


Solution

  • I found the solution by myself. If you are using spring cloud on client side you should not put the spring.config.import in the application.yml. Instead you should put it into the bootstrap.yml.

    Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.

    Conclusion: If you put the spring.config.import into application.yml the program parameters override the spring cloud config. If you put it into the bootstrap.yml the spring cloud config overrides the program parameters

    Credits to: