• 【nacos】加载配置文件顺序解析


    首先说结论

    • nacos首先加载SharedConfiguration也就是共享配置文件
      cloud.nacos.config.shared-configs下指定,可以是多个
    • 第二步加载扩展配置
      cloud.nacos.config.ext-configs下指定,也可以是多个
    • 第三步根据spring.cloud.nacos.config.prefix(这个如果没有指定的话则默认是application.name)再拼上spring.cloud.nacos.config.file-extension加载
    • 第四步根据spring.cloud.nacos.config.prefix再拼上spring.cloud.nacos.config.file-extension再拼上spring.cloud.nacos.config.file-extension加载
    • 第五步根据spring.cloud.nacos.config.prefix拼上profiles.active再拼上spring.cloud.nacos.config.file-extension加载

    即如果我的共享配置指定为application.yml扩展配置为redis.yml,application.name是app1,active是dev的话加载顺序如下

    1. application.yml
    2. redis.yml
    3. app1.yml
    4. app1.yml.yml
    5. app1-dev.yml

    这里注意优先级,优先级是跟加载顺序相反,即application.name+active+file-extension的优先级最高

    nacos加载配置文件顺序解析

    入口class

    入口文件在NacosPropertySourceLocator,其中加载的顺序代码如下
    this.loadSharedConfiguration(composite);
    this.loadExtConfiguration(composite);
    this.loadApplicationConfiguration(composite, dataIdPrefix, this.nacosConfigProperties, env);

    this.loadSharedConfiguration这里就是先加载的共享配置
    this.loadExtConfiguration加载扩展配置
    this.loadApplicationConfiguration加载该项目独有的配置,在这个方法里面有分别尝试加载

        private void loadApplicationConfiguration(CompositePropertySource compositePropertySource, String dataIdPrefix, NacosConfigProperties properties, Environment environment) {
            String fileExtension = properties.getFileExtension();
            String nacosGroup = properties.getGroup();
            // dataIdPrefix+fileExtension  即 app.yml
            this.loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup, fileExtension, true);
            // dataIdPrefix+ "." + fileExtension + fileExtension  即 app.yml.yml
            this.loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + "." + fileExtension, nacosGroup, fileExtension, true);
            String[] var7 = environment.getActiveProfiles();
            int var8 = var7.length;
    
            for(int var9 = 0; var9 < var8; ++var9) {
                String profile = var7[var9];
                String dataId = dataIdPrefix + "-" + profile + "." + fileExtension;
                // ddataIdPrefix + "-" + profile + "." + fileExtension  即 app-dev.yml
                this.loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true);
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    nacos加载按顺序加载完所有配置文件后,根据顺序将list返回

    优先级生效的源码

    加载配置文件的代码是在PropertySourceBootstrapConfiguration的initialize中

        public void initialize(ConfigurableApplicationContext applicationContext) {
            List<PropertySource<?>> composite = new ArrayList();
            AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
            boolean empty = true;
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
            Iterator var5 = this.propertySourceLocators.iterator();
    
            while(true) {
                Collection source;
                do {
                    do {
                        if (!var5.hasNext()) {
                            if (!empty) {
                                MutablePropertySources propertySources = environment.getPropertySources();
                                String logConfig = environment.resolvePlaceholders("${logging.config:}");
                                LogFile logFile = LogFile.get(environment);
                                Iterator var15 = environment.getPropertySources().iterator();
    
                                while(var15.hasNext()) {
                                    PropertySource<?> p = (PropertySource)var15.next();
                                    if (p.getName().startsWith("bootstrapProperties")) {
                                        propertySources.remove(p.getName());
                                    }
                                }
    							//当所有配置文件加载完成后就会进入到这个方法里面将配置参数加载到内存里
                                this.insertPropertySources(propertySources, composite);
                                this.reinitializeLoggingSystem(environment, logConfig, logFile);
                                this.setLogLevels(applicationContext, environment);
                                this.handleIncludedProfiles(environment);
                            }
    
                            return;
                        }
    
                        PropertySourceLocator locator = (PropertySourceLocator)var5.next();
                        // 调用所有实现PropertySourceLocator的方法加载配置文件(刚才的入口类就实现了他)
                        source = locator.locateCollection(environment);
                    } while(source == null);
                } while(source.size() == 0);
    
                List<PropertySource<?>> sourceList = new ArrayList();
                Iterator var9 = source.iterator();
    
                while(var9.hasNext()) {
                    PropertySource<?> p = (PropertySource)var9.next();
                    if (p instanceof EnumerablePropertySource) {
                        EnumerablePropertySource<?> enumerable = (EnumerablePropertySource)p;
                        sourceList.add(new BootstrapPropertySource(enumerable));
                    } else {
                        sourceList.add(new SimpleBootstrapPropertySource(p));
                    }
                }
    
                logger.info("Located property source: " + sourceList);
                composite.addAll(sourceList);
                empty = false;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    这里需要重点关注一下,这个就是优先级的原理

    private void insertPropertySources(MutablePropertySources propertySources, List<PropertySource<?>> composite) {
            MutablePropertySources incoming = new MutablePropertySources();
            List<PropertySource<?>> reversedComposite = new ArrayList(composite);
            // 重点在这里,当所有配置文件加载完读取之前,spring对配置文件的list做了反转,所以后加载的优先度最高
            Collections.reverse(reversedComposite);
            Iterator var5 = reversedComposite.iterator();
    
            while(var5.hasNext()) {
                PropertySource<?> p = (PropertySource)var5.next();
                // addFirst这个方法,是加之前先判断如果已经有的话不再加载
                incoming.addFirst(p);
            }
    
            PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
            Binder.get(this.environment(incoming)).bind("spring.cloud.config", Bindable.ofInstance(remoteProperties));
            PropertySource p;
            Iterator var9;
            if (remoteProperties.isAllowOverride() && (remoteProperties.isOverrideNone() || !remoteProperties.isOverrideSystemProperties())) {
                if (remoteProperties.isOverrideNone()) {
                    var9 = composite.iterator();
    
                    while(var9.hasNext()) {
                        p = (PropertySource)var9.next();
                        propertySources.addLast(p);
                    }
    
                } else {
                    if (propertySources.contains("systemEnvironment")) {
                        if (!remoteProperties.isOverrideSystemProperties()) {
                            var9 = reversedComposite.iterator();
    
                            while(var9.hasNext()) {
                                p = (PropertySource)var9.next();
                                propertySources.addAfter("systemEnvironment", p);
                            }
                        } else {
                            var9 = composite.iterator();
    
                            while(var9.hasNext()) {
                                p = (PropertySource)var9.next();
                                propertySources.addBefore("systemEnvironment", p);
                            }
                        }
                    } else {
                        var9 = composite.iterator();
    
                        while(var9.hasNext()) {
                            p = (PropertySource)var9.next();
                            propertySources.addLast(p);
                        }
                    }
    
                }
            } else {
                var9 = reversedComposite.iterator();
    
                while(var9.hasNext()) {
                    p = (PropertySource)var9.next();
                    propertySources.addFirst(p);
                }
    
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    OkHttp搞定Http请求
    CSS简介
    一篇文章教你Pytest快速入门和基础讲解,一定要看!
    PHP Curl请求封装
    【JavaScript】DOM的其他查询
    PMP 每日练习题打卡,11 月考试的宝子跟着我一起的打卡到吧
    音频采集原理
    【面试】测试/测开(未完成版)
    Apache commons email邮件工具类简介及使用说明
    c++四种强制类型转换
  • 原文地址:https://blog.csdn.net/baidu_29609961/article/details/133746540