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.