【问题标题】:Override Spring Cloud Config values with environment使用环境覆盖 Spring Cloud Config 值
【发布时间】:2015-10-11 14:45:07
【问题描述】:

有没有办法用另一个属性源(特别是系统环境)覆盖通过 Spring Cloud Config Server 设置的属性?我知道我可以通过循环遍历Environment 对象的PropertySources 来手动执行此操作,但如果我可以将其设置为bootstrapConfig 源的优先级最低,那将是理想的。

【问题讨论】:

  • 您有没有找到一种无需编写自己的应用程序侦听器就能做到这一点的方法?
  • 不,没有其他方法可以做到这一点,至少在 Spring Boot 1.2.x 中没有。我还没有检查 1.3.x (Spring Cloud Brixton)。

标签: java spring spring-boot cloud-foundry spring-cloud


【解决方案1】:

FWIW,我通过编写自定义 ApplicationListener 来完成此操作,其事件在周期早期触发,但在加载配置服务的 PropertySource 之后。我在这里附上了代码,以防有人感兴趣。如果有一种“官方”的 Spring 方式可以做到这一点,我仍然很感兴趣,但这有效:

package com.example;

import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

@Order(Ordered.HIGHEST_PRECEDENCE)
public class ConfigServicePropertyDeprioritizer
        implements ApplicationListener<ApplicationPreparedEvent>
{
    private static final String CONFIG_SOURCE = "bootstrap";

    private static final String PRIORITY_SOURCE = "systemEnvironment";

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event)
    {
        ConfigurableEnvironment environment = event.getApplicationContext()
                .getEnvironment();
        MutablePropertySources sources = environment.getPropertySources();
        PropertySource<?> bootstrap = findSourceToMove(sources);

        if (bootstrap != null)
        {
            sources.addAfter(PRIORITY_SOURCE, bootstrap);
        }
    }

    private PropertySource<?> findSourceToMove(MutablePropertySources sources)
    {
        boolean foundPrioritySource = false;

        for (PropertySource<?> source : sources)
        {
            if (PRIORITY_SOURCE.equals(source.getName()))
            {
                foundPrioritySource = true;
                continue;
            }

            if (CONFIG_SOURCE.equals(source.getName()))
            {
                // during bootstrapping, the "bootstrap" PropertySource
                // is a simple MapPropertySource, which we don't want to
                // use, as it's eventually removed. The real values will 
                // be in a CompositePropertySource
                if (source instanceof CompositePropertySource)
                {
                    return foundPrioritySource ? null : source;
                }
            }
        }

        return null;
    }
}

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 2017-08-09
    • 1970-01-01
    • 2016-09-15
    • 2018-06-10
    • 2017-10-03
    • 2015-09-13
    • 2020-03-15
    • 1970-01-01
    相关资源
    最近更新 更多