Search code examples
javaspringspring-bootspring-cloud-config

Override `spring.cloud.config.profile` at runtime


I'm using Spring cloud config, and I need to override my spring.cloud.config.profile property at runtime to either be blue or green

I have a method - determineConfigProfile() that returns blue or green but how to do I tell Spring Boot to run this method before fetching the config (i.e. resolving the config properties) and how do I tell Spring Boot to override the system property?

I see here https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html that "profile" = ${spring.profiles.active} (actually Environment.getActiveProfiles()) but I don't see how to set the active profiles.


Solution

  • Leaving this because I think it's useful, what I did was in my public class Application extends SpringBootServletInitializer:

    I did the following:

        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            String profile = determineConfigProfile();
            System.setProperty("spring.cloud.config.profile", profile);
            return application.sources(Application.class);
        }
    

    This runs before the Spring cloud config kicks off and it overrides the profile.