简体   繁体   English

在运行时覆盖`spring.cloud.config.profile`

[英]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 我正在使用Spring cloud config,我需要在运行时覆盖我的spring.cloud.config.profile属性为bluegreen

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? 我有一个方法determineConfigProfile()返回bluegreen但是如何在获取配置之前告诉Spring Boot运行该方法(即解析配置属性),如何告诉Spring Boot覆盖系统属性?

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. 我在这里看到https://cloud.spring.io/spring-cloud-config/multi/multi__spring_cloud_config_client.html那个"profile" = ${spring.profiles.active} (actually Environment.getActiveProfiles())但我没有了解如何设置活动配置文件。

Leaving this because I think it's useful, what I did was in my public class Application extends SpringBootServletInitializer : 离开这个是因为我认为这很有用,我所做的是在我的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. 该操作在Spring云配置启动并覆盖配置文件之前运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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