From 21ff07563d88a12eb9032aa1db3ff59d0e2c1d90 Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Sun, 16 Jun 2019 08:24:17 +0800 Subject: [PATCH] refactor(nacos-client:config): rmove un use construct of CacheData --- .../nacos/api/config/ConfigService.java | 5 +++++ .../nacos/client/config/impl/CacheData.java | 19 +------------------ .../client/config/impl/ClientWorker.java | 7 +++---- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 7da577f1d..87234f160 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -39,6 +39,11 @@ public interface ConfigService { /** * Get config and register Listener * + * If you want to pull it yourself when the program starts to get the configuration for the first time, + * and the registered Listener is used for future configuration updates, you can keep the original + * code unchanged, just add the system parameter: enableRemoteSyncConfig = "true" ( But there is network overhead); + * therefore we recommend that you use this interface directly + * * @param dataId dataId * @param group group * @param timeoutMs read timeout diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index aa05cac49..6e3f289da 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -23,7 +23,6 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.utils.MD5; import com.alibaba.nacos.client.utils.LogUtils; -import com.alibaba.nacos.client.utils.StringUtils; import com.alibaba.nacos.client.utils.TenantUtil; import org.slf4j.Logger; @@ -263,22 +262,6 @@ public class CacheData { this.md5 = getMd5String(content); } - public CacheData(ConfigFilterChainManager configFilterChainManager, String name, String dataId, String group, - String tenant, String content) { - if (null == dataId || null == group) { - throw new IllegalArgumentException("dataId=" + dataId + ", group=" + group); - } - this.name = name; - this.configFilterChainManager = configFilterChainManager; - this.dataId = dataId; - this.group = group; - this.tenant = tenant; - listeners = new CopyOnWriteArrayList(); - this.isInitializing = true; - this.content = StringUtils.isEmpty(content) ? loadCacheContentFromDiskLocal(name, dataId, group, tenant) : content; - this.md5 = getMd5String(content); - } - // ================== private final String name; @@ -304,7 +287,7 @@ public class CacheData { class ManagerListenerWrap { final Listener listener; - volatile String lastCallMd5 = CacheData.getMd5String(null); + String lastCallMd5 = CacheData.getMd5String(null); ManagerListenerWrap(Listener listener) { this.listener = listener; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 221b0d2ce..97e6afe3b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -188,15 +188,14 @@ public class ClientWorker { cache.setInitializing(true); } else { cache = new CacheData(configFilterChainManager, agent.getName(), dataId, group, tenant); - String content; // fix issue # 1317 if (enableRemoteSyncConfig) { try { - content = getServerConfig(dataId, group, tenant, 3000L); + String content = getServerConfig(dataId, group, tenant, 3000L); + cache.setContent(content); } catch (NacosException ignore) { - content = null; + // If the remote pull fails, the local snapshot should not be overwritten } - cache.setContent(content); } }