Nacos源码解析(四)-获取Nacos配置流程分析

spring-cloud-starter-alibaba-nacos-config依赖中,spring.factories文件有一个com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration,定位到NacosConfigBootstrapConfiguration中,看一下创建了哪些Bean
@Bean
@ConditionalOnMissingBean
public NacosConfigProperties nacosConfigProperties() {
   return new NacosConfigProperties();
}

@Bean
@ConditionalOnMissingBean
public NacosConfigManager nacosConfigManager(
      NacosConfigProperties nacosConfigProperties) {
   return new NacosConfigManager(nacosConfigProperties);
}

@Bean
public NacosPropertySourceLocator nacosPropertySourceLocator(
      NacosConfigManager nacosConfigManager) {
   return new NacosPropertySourceLocator(nacosConfigManager);
}

主要初始化了

NacosConfigProperties
NacosConfigManager
NacosPropertSourceLocator
前两个在NacosConfigService创建中已经看到过,这次主要看NacosPropertySourceLocator
根据名字,大致可以看出,主要是Nacos配置资源定位器
进入NacosPropertySourceLocator查看源码,实现了PropertySourceLocator并实现了locate方法,查看一下locate方法并分析其主要作用
public PropertySource<?> locate(Environment env) {
   nacosConfigProperties.setEnvironment(env);
   ConfigService configService = nacosConfigManager.getConfigService();

   if (null == configService) {
      log.warn("no instance of config service found, can't load config from nacos");
      return null;
   }
   long timeout = nacosConfigProperties.getTimeout();
   nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
         timeout);
   String name = nacosConfigProperties.getName();

   String dataIdPrefix = nacosConfigProperties.getPrefix();
   if (StringUtils.isEmpty(dataIdPrefix)) {
      dataIdPrefix = name;
   }

   if (StringUtils.isEmpty(dataIdPrefix)) {
      dataIdPrefix = env.getProperty("spring.application.name");
   }

   CompositePropertySource composite = new CompositePropertySource(
         NACOS_PROPERTY_SOURCE_NAME);

   loadSharedConfiguration(composite);
   loadExtConfiguration(composite);
   loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
   return composite;
}
重点看三个方法:
1.loadSharedConfiguration(composite);
2. loadExtConfiguration(composite);
3. loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
根据方法可以看出:
1. loadSharedConfiguration 加载共享的配置
2. loadExtConfiguration  加载扩展的配置
3. loadApplicationConfiguration 加载应用程序配置
//1.加载共享配置
private void loadSharedConfiguration(
      CompositePropertySource compositePropertySource) {
   List<NacosConfigProperties.Config> sharedConfigs = nacosConfigProperties
         .getSharedConfigs();
   if (!CollectionUtils.isEmpty(sharedConfigs)) {
      checkConfiguration(sharedConfigs, "shared-configs");
      loadNacosConfiguration(compositePropertySource, sharedConfigs);
   }
}

//2.加载扩展配置
private void loadExtConfiguration(CompositePropertySource compositePropertySource) {
   List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
         .getExtensionConfigs();
   if (!CollectionUtils.isEmpty(extConfigs)) {
      checkConfiguration(extConfigs, "extension-configs");
      loadNacosConfiguration(compositePropertySource, extConfigs);
   }
}


//3.加载应用程序配置
private void loadApplicationConfiguration(
      CompositePropertySource compositePropertySource, String dataIdPrefix,
      NacosConfigProperties properties, Environment environment) {
   String fileExtension = properties.getFileExtension();
   String nacosGroup = properties.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值