即如果我的共享配置指定为application.yml扩展配置为redis.yml,application.name是app1,active是dev的话加载顺序如下
这里注意优先级,优先级是跟加载顺序相反,即application.name+active+file-extension的优先级最高
入口文件在NacosPropertySourceLocator,其中加载的顺序代码如下
this.loadSharedConfiguration(composite);
this.loadExtConfiguration(composite);
this.loadApplicationConfiguration(composite, dataIdPrefix, this.nacosConfigProperties, env);
this.loadSharedConfiguration这里就是先加载的共享配置
this.loadExtConfiguration加载扩展配置
this.loadApplicationConfiguration加载该项目独有的配置,在这个方法里面有分别尝试加载
private void loadApplicationConfiguration(CompositePropertySource compositePropertySource, String dataIdPrefix, NacosConfigProperties properties, Environment environment) {
String fileExtension = properties.getFileExtension();
String nacosGroup = properties.getGroup();
// dataIdPrefix+fileExtension 即 app.yml
this.loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup, fileExtension, true);
// dataIdPrefix+ "." + fileExtension + fileExtension 即 app.yml.yml
this.loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + "." + fileExtension, nacosGroup, fileExtension, true);
String[] var7 = environment.getActiveProfiles();
int var8 = var7.length;
for(int var9 = 0; var9 < var8; ++var9) {
String profile = var7[var9];
String dataId = dataIdPrefix + "-" + profile + "." + fileExtension;
// ddataIdPrefix + "-" + profile + "." + fileExtension 即 app-dev.yml
this.loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true);
}
}
nacos加载按顺序加载完所有配置文件后,根据顺序将list返回
加载配置文件的代码是在PropertySourceBootstrapConfiguration的initialize中
public void initialize(ConfigurableApplicationContext applicationContext) {
List<PropertySource<?>> composite = new ArrayList();
AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
boolean empty = true;
ConfigurableEnvironment environment = applicationContext.getEnvironment();
Iterator var5 = this.propertySourceLocators.iterator();
while(true) {
Collection source;
do {
do {
if (!var5.hasNext()) {
if (!empty) {
MutablePropertySources propertySources = environment.getPropertySources();
String logConfig = environment.resolvePlaceholders("${logging.config:}");
LogFile logFile = LogFile.get(environment);
Iterator var15 = environment.getPropertySources().iterator();
while(var15.hasNext()) {
PropertySource<?> p = (PropertySource)var15.next();
if (p.getName().startsWith("bootstrapProperties")) {
propertySources.remove(p.getName());
}
}
//当所有配置文件加载完成后就会进入到这个方法里面将配置参数加载到内存里
this.insertPropertySources(propertySources, composite);
this.reinitializeLoggingSystem(environment, logConfig, logFile);
this.setLogLevels(applicationContext, environment);
this.handleIncludedProfiles(environment);
}
return;
}
PropertySourceLocator locator = (PropertySourceLocator)var5.next();
// 调用所有实现PropertySourceLocator的方法加载配置文件(刚才的入口类就实现了他)
source = locator.locateCollection(environment);
} while(source == null);
} while(source.size() == 0);
List<PropertySource<?>> sourceList = new ArrayList();
Iterator var9 = source.iterator();
while(var9.hasNext()) {
PropertySource<?> p = (PropertySource)var9.next();
if (p instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource)p;
sourceList.add(new BootstrapPropertySource(enumerable));
} else {
sourceList.add(new SimpleBootstrapPropertySource(p));
}
}
logger.info("Located property source: " + sourceList);
composite.addAll(sourceList);
empty = false;
}
}
这里需要重点关注一下,这个就是优先级的原理
private void insertPropertySources(MutablePropertySources propertySources, List<PropertySource<?>> composite) {
MutablePropertySources incoming = new MutablePropertySources();
List<PropertySource<?>> reversedComposite = new ArrayList(composite);
// 重点在这里,当所有配置文件加载完读取之前,spring对配置文件的list做了反转,所以后加载的优先度最高
Collections.reverse(reversedComposite);
Iterator var5 = reversedComposite.iterator();
while(var5.hasNext()) {
PropertySource<?> p = (PropertySource)var5.next();
// addFirst这个方法,是加之前先判断如果已经有的话不再加载
incoming.addFirst(p);
}
PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
Binder.get(this.environment(incoming)).bind("spring.cloud.config", Bindable.ofInstance(remoteProperties));
PropertySource p;
Iterator var9;
if (remoteProperties.isAllowOverride() && (remoteProperties.isOverrideNone() || !remoteProperties.isOverrideSystemProperties())) {
if (remoteProperties.isOverrideNone()) {
var9 = composite.iterator();
while(var9.hasNext()) {
p = (PropertySource)var9.next();
propertySources.addLast(p);
}
} else {
if (propertySources.contains("systemEnvironment")) {
if (!remoteProperties.isOverrideSystemProperties()) {
var9 = reversedComposite.iterator();
while(var9.hasNext()) {
p = (PropertySource)var9.next();
propertySources.addAfter("systemEnvironment", p);
}
} else {
var9 = composite.iterator();
while(var9.hasNext()) {
p = (PropertySource)var9.next();
propertySources.addBefore("systemEnvironment", p);
}
}
} else {
var9 = composite.iterator();
while(var9.hasNext()) {
p = (PropertySource)var9.next();
propertySources.addLast(p);
}
}
}
} else {
var9 = reversedComposite.iterator();
while(var9.hasNext()) {
p = (PropertySource)var9.next();
propertySources.addFirst(p);
}
}
}