Spring Cloud Alibaba Nacos Config 加载配置

1、加载节点

SpringBoot启动时,会执行这个方法:SpringApplication#run,这个方法中会调prepareContext来准备上下文,这个方法中调用了applyInitializers方法来执行实现了ApplicationContextInitializer接口的类的initialize方法。其中包括PropertySourceBootstrapConfiguration#initialize 来加载外部的配置。

@Autowired(required = false)
private List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();

public void initialize(ConfigurableApplicationContext applicationContext) {
   
	...
   for (PropertySourceLocator locator : this.propertySourceLocators) {
   
       //载入PropertySource
      Collection<PropertySource<?>> source = locator.locateCollection(environment);
      ...
   }
   ...
}
//org.springframework.cloud.bootstrap.config.PropertySourceLocator#locateCollection
default Collection<PropertySource<?>> locateCollection(Environment environment) {
   
    return locateCollection(this, environment);
}
//org.springframework.cloud.bootstrap.config.PropertySourceLocator#locateCollection
static Collection<PropertySource<?>> locateCollection(PropertySourceLocator locator, Environment environment) {
   
    //执行locate方法,将PropertySource加载进来
    PropertySource<?> propertySource = locator.locate(environment);
    ...
}

这个类中会注入实现了PropertySourceLocator接口的类,在nacos中是NacosPropertySourceLocator。

在initialize方法中会执行NacosPropertySourceLocator的locate方法,将NacosPropertySource加载进来。

2、NacosPropertySourceLocator的注册

NacosPropertySourceLocator在配置类NacosConfigBootstrapConfiguration中注册。

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
public class NacosConfigBootstrapConfiguration {
   

   @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,NacosPropertySourceLocator。

3、加载

//com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#locate
public PropertySource<?> locate(Environment env) {
   
   nacosConfigProperties.setEnvironment(env);
    //获取ConfigService
   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
   nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
         timeout);
   String name = nacosConfigProperties.getName();
	//获取dataIdPrefix,一次从prefix,name,spring.application.name中获取
   String dataIdPrefix 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值