从源码角度分析 Nacos 配置文件加载以及加载优先级

ゝ一纸荒年。 2024-03-26 13:45 121阅读 0赞

#

      • 前言
      • 核心类 NacosPropertySourceLocator 加载过程
      • 核心方法 NacosPropertySourceLocator#locate
      • 总结

前言

源码版本以 2021.0.1.0 版本进行分析:spring-cloud-starter-alibaba-nacos-config-2021.0.1.0

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  4. <version>2021.0.1.0</version>
  5. </dependency>

核心类 NacosPropertySourceLocator 加载过程

当项目中引入了当前依赖,可以查看引入的依赖信息,关心它自动装配的类,规则加载流程主要关注这个类
在这里插入图片描述

  1. // 对 @Bean 注解标注方法不通过代理调用
  2. @Configuration(proxyBeanMethods = false)
  3. // spring.cloud.nacos.config.enabled 默认为 true
  4. @ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
  5. public class NacosConfigBootstrapConfiguration {
  6. @Bean
  7. @ConditionalOnMissingBean
  8. public NacosConfigProperties nacosConfigProperties() {
  9. // spring.cloud.nacos.config 前缀的所有配置内容,都存储在这个类下面
  10. return new NacosConfigProperties();
  11. }
  12. @Bean
  13. @ConditionalOnMissingBean
  14. public NacosConfigManager nacosConfigManager(
  15. NacosConfigProperties nacosConfigProperties) {
  16. // 用于管理配置内容,创建 Nacos 客户端动态刷新的核心类以及设置动态刷新的监听
  17. return new NacosConfigManager(nacosConfigProperties);
  18. }
  19. @Bean
  20. public NacosPropertySourceLocator nacosPropertySourceLocator(
  21. NacosConfigManager nacosConfigManager) {
  22. // 属性配置文件解析
  23. return new NacosPropertySourceLocator(nacosConfigManager);
  24. }
  25. }

NacosPropertySourceLocator 类是处理配置规则加载的核心类;NacosConfigProperties 类是基础配置类存储配置信息的;NacosConfigManager 是用于所有配置源的,会使用它来创建动态配置的监听以及 Nacos 客户端核心类

NacosConfigManager 依赖于 NacosConfigProperties,会优先注入

NacosPropertySourceLocator 依赖于 NacosConfigManager,会优先注入

加载配置的方法入口:NacosPropertySourceLocator#locate,首先梳理该方法是如何被调用的,Debug 断点在该方法上,启动应用,就可以看到下面调用的方法堆栈信息:
在这里插入图片描述
流程图梳理如下:

\[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R8sGf3tH-1677233454924)(property-source-locator-entrance.png)\]

核心方法 NacosPropertySourceLocator#locate

在这里主要介绍的是 NacosPropertySourceLocator 类,看它是如何加载配置文件内容以及配置文件优先级处理的?

到这里,开始分析 NacosPropertySourceLocator#locate 方法源码的处理过程了

  1. public PropertySource<?> locate(Environment env) {
  2. nacosConfigProperties.setEnvironment(env);
  3. ConfigService configService = nacosConfigManager.getConfigService();
  4. if (null == configService) {
  5. log.warn("no instance of config service found, can't load config from nacos");
  6. return null;
  7. }
  8. // 设置读取配置的超时时间,默认为 3000 毫秒
  9. long timeout = nacosConfigProperties.getTimeout();
  10. nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout);
  11. String name = nacosConfigProperties.getName();
  12. // 优先获取此属性:spring.cloud.nacos.config.prefix
  13. String dataIdPrefix = nacosConfigProperties.getPrefix();
  14. if (StringUtils.isEmpty(dataIdPrefix)) {
  15. // 未配置取:spring.cloud.nacos.config.name
  16. dataIdPrefix = name;
  17. }
  18. if (StringUtils.isEmpty(dataIdPrefix)) {
  19. // 未配置取:spring.application.name
  20. dataIdPrefix = env.getProperty("spring.application.name");
  21. }
  22. CompositePropertySource composite = new CompositePropertySource(
  23. NACOS_PROPERTY_SOURCE_NAME);
  24. // 优先级在这里体现:shared-configs < ext-configs < 当前项目配置
  25. // 当前项目配置内也存在一个优先级:default < 扩展符配置 < 环境标识符配置
  26. // 在这些配置文件中设置了 refresh=true 标识,才进行动态刷新,先会追加到监听器中
  27. loadSharedConfiguration(composite);
  28. loadExtConfiguration(composite);
  29. loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
  30. // 组合所有读取到的配置文件返回,后续再 Spring 子容器中调用 refresh 方法时再填充属性阶段进行赋值.
  31. return composite;
  32. }

此方法加载了在 bootstrap 中配置的文件,应用名|应用名+后缀|应用名+profile+后缀、shared-configs、extension-configs

从源码上就可以看出,最后加载的文件肯定是优先级最高的,即 loadApplicationConfiguration 方法,该方法内又区分了应用内多个配置文件下的优先级:应用名 < 应用名+后缀 < 应用名+profile+后缀

  1. /**
  2. * 加载 shared-configs 配置
  3. */
  4. private void loadSharedConfiguration(
  5. CompositePropertySource compositePropertySource) {
  6. List<NacosConfigProperties.Config> sharedConfigs = nacosConfigProperties
  7. .getSharedConfigs();
  8. if (!CollectionUtils.isEmpty(sharedConfigs)) {
  9. checkConfiguration(sharedConfigs, "shared-configs");
  10. loadNacosConfiguration(compositePropertySource, sharedConfigs);
  11. }
  12. }
  13. /**
  14. * 加载 extension-configs 配置
  15. */
  16. private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
  17. List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
  18. .getExtensionConfigs();
  19. if (!CollectionUtils.isEmpty(extConfigs)) {
  20. checkConfiguration(extConfigs, "extension-configs");
  21. loadNacosConfiguration(compositePropertySource, extConfigs);
  22. }
  23. }
  24. /**
  25. * 加载应用内配置
  26. */
  27. private void loadApplicationConfiguration(
  28. CompositePropertySource compositePropertySource, String dataIdPrefix,
  29. NacosConfigProperties properties, Environment environment) {
  30. // 不设置,默认值:properties
  31. String fileExtension = properties.getFileExtension();
  32. String nacosGroup = properties.getGroup();
  33. // 加载默认的,不追加扩展符的 data-id 文件
  34. loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup,
  35. fileExtension, true);
  36. // 加载带扩展符的 data-id 文件,优先级大于默认的
  37. loadNacosDataIfPresent(compositePropertySource,
  38. dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
  39. // 加载带环境标识的 data-id 文件,优先级大于带扩展符的
  40. for (String profile : environment.getActiveProfiles()) {
  41. String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
  42. loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
  43. fileExtension, true);
  44. }
  45. }

加载 shared-configs、extension-configs 配置时会去检测 dataId 是否为空,为空就会出现异常

  1. /**
  2. * 检查配置是否有问题,dataId 不允许为空
  3. */
  4. private void checkConfiguration(List<NacosConfigProperties.Config> configs, String tips) {
  5. for (int i = 0; i < configs.size(); i++) {
  6. String dataId = configs.get(i).getDataId();
  7. if (dataId == null || dataId.trim().length() == 0) {
  8. throw new IllegalStateException(String.format(
  9. "the [ spring.cloud.nacos.config.%s[%s] ] must give a dataId",
  10. tips, i));
  11. }
  12. }
  13. }

在这里插入图片描述

以上源码图是加载属性的部分,在加载属性时,若已经动态刷新过了&未开启动态刷新,就会取用之前的数据;否则,就会调用 NacosPropertySourceBuilder#build 方法

  1. NacosPropertySource build(String dataId, String group, String fileExtension,
  2. boolean isRefreshable) {
  3. // 调用 NacosConfigService.getConfig 方法获取 dataId 对应的配置内容
  4. List<PropertySource<?>> propertySources = loadNacosData(dataId, group, fileExtension);
  5. NacosPropertySource nacosPropertySource = new NacosPropertySource(propertySources,
  6. group, dataId, new Date(), isRefreshable);
  7. // 封装完对象后存入到 NacosPropertySourceRepository#NACOS_PROPERTY_SOURCE_REPOSITORY 集合中
  8. NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);
  9. return nacosPropertySource;
  10. }

从上面源码可以看出,若开启了刷新机制,就会往 NacosPropertySourceRepository#NACOS_PROPERTY_SOURCE_REPOSITORY 集合中塞数据;若未开启的话,就不会塞数据

NacosPropertySourceRepository#NACOS_PROPERTY_SOURCE_REPOSITORY 集合里的元素是后续用来注册监听器的,涉及到动态监听的部分了,这块后续有文章对整个动态刷新机制源码的讲解

无论其是否开启动态刷新,最终都会把数据添加到 CompositePropertySource#propertySources 集合中,它里面的元素最终用处在于: Bean 生命周期-填充属性阶段

到这里,整个配置规则加载的源码分析就完成了

总结

该篇文章从源码角度上分析了 Nacos 配置文件如何加载的过程以及常见的一些会出现在细节上忽略的问题,配置文件上加载的优先级:shared-configs、extension-configs、应用配置,最后对配置文件加载的入口以及配置规则解析的源码进行了分析,包括:动态刷新监听器的触发点->NacosPropertySourceRepository#NACOS_PROPERTY_SOURCE_REPOSITORY

更多技术文章可以查看:vnjohn 个人博客

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读