Nacos Spring Cloud如何加载nacos服务端配置文件

springcloud提供了顶层接口PropertiesSourceLocator,所有基于Springcloud的微服务组件都需要按照springcloud的规范实现,nacos也不列外,nacos的实现是NacosPropertySourceLocator。

 NacosPropertySourceLocator根据springboot的自动转配会被加载到ioc环境中,且在

org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration进行依赖注入时该类也会被填充到propertySourceLocators 字段中。
@Autowired(required = false) 
List<PropertySourceLocator> propertySourceLocators = new ArrayList<>();

接着在类PropertySourceBootstrapConfiguration进行初始化时会遍历propertySourceLocators字段进行locator.locateCollection(environment)调用。

最终会调用到com.alibaba.cloud.nacos.client.NacosPropertySourceLocator#locate方法

最终按顺序加载我们所配置的各种类型的配置文件
 

CompositePropertySource composite = new CompositePropertySource(
      NACOS_PROPERTY_SOURCE_NAME)
##共享类型配置文件
loadSharedConfiguration(composite);
##扩展类型配置文件
loadExtConfiguration(composite);
##应用类型配置文件
loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);

装置配置文件代码如下:

NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,
      fileExtension, isRefreshable);
this.addFirstPropertySource(composite, propertySource, false);


private void addFirstPropertySource(final CompositePropertySource composite,
      NacosPropertySource nacosPropertySource, boolean ignoreEmpty) {
   if (null == nacosPropertySource || null == composite) {
      return;
   }
   if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) {
      return;
   }
   composite.addFirstPropertySource(nacosPropertySource);
}


	public void addFirstPropertySource(PropertySource<?> propertySource) {
		List<PropertySource<?>> existing = new ArrayList<>(this.propertySources);
		this.propertySources.clear();
		this.propertySources.add(propertySource);
		this.propertySources.addAll(existing);
	}

可以通过下面这段代码发现加载完侯会将配置文件放在composite中的第一个元素,当应用程序

加载Environment中的属性值时,当匹配到第一个值时就会返回,即使后面有相同元素的值也不再加载(不了解的可以去看一下@Value的工作机制),由此可见nacos配置文件的优先级应该是

application>ext>share

接下来我们看loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);

是如何加载的通常情况下dataIdPrefix就是我们配置的spring.application.name

private void loadApplicationConfiguration(
			CompositePropertySource compositePropertySource, String dataIdPrefix,
			NacosConfigProperties properties, Environment environment) {
		String fileExtension = properties.getFileExtension();
		String nacosGroup = properties.getGroup();
		// load directly once by default
		loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup,
				fileExtension, true);
		// load with suffix, which have a higher priority than the default
		loadNacosDataIfPresent(compositePropertySource,
				dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
		// Loaded with profile, which have a higher priority than the suffix
		for (String profile : environment.getActiveProfiles()) {
			String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
			loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
					fileExtension, true);
		}

	}

通过代码可以看到按照顺寻从上往下加载的分别是,dataIdPrefix,dataIdPrefix.fileExtension,

dataIdPrefix.profile.fileExtension。所以该文件的优先级为dataIdPrefix.profile.fileExtension>dataIdPrefix.fileExtension>dataIdPrefix,掌握了这些后,我们在读取配置文件时就不会出现明明配置的是这个属性为什么变成其它了,此时您就需要思考一下是不是被别的配置文件覆盖了,别的配置文件优先级是否比你高。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值