Spring Cloud 2.2.2 源码之二十七nacos客户端获取配置原理二

PropertySourceBootstrapConfiguration

自动配置里有这个类,这个就是来进行远程配置处理的。
在这里插入图片描述
他也是个初始化器,因为在准备好上下文,刷新前applyInitializers方法中进行处理:
在这里插入图片描述
而且他有个注入属性,我们上一篇已经注入了nacosNacosPropertySourceLocator了:
在这里插入图片描述

initialize初始化一

方法太长,做的事又多,先分析一部分,首先就是将属性源定位器propertySourceLocators排序,然后遍历进行定位,放进封装成PropertySource并放进集合里。

@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		List<PropertySource<?>> composite = new ArrayList<>();
		AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
		boolean empty = true;
		ConfigurableEnvironment environment = applicationContext.getEnvironment();
		for (PropertySourceLocator locator : this.propertySourceLocators) {
			Collection<PropertySource<?>> source = locator.locateCollection(environment);
			if (source == null || source.size() == 0) {
				continue;
			}
			List<PropertySource<?>> sourceList = new ArrayList<>();
			for (PropertySource<?> p : source) {
				sourceList.add(new BootstrapPropertySource<>(p));
			}
			logger.info("Located property source: " + sourceList);
			composite.addAll(sourceList);
			empty = false;
		}

NacosPropertySourceLocator的locate

接口默认方法层层深入,到NacosPropertySourceLocatorlocate
在这里插入图片描述
在这里插入图片描述
先准备设置一堆属性,然后进行共享配置和额外配置的加载,默认都是没有的,可以不管,主要是loadApplicationConfiguration

	@Override
	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();//超时30秒
		nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
				timeout);//属性源建造器
		String name = nacosConfigProperties.getName();//dataid的名字

		String dataIdPrefix = nacosConfigProperties.getPrefix();//前缀
		if (StringUtils.isEmpty(dataIdPrefix)) {
			dataIdPrefix = name;
		}

		if (StringUtils.isEmpty(dataIdPrefix)) {//前缀为空的话默认就是spring.application.name
			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;
	}

NacosPropertySourceLocator的loadApplicationConfiguration加载应用程序配置

先获取配置的扩展名和费祖,然后进行3次加载,第一次是默认文件名的配置加载,第二次是默认文件名加后缀,第三次是默认文件名加激活的环境加后缀,也就是我们经常用激活环境的配置文件xxx-dev-yaml这种。加载后的信息都要放入CompositePropertySource符合属性里,到时候要返回出去的。

	private void loadApplicationConfiguration(
			CompositePropertySource compositePropertySource, String dataIdPrefix,
			NacosConfigProperties properties, Environment environment) {
		String fileExtension = properties.getFileExtension();//扩展名
		String nacosGroup = properties.getGroup();//分组,默认DEFAULT_GROUP
		// 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);
		}

	}

具体怎么加载的后面说吧。

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章