使用环境覆盖Spring Cloud Config值 [英] Override Spring Cloud Config values with environment

查看:228
本文介绍了使用环境覆盖Spring Cloud Config值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以通过Spring Cloud Config Server用另一个属性源(特别是系统环境)覆盖设置的属性?我知道我可以通过遍历 Environment 对象的 PropertySource s来手动完成此操作,但是如果可以将其设置为 bootstrapConfig 源是最低优先级,这是理想的选择。

Is there a way to override properties set via a Spring Cloud Config Server with another property source (specifically, the system environment)? I know I could manually do it by looping through the Environment object's PropertySources, but if I could set it up so that the bootstrapConfig source was lowest priority, that would be ideal.

推荐答案

FWIW,我通过编写一个自定义的 ApplicationListener 完成了该任务,该事件在周期的早期触发,但是在Config Service的 PropertySource 已加载。如果有人感兴趣,请在此处附加代码。如果有一种正式的 Spring方式可以做到这一点,我仍然很感兴趣,但这可以起作用:

FWIW, I accomplished this by writing a custom ApplicationListener whose Event fired early in the cycle, but after the Config Service's PropertySource was loaded. I've attached the code here in case anyone is interested. If there's an "official" Spring way to do this, I'm still interested, but this works:

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;
    }
}

这篇关于使用环境覆盖Spring Cloud Config值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆