简体   繁体   中英

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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM