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);
		}

	}

具體怎麼加載的後面說吧。

好了,今天就到這裏了,希望對學習理解有幫助,大神看見勿噴,僅爲自己的學習理解,能力有限,請多包涵。

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