In-depth Nacos Config core source code
Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
PropertySourceLocator
The configuration load is then started, returning to the run method of the launch class and calling PropertySourceContext
public configurableApplicationContext run(String... args){
prepareContext(context,environment,listeners,applicationArguments,printedBanner);
}
Copy the code
PrepareContext refreshes the preparation phase of the application context, and then calls applyInitializers
private void prepareContext(ConfigurableApplication context,ConfigurableEnvironment environment,SpringApplicationListeners,ApplicationArguments applicationArguments,Banner printedBanner){
applyInitializers(context);
}
Copy the code
ApplyInitializers method mainly performs ApplicationContextInitializer in container, it is used in the application context initialization time to do a series of operations.
protect void applyInitializers(ConfigurableApplicationContext context){
for(ApplicationContextInitializer initializer:getInitializers()){ Class<? > type = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),ApplicationContextInitializer.class); Assert.isInstanceOf(type,context,"unable to initializer!"); initializer.initialize(context); }}Copy the code
PropertySourceBootstrapConfiguration ApplicationContextInitializer interface, realized in the applyInitializers method call the initialize method, In the last call PropertySourceBootstrapConfiguration the initialize method.
public void initialize(ConfigurableApplicationContext applicationContext){
Iterator iterator = this.propertySourceLocators.iterator();
while(iterator.hasNext()){
PropertySourceLocator propertySourceLocator =(PropertySourceLocator)iterator.next();
PropertySource<?> source= propertySourceLocator.locate(environment);
}
}
Copy the code
PropertySourceLocator interface function is to realize the application of externalized configuration dynamically loaded, and NacosPropertySourceLocator implements this interface, The locator. Locate call is NacosPropertySourceLocator locate method.
Locate in NacosPropertySourceLocator method that is stored in the NacosServer read configuration information, the relevant configuration information endures PropertySource instance and returns.
In-depth Nacos Config core source code
Through the Context in the Spring Cloud Alibaba Nacos Config. GetEnvironment. GetProperty (” Key “) for Nacos Config data on the server, Methods, focus on the implementation of the class is NacosPropertySourceLocator, contain the locate (),
What it does:
- Initialize the ConfigService object, which is a class provided by the Nacos client to access the basic operations that implement the configuration center.
- Load configuration, extension configuration, and configuration corresponding to the application name
publicPropertySource<? > locate(Environment environment){ ConfigService configService=nacosConfigProperties.configServiceInstance(); nacosPropertySourceBuilder=new NacosPropertyBuilder(ConfigService,timeout);
loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite,dataIdPrefix,nacosCOnfigProperties,environment);
}
Copy the code
Then look at loadApplicationConfiguration method deeply
private Properties loadNacosData(String dataId,String group,String fileExtension){
String data = null;
try{
data = configService.getConfig(dataId,group,timeout);
Properties properties=NacosDataParserHandler.getInstance().parseNacosData(data,fileExtension);
return properties == null? EMPTY_PROPERTIES:properties;
}
return EMPTY_PROPERTIES;
}
Copy the code