• sc 使用了配置中心后,如何设置远程和本地配置的优先级


    在 spring 中,如何获取一个 key 的值?

    applicationContext.getEnvironment().getProperty("swagger.show")

    那么 key 的优先级呢?spring 会加载所有的配置文件,获取 key 的 value 时,会从前往后遍历这些配置文件,找到了即返回。所以,靠前的优先级高

    sc 中加载远程配置文件的逻辑:

    // org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#initialize
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        CompositePropertySource composite = new CompositePropertySource(
                BOOTSTRAP_PROPERTY_SOURCE_NAME);
        // NacosPropertySourceLocator 实现了这个接口,order 为 0
        AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
        boolean empty = true;
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        for (PropertySourceLocator locator : this.propertySourceLocators) {
            PropertySource<?> source = null;
            // nacos 拉取配置
            source = locator.locate(environment);
            if (source == null) {
                continue;
            }
            logger.info("Located property source: " + source);
            composite.addPropertySource(source);
            empty = false;
        }
        if (!empty) {
            MutablePropertySources propertySources = environment.getPropertySources();
            String logConfig = environment.resolvePlaceholders("${logging.config:}");
            LogFile logFile = LogFile.get(environment);
            if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
                propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
            }
            // 把远程配置加入到 environment 中
            insertPropertySources(propertySources, composite);
            reinitializeLoggingSystem(environment, logConfig, logFile);
            setLogLevels(applicationContext, environment);
            handleIncludedProfiles(environment);
        }
    }

    确定配置优先级的逻辑:

    // org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#insertPropertySources
    private void insertPropertySources(MutablePropertySources propertySources,
            CompositePropertySource composite) {
        MutablePropertySources incoming = new MutablePropertySources();
        incoming.addFirst(composite);
        PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
        Binder.get(environment(incoming)).bind("spring.cloud.config", Bindable.ofInstance(remoteProperties));
        if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone()
                && remoteProperties.isOverrideSystemProperties())) {
            // 远程配置优先级高
            propertySources.addFirst(composite);
            return;
        }
        if (remoteProperties.isOverrideNone()) {
            // 远程配置优先级低
            propertySources.addLast(composite);
            return;
        }
        if (propertySources
                .contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
            if (!remoteProperties.isOverrideSystemProperties()) {
                propertySources.addAfter(
                        StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
                        composite);
            }
            else {
                propertySources.addBefore(
                        StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
                        composite);
            }
        }
        else {
            propertySources.addLast(composite);
        }
    }

    如果需要使用本地配置覆盖远程配置,设置 spring.cloud.config.overrideNone = true

  • 相关阅读:
    Python学习Day1
    Linux使用外部邮箱发送邮件
    Linux命令学习1(awk、grep、sed)
    html笔记之表格
    html笔记之认识标签
    zabbix笔记之告警配置
    zabbix笔记之zabbix基础知识了解
    Windows之80端口被系统占用
    python笔记之流程控制
    python笔记之基本数据类型
  • 原文地址:https://www.cnblogs.com/allenwas3/p/11350734.html
Copyright © 2020-2023  润新知