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

NacosPropertySourceLocator的loadNacosDataIfPresent

先加载,然后把结果放CompositePropertySource 的最前面,加载不到也没关系。

	private void loadNacosDataIfPresent(final CompositePropertySource composite,
			final String dataId, final String group, String fileExtension,
			boolean isRefreshable) {
		...
		NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,
				fileExtension, isRefreshable);
		this.addFirstPropertySource(composite, propertySource, false);
	}

NacosPropertySourceLocator的loadNacosPropertySource

第一次进来的时候是要去获取的,所以走 nacosPropertySourceBuilder.build

	private NacosPropertySource loadNacosPropertySource(final String dataId,
			final String group, String fileExtension, boolean isRefreshable) {
		if (NacosContextRefresher.getRefreshCount() != 0) {//刷新过了
			if (!isRefreshable) {//不刷新,直接缓存取
				return NacosPropertySourceRepository.getNacosPropertySource(dataId,
						group);
			}
		}
		return nacosPropertySourceBuilder.build(dataId, group, fileExtension,
				isRefreshable);
	}

NacosPropertySourceBuilder的build

先加载数据,然后结果封装成NacosPropertySource,放进缓存。

	NacosPropertySource build(String dataId, String group, String fileExtension,
			boolean isRefreshable) {
		Map<String, Object> p = loadNacosData(dataId, group, fileExtension);
		NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
				p, new Date(), isRefreshable);
		NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);//缓存
		return nacosPropertySource;
	}

loadNacosData

NacosConfigService来加载,加载到了就解析成LinkedHashMap返回,否则就是个空的LinkedHashMap

	private Map<String, Object> loadNacosData(String dataId, String group,
			String fileExtension) {
		String data = null;
		try {
			data = configService.getConfig(dataId, group, timeout);
			...
			Map<String, Object> dataMap = NacosDataParserHandler.getInstance()
					.parseNacosData(data, fileExtension);
			return dataMap == null ? EMPTY_MAP : dataMap;
		}
		return EMPTY_MAP;
	}

NacosConfigService的getConfig一


    @Override
    public String getConfig(String dataId, String group, long timeoutMs) throws NacosException {
        return getConfigInner(namespace, dataId, group, timeoutMs);
    }

首先优先从本地获取,其实就是从本地读取,比如windows的话,就是C:\Users\Administrator\nacos\config\fixed-127.0.0.1_8848_nacos\data下的config-data后者config-data-tenant目录中获取相应配置文件。

private String getConfigInner(String tenant, String dataId, String group, long timeoutMs) throws NacosException {
        group = null2defaultGroup(group);//默认组
        ParamUtils.checkKeyParam(dataId, group);//检查参数
        ConfigResponse cr = new ConfigResponse();//创建响应

        cr.setDataId(dataId);
        cr.setTenant(tenant);
        cr.setGroup(group);

        // 优先使用本地配置
        String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);
        if (content != null) {
            LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}", agent.getName(),
                dataId, group, tenant, ContentUtils.truncateContent(content));
            cr.setContent(content);
            configFilterChainManager.doFilter(null, cr);
            content = cr.getContent();
            return content;
        }

具体我就不跟了,就是文件判断在不在,然后读取。
在这里插入图片描述
在这里插入图片描述

NacosConfigService的getConfig二

如果不本地不存在的话,就从网络读。

 	try {
            String[] ct = worker.getServerConfig(dataId, group, tenant, timeoutMs);
            cr.setContent(ct[0]);

            configFilterChainManager.doFilter(null, cr);
            content = cr.getContent();

            return content;
        } catch (NacosException ioe) {
          ...
        }
ClientWorker的getServerConfig

用代理请求/v1/cs/configs,传参数dataId,group,tenant获取配置文件。

 public String[] getServerConfig(String dataId, String group, String tenant, long readTimeout)
        throws NacosException {
        String[] ct = new String[2];//放文件内容和类型
        if (StringUtils.isBlank(group)) {
            group = Constants.DEFAULT_GROUP;
        }

        HttpResult result = null;
        try {
            List<String> params = null;
            if (StringUtils.isBlank(tenant)) {
                params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group));
            } else {
                params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant));
            }
            result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout);
        } catch (IOException e) {
			。。。
        }

让代理去执行请求下篇说吧。

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

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