diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index b196f300f32..f8a351d3985 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -32,6 +32,7 @@ import com.alibaba.nacos.client.config.impl.ServerListManager; import com.alibaba.nacos.client.config.utils.ContentUtils; import com.alibaba.nacos.client.config.utils.ParamUtils; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.client.utils.ValidatorUtils; @@ -71,22 +72,23 @@ public class NacosConfigService implements ConfigService { private final ConfigFilterChainManager configFilterChainManager; public NacosConfigService(Properties properties) throws NacosException { - ValidatorUtils.checkInitParam(properties); + final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + ValidatorUtils.checkInitParam(clientProperties); - initNamespace(properties); - this.configFilterChainManager = new ConfigFilterChainManager(properties); - ServerListManager serverListManager = new ServerListManager(properties); + initNamespace(clientProperties); + this.configFilterChainManager = new ConfigFilterChainManager(clientProperties.asProperties()); + ServerListManager serverListManager = new ServerListManager(clientProperties); serverListManager.start(); - this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, properties); + this.worker = new ClientWorker(this.configFilterChainManager, serverListManager, clientProperties); // will be deleted in 2.0 later versions agent = new ServerHttpAgent(serverListManager); } - private void initNamespace(Properties properties) { + private void initNamespace(NacosClientProperties properties) { namespace = ParamUtil.parseNamespace(properties); - properties.put(PropertyKeyConst.NAMESPACE, namespace); + properties.setProperty(PropertyKeyConst.NAMESPACE, namespace); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index b0312ccb2e4..9851935e30a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.ConfigHttpClientManager; import com.alibaba.nacos.client.config.impl.ServerListManager; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.ContextPathUtil; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; @@ -242,7 +243,7 @@ public ServerHttpAgent(ServerListManager mgr, Properties properties) { } public ServerHttpAgent(Properties properties) throws NacosException { - this.serverListMgr = new ServerListManager(properties); + this.serverListMgr = new ServerListManager(NacosClientProperties.PROTOTYPE.derive(properties)); } @Override 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 7159566643c..4702934b80b 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 @@ -25,6 +25,7 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.TenantUtil; import com.alibaba.nacos.common.utils.MD5Utils; @@ -62,7 +63,7 @@ public class CacheData { static boolean initSnapshot; static { - initSnapshot = Boolean.valueOf(System.getProperty("nacos.cache.data.init.snapshot", "true")); + initSnapshot = NacosClientProperties.PROTOTYPE.getBoolean("nacos.cache.data.init.snapshot", true); LOGGER.info("nacos.cache.data.init.snapshot = {} ", initSnapshot); } 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 6367c94a5ef..7011b4e4545 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 @@ -37,6 +37,7 @@ import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.api.remote.response.Response; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.plugin.auth.api.RequestResource; import com.alibaba.nacos.client.config.common.GroupKey; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; @@ -75,7 +76,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.Set; import java.util.UUID; import java.util.concurrent.ArrayBlockingQueue; @@ -410,7 +410,7 @@ private String blank2defaultGroup(String group) { @SuppressWarnings("PMD.ThreadPoolCreationRule") public ClientWorker(final ConfigFilterChainManager configFilterChainManager, ServerListManager serverListManager, - final Properties properties) throws NacosException { + final NacosClientProperties properties) throws NacosException { this.configFilterChainManager = configFilterChainManager; init(properties); @@ -457,7 +457,7 @@ private void refreshContentAndCheck(CacheData cacheData, boolean notify) { } } - private void init(Properties properties) { + private void init(NacosClientProperties properties) { timeout = Math.max(ConvertUtils.toInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT), Constants.CONFIG_LONG_POLL_TIMEOUT), Constants.MIN_CONFIG_LONG_POLL_TIMEOUT); @@ -540,7 +540,7 @@ public class ConfigRpcTransportClient extends ConfigTransportClient { */ private static final long ALL_SYNC_INTERNAL = 5 * 60 * 1000L; - public ConfigRpcTransportClient(Properties properties, ServerListManager serverListManager) { + public ConfigRpcTransportClient(NacosClientProperties properties, ServerListManager serverListManager) { super(properties, serverListManager); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java index dcb0ca0aa95..596c997846b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.plugin.auth.api.RequestResource; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.security.SecurityProxy; @@ -66,7 +67,7 @@ public void shutdown() throws NacosException { securityProxy.shutdown(); } - public ConfigTransportClient(Properties properties, ServerListManager serverListManager) { + public ConfigTransportClient(NacosClientProperties properties, ServerListManager serverListManager) { String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE); if (StringUtils.isBlank(encodeTmp)) { @@ -77,7 +78,7 @@ public ConfigTransportClient(Properties properties, ServerListManager serverList this.tenant = properties.getProperty(PropertyKeyConst.NAMESPACE); this.serverListManager = serverListManager; - this.properties = properties; + this.properties = properties.asProperties(); this.securityProxy = new SecurityProxy(serverListManager.getServerUrls(), ConfigHttpClientManager.getInstance().getNacosRestTemplate()); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java index a05c2ec1b90..f5df497a8a7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.config.impl; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.common.cache.Cache; import com.alibaba.nacos.common.cache.builder.CacheBuilder; @@ -56,7 +57,7 @@ public class Limiter { static { try { - String limitTimeStr = System.getProperty(LIMIT_TIME_PROPERTY, String.valueOf(limit)); + String limitTimeStr = NacosClientProperties.PROTOTYPE.getProperty(LIMIT_TIME_PROPERTY, String.valueOf(limit)); limit = Double.parseDouble(limitTimeStr); LOGGER.info("limitTime:{}", limit); } catch (Exception e) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java index cbe93557784..fd7258e66e7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.client.config.utils.ConcurrentDiskUtil; import com.alibaba.nacos.client.config.utils.JvmUtil; import com.alibaba.nacos.client.config.utils.SnapShotSwitch; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.common.utils.StringUtils; @@ -58,12 +59,12 @@ public class LocalConfigInfoProcessor { private static final String SNAPSHOT_FILE_CHILD_2 = "snapshot-tenant"; static { - LOCAL_FILEROOT_PATH = - System.getProperty("JM.LOG.PATH", System.getProperty("user.home")) + File.separator + "nacos" - + File.separator + "config"; - LOCAL_SNAPSHOT_PATH = - System.getProperty("JM.SNAPSHOT.PATH", System.getProperty("user.home")) + File.separator + "nacos" - + File.separator + "config"; + LOCAL_FILEROOT_PATH = NacosClientProperties.PROTOTYPE.getProperty("JM.LOG.PATH", + NacosClientProperties.PROTOTYPE.getProperty("user.home")) + File.separator + "nacos" + File.separator + + "config"; + LOCAL_SNAPSHOT_PATH = NacosClientProperties.PROTOTYPE.getProperty("JM.SNAPSHOT.PATH", + NacosClientProperties.PROTOTYPE.getProperty("user.home")) + File.separator + "nacos" + File.separator + + "config"; LOGGER.info("LOCAL_SNAPSHOT_PATH:{}", LOCAL_SNAPSHOT_PATH); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index b306a85e059..4977a9e3f16 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.ContextPathUtil; import com.alibaba.nacos.client.utils.EnvUtil; import com.alibaba.nacos.client.utils.LogUtils; @@ -165,7 +166,8 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio this.isStarted = false; Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); - this.endpoint = initEndpoint(properties); + final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + this.endpoint = initEndpoint(clientProperties); if (StringUtils.isBlank(endpoint)) { throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "endpoint is blank"); @@ -176,10 +178,10 @@ public ServerListManager(String endpoint, String namespace) throws NacosExceptio } this.name = initServerName(null); - initAddressServerUrl(properties); + initAddressServerUrl(clientProperties); } - public ServerListManager(Properties properties) throws NacosException { + public ServerListManager(NacosClientProperties properties) throws NacosException { this.isStarted = false; this.serverAddrsStr = properties.getProperty(PropertyKeyConst.SERVER_ADDR); String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); @@ -222,11 +224,11 @@ public ServerListManager(Properties properties) throws NacosException { } - private String initServerName(Properties properties) { + private String initServerName(NacosClientProperties properties) { String serverName; //1.user define server name. if (properties != null && properties.containsKey(PropertyKeyConst.SERVER_NAME)) { - serverName = properties.get(PropertyKeyConst.SERVER_NAME).toString(); + serverName = properties.getProperty(PropertyKeyConst.SERVER_NAME); } else { // if fix url,use fix url join string. if (isFixed) { @@ -245,7 +247,7 @@ private String initServerName(Properties properties) { return serverName; } - private void initAddressServerUrl(Properties properties) { + private void initAddressServerUrl(NacosClientProperties properties) { if (isFixed) { return; } @@ -259,7 +261,7 @@ private void initAddressServerUrl(Properties properties) { } if (properties != null && properties.containsKey(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)) { addressServerUrlTem - .append(hasQueryString ? "&" : "?" + properties.get(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)); + .append(hasQueryString ? "&" : "?" + properties.getProperty(PropertyKeyConst.ENDPOINT_QUERY_PARAMS)); } @@ -267,7 +269,7 @@ private void initAddressServerUrl(Properties properties) { LOGGER.info("serverName = {}, address server url = {}", this.name, this.addressServerUrl); } - private void initParam(Properties properties) { + private void initParam(NacosClientProperties properties) { this.endpoint = initEndpoint(properties); String contentPathTmp = properties.getProperty(PropertyKeyConst.CONTEXT_PATH); @@ -280,10 +282,10 @@ private void initParam(Properties properties) { } } - private String initEndpoint(final Properties properties) { + private String initEndpoint(final NacosClientProperties properties) { String endpointPortTmp = TemplateUtils - .stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), + .stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), () -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT)); if (StringUtils.isNotBlank(endpointPortTmp)) { @@ -294,7 +296,7 @@ private String initEndpoint(final Properties properties) { // Whether to enable domain name resolution rules String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, - System.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, + properties.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE))); if (Boolean.parseBoolean(isUseEndpointRuleParsing)) { String endpointUrl = ParamUtil.parsingEndpointRule(endpointTmp); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/utils/JvmUtil.java b/client/src/main/java/com/alibaba/nacos/client/config/utils/JvmUtil.java index 5c44f2452a0..c0d612f327f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/utils/JvmUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/utils/JvmUtil.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.client.config.utils; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.LogUtils; import org.slf4j.Logger; @@ -46,7 +47,7 @@ public static Boolean isMultiInstance() { private static final String DEFAULT_IS_MULTI_INSTANCE = "false"; static { - String multiDeploy = System.getProperty(IS_MULTI_INSTANCE_PROPERTY, DEFAULT_IS_MULTI_INSTANCE); + String multiDeploy = NacosClientProperties.PROTOTYPE.getProperty(IS_MULTI_INSTANCE_PROPERTY, DEFAULT_IS_MULTI_INSTANCE); if (TRUE.equals(multiDeploy)) { isMultiInstance = true; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java index 3035e0f4bab..10b98701088 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingMaintainService.java @@ -25,6 +25,7 @@ import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.core.ServerListManager; import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager; import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy; @@ -72,15 +73,16 @@ public NacosNamingMaintainService(Properties properties) throws NacosException { } private void init(Properties properties) throws NacosException { - ValidatorUtils.checkInitParam(properties); - namespace = InitUtils.initNamespaceForNaming(properties); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + ValidatorUtils.checkInitParam(nacosClientProperties); + namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); InitUtils.initSerialization(); - InitUtils.initWebRootContext(properties); - serverListManager = new ServerListManager(properties, namespace); + InitUtils.initWebRootContext(nacosClientProperties); + serverListManager = new ServerListManager(nacosClientProperties, namespace); securityProxy = new SecurityProxy(serverListManager.getServerList(), NamingHttpClientManager.getInstance().getNacosRestTemplate()); initSecurityProxy(properties); - serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, properties); + serverProxy = new NamingHttpClientProxy(namespace, securityProxy, serverListManager, nacosClientProperties); } private void initSecurityProxy(Properties properties) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java index bed4737e1cc..c67907e98c4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java @@ -26,6 +26,7 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.core.Balancer; import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; @@ -85,25 +86,27 @@ public NacosNamingService(Properties properties) throws NacosException { } private void init(Properties properties) throws NacosException { - ValidatorUtils.checkInitParam(properties); - this.namespace = InitUtils.initNamespaceForNaming(properties); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + + ValidatorUtils.checkInitParam(nacosClientProperties); + this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties); InitUtils.initSerialization(); - InitUtils.initWebRootContext(properties); - initLogName(properties); + InitUtils.initWebRootContext(nacosClientProperties); + initLogName(nacosClientProperties); this.notifierEventScope = UUID.randomUUID().toString(); this.changeNotifier = new InstancesChangeNotifier(this.notifierEventScope); NotifyCenter.registerToPublisher(InstancesChangeEvent.class, 16384); NotifyCenter.registerSubscriber(changeNotifier); - this.serviceInfoHolder = new ServiceInfoHolder(namespace, this.notifierEventScope, properties); - this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, properties, changeNotifier); + this.serviceInfoHolder = new ServiceInfoHolder(namespace, this.notifierEventScope, nacosClientProperties); + this.clientProxy = new NamingClientProxyDelegate(this.namespace, serviceInfoHolder, nacosClientProperties, changeNotifier); } - private void initLogName(Properties properties) { - logName = System.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); + private void initLogName(NacosClientProperties properties) { + logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); if (StringUtils.isEmpty(logName)) { - if (properties != null && StringUtils + if (StringUtils .isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) { logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); } else { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java index b8be26b140a..3a4d6311832 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolder.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.backups.FailoverReactor; import com.alibaba.nacos.client.naming.event.InstancesChangeEvent; @@ -36,7 +37,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -68,7 +68,7 @@ public class ServiceInfoHolder implements Closeable { private String notifierEventScope; - public ServiceInfoHolder(String namespace, String notifierEventScope, Properties properties) { + public ServiceInfoHolder(String namespace, String notifierEventScope, NacosClientProperties properties) { initCacheDir(namespace, properties); if (isLoadCacheAtStart(properties)) { this.serviceInfoMap = new ConcurrentHashMap<>(DiskCache.read(this.cacheDir)); @@ -80,8 +80,8 @@ public ServiceInfoHolder(String namespace, String notifierEventScope, Properties this.notifierEventScope = notifierEventScope; } - private void initCacheDir(String namespace, Properties properties) { - String jmSnapshotPath = System.getProperty(JM_SNAPSHOT_PATH_PROPERTY); + private void initCacheDir(String namespace, NacosClientProperties properties) { + String jmSnapshotPath = properties.getProperty(JM_SNAPSHOT_PATH_PROPERTY); String namingCacheRegistryDir = ""; if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) { @@ -92,12 +92,12 @@ private void initCacheDir(String namespace, Properties properties) { cacheDir = jmSnapshotPath + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + File.separator + FILE_PATH_NAMING + File.separator + namespace; } else { - cacheDir = System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + cacheDir = properties.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir + File.separator + FILE_PATH_NAMING + File.separator + namespace; } } - private boolean isLoadCacheAtStart(Properties properties) { + private boolean isLoadCacheAtStart(NacosClientProperties properties) { boolean loadCacheAtStart = false; if (properties != null && StringUtils .isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_LOAD_CACHE_AT_START))) { @@ -107,7 +107,7 @@ private boolean isLoadCacheAtStart(Properties properties) { return loadCacheAtStart; } - private boolean isPushEmptyProtect(Properties properties) { + private boolean isPushEmptyProtect(NacosClientProperties properties) { boolean pushEmptyProtection = false; if (properties != null && StringUtils .isNotEmpty(properties.getProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION))) { diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java index 362f1f4c1c5..f130466189f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServerListManager.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.runtime.NacosLoadException; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; import com.alibaba.nacos.client.naming.remote.http.NamingHttpClientManager; import com.alibaba.nacos.client.naming.utils.CollectionUtils; @@ -79,10 +80,10 @@ public class ServerListManager implements ServerListFactory, Closeable { private long lastServerListRefreshTime = 0L; public ServerListManager(Properties properties) { - this(properties, null); + this(NacosClientProperties.PROTOTYPE.derive(properties), null); } - public ServerListManager(Properties properties, String namespace) { + public ServerListManager(NacosClientProperties properties, String namespace) { this.namespace = namespace; initServerAddr(properties); if (!serverList.isEmpty()) { @@ -93,7 +94,7 @@ public ServerListManager(Properties properties, String namespace) { } } - private void initServerAddr(Properties properties) { + private void initServerAddr(NacosClientProperties properties) { this.endpoint = InitUtils.initEndpoint(properties); if (StringUtils.isNotEmpty(endpoint)) { this.serversFromEndpoint = getServerListFromEndpoint(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java index 7f3c3f4bc52..9086552abd8 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateService.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.InstancesChangeNotifier; import com.alibaba.nacos.client.naming.remote.NamingClientProxy; @@ -32,7 +33,6 @@ import java.util.HashMap; import java.util.Map; -import java.util.Properties; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; @@ -63,7 +63,7 @@ public class ServiceInfoUpdateService implements Closeable { private final boolean asyncQuerySubscribeService; - public ServiceInfoUpdateService(Properties properties, ServiceInfoHolder serviceInfoHolder, + public ServiceInfoUpdateService(NacosClientProperties properties, ServiceInfoHolder serviceInfoHolder, NamingClientProxy namingClientProxy, InstancesChangeNotifier changeNotifier) { this.asyncQuerySubscribeService = isAsyncQueryForSubscribeService(properties); this.executor = new ScheduledThreadPoolExecutor(initPollingThreadCount(properties), @@ -73,14 +73,14 @@ public ServiceInfoUpdateService(Properties properties, ServiceInfoHolder service this.changeNotifier = changeNotifier; } - private boolean isAsyncQueryForSubscribeService(Properties properties) { + private boolean isAsyncQueryForSubscribeService(NacosClientProperties properties) { if (properties == null || !properties.containsKey(PropertyKeyConst.NAMING_ASYNC_QUERY_SUBSCRIBE_SERVICE)) { - return true; + return false; } return ConvertUtils.toBoolean(properties.getProperty(PropertyKeyConst.NAMING_ASYNC_QUERY_SUBSCRIBE_SERVICE), false); } - private int initPollingThreadCount(Properties properties) { + private int initPollingThreadCount(NacosClientProperties properties) { if (properties == null) { return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT; } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegate.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegate.java index a092886c0d8..33013f842ed 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegate.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegate.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.selector.AbstractSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.core.ServerListManager; import com.alibaba.nacos.client.naming.core.ServiceInfoUpdateService; @@ -64,7 +65,7 @@ public class NamingClientProxyDelegate implements NamingClientProxy { private ScheduledExecutorService executorService; - public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfoHolder, Properties properties, + public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfoHolder, NacosClientProperties properties, InstancesChangeNotifier changeNotifier) throws NacosException { this.serviceInfoUpdateService = new ServiceInfoUpdateService(properties, serviceInfoHolder, this, changeNotifier); @@ -78,16 +79,17 @@ public NamingClientProxyDelegate(String namespace, ServiceInfoHolder serviceInfo serviceInfoHolder); } - private void initSecurityProxy(Properties properties) { + private void initSecurityProxy(NacosClientProperties properties) { this.executorService = new ScheduledThreadPoolExecutor(1, r -> { Thread t = new Thread(r); t.setName("com.alibaba.nacos.client.naming.security"); t.setDaemon(true); return t; }); - this.securityProxy.login(properties); + final Properties nacosClientPropertiesView = properties.asProperties(); + this.securityProxy.login(nacosClientPropertiesView); this.executorService - .scheduleWithFixedDelay(() -> securityProxy.login(properties), 0, SECURITY_INFO_REFRESH_INTERVAL_MILLS, + .scheduleWithFixedDelay(() -> securityProxy.login(nacosClientPropertiesView), 0, SECURITY_INFO_REFRESH_INTERVAL_MILLS, TimeUnit.MILLISECONDS); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java index 802b89cadb5..8b2536bb6bf 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxy.java @@ -39,6 +39,7 @@ import com.alibaba.nacos.api.remote.response.ResponseCode; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.SelectorType; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; import com.alibaba.nacos.client.naming.remote.AbstractNamingClientProxy; @@ -59,7 +60,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; @@ -84,7 +84,7 @@ public class NamingGrpcClientProxy extends AbstractNamingClientProxy { private final NamingGrpcRedoService redoService; public NamingGrpcClientProxy(String namespaceId, SecurityProxy securityProxy, ServerListFactory serverListFactory, - Properties properties, ServiceInfoHolder serviceInfoHolder) throws NacosException { + NacosClientProperties properties, ServiceInfoHolder serviceInfoHolder) throws NacosException { super(securityProxy); this.namespaceId = namespaceId; this.uuid = UUID.randomUUID().toString(); diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java index e6ef24d5660..4bf89e7a32a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxy.java @@ -29,6 +29,7 @@ import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.SelectorType; import com.alibaba.nacos.api.utils.NetUtils; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.naming.core.ServerListManager; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; @@ -55,7 +56,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Properties; import java.util.Random; import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER; @@ -110,7 +110,7 @@ public class NamingHttpClientProxy extends AbstractNamingClientProxy { private int serverPort = DEFAULT_SERVER_PORT; public NamingHttpClientProxy(String namespaceId, SecurityProxy securityProxy, ServerListManager serverListManager, - Properties properties) { + NacosClientProperties properties) { super(securityProxy); this.serverListManager = serverListManager; this.setServerPort(DEFAULT_SERVER_PORT); @@ -472,7 +472,7 @@ public String getNamespaceId() { public void setServerPort(int serverPort) { this.serverPort = serverPort; - String sp = System.getProperty(SystemPropertyKeyConst.NAMING_SERVER_PORT); + String sp = NacosClientProperties.PROTOTYPE.getProperty(SystemPropertyKeyConst.NAMING_SERVER_PORT); if (StringUtils.isNotBlank(sp)) { this.serverPort = Integer.parseInt(sp); } diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java index e147a2eddee..33916f3177f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/utils/InitUtils.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; import com.alibaba.nacos.api.selector.SelectorType; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.utils.ContextPathUtil; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; @@ -30,8 +31,6 @@ import com.alibaba.nacos.common.utils.JacksonUtils; import com.alibaba.nacos.common.utils.StringUtils; -import java.util.Properties; - /** * Init utils. * @@ -49,11 +48,11 @@ public class InitUtils { * @param properties properties * @return namespace */ - public static String initNamespaceForNaming(Properties properties) { + public static String initNamespaceForNaming(NacosClientProperties properties) { String tmpNamespace = null; String isUseCloudNamespaceParsing = properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, - System.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, + properties.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING))); if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) { @@ -62,14 +61,14 @@ public static String initNamespaceForNaming(Properties properties) { LogUtils.NAMING_LOGGER.info("initializer namespace from System Property : {}", tmpNamespace); tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> { - String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); + String namespace = properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace); return namespace; }); } tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> { - String namespace = System.getProperty(PropertyKeyConst.NAMESPACE); + String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace); return namespace; }); @@ -88,7 +87,7 @@ public static String initNamespaceForNaming(Properties properties) { * @param properties properties * @since 1.4.1 */ - public static void initWebRootContext(Properties properties) { + public static void initWebRootContext(NacosClientProperties properties) { final String webContext = properties.getProperty(PropertyKeyConst.CONTEXT_PATH); TemplateUtils.stringNotEmptyAndThenExecute(webContext, () -> { UtilAndComs.webContext = ContextPathUtil.normalizeContextPath(webContext); @@ -103,14 +102,14 @@ public static void initWebRootContext(Properties properties) { * @param properties properties * @return end point */ - public static String initEndpoint(final Properties properties) { + public static String initEndpoint(final NacosClientProperties properties) { if (properties == null) { return ""; } // Whether to enable domain name resolution rules String isUseEndpointRuleParsing = properties.getProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, - System.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, + properties.getProperty(SystemPropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, String.valueOf(ParamUtil.USE_ENDPOINT_PARSING_RULE_DEFAULT_VALUE))); boolean isUseEndpointParsingRule = Boolean.parseBoolean(isUseEndpointRuleParsing); @@ -130,7 +129,7 @@ public static String initEndpoint(final Properties properties) { } String endpointPort = TemplateUtils - .stringEmptyAndThenExecute(System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), + .stringEmptyAndThenExecute(properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_PORT), () -> properties.getProperty(PropertyKeyConst.ENDPOINT_PORT)); endpointPort = TemplateUtils.stringEmptyAndThenExecute(endpointPort, () -> DEFAULT_END_POINT_PORT); diff --git a/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java b/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java index 89f8c2e70b9..5c48293fb2f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java +++ b/client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java @@ -24,7 +24,6 @@ import com.alibaba.nacos.common.utils.VersionUtils; import org.slf4j.Logger; -import java.util.Properties; import java.util.regex.Pattern; /** @@ -175,18 +174,18 @@ public static void setDefaultNodesPath(String defaultNodesPath) { * @param properties properties * @return namespace */ - public static String parseNamespace(Properties properties) { + public static String parseNamespace(NacosClientProperties properties) { String namespaceTmp = null; String isUseCloudNamespaceParsing = properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, - NacosClientProperties.PROTOTYPE.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, + properties.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING))); if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) { namespaceTmp = TenantUtil.getUserTenantForAcm(); namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, () -> { - String namespace = NacosClientProperties.PROTOTYPE.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); + String namespace = properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); return StringUtils.isNotBlank(namespace) ? namespace : StringUtils.EMPTY; }); } diff --git a/client/src/main/java/com/alibaba/nacos/client/utils/ValidatorUtils.java b/client/src/main/java/com/alibaba/nacos/client/utils/ValidatorUtils.java index 997e92f87da..e1a0c08207f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/utils/ValidatorUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/utils/ValidatorUtils.java @@ -18,8 +18,8 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; -import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -32,7 +32,7 @@ public final class ValidatorUtils { private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+"); - public static void checkInitParam(Properties properties) throws NacosException { + public static void checkInitParam(NacosClientProperties properties) throws NacosException { checkContextPath(properties.getProperty(PropertyKeyConst.CONTEXT_PATH)); } diff --git a/client/src/test/java/com/alibaba/nacos/client/config/NacosConfigServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/config/NacosConfigServiceTest.java index 2e7fce068cd..36e565ab84c 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/NacosConfigServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/NacosConfigServiceTest.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.client.config.impl.ConfigTransportClient; import com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor; import com.alibaba.nacos.client.config.impl.ServerListManager; +import com.alibaba.nacos.client.env.NacosClientProperties; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -101,7 +102,8 @@ public void receiveConfigInfo(String configInfo) { response.setContent(content); response.setConfigType("bb"); Mockito.when(mockWoker.getServerConfig(dataId, group, "", timeout, false)).thenReturn(response); - Mockito.when(mockWoker.getAgent()).thenReturn(new ConfigTransportClient(new Properties(), new ServerListManager()) { + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(new Properties()); + Mockito.when(mockWoker.getAgent()).thenReturn(new ConfigTransportClient(properties, new ServerListManager()) { @Override public void startInternal() throws NacosException { // NOOP diff --git a/client/src/test/java/com/alibaba/nacos/client/config/impl/ClientWorkerTest.java b/client/src/test/java/com/alibaba/nacos/client/config/impl/ClientWorkerTest.java index 21545fc1994..aa6831e99c4 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/impl/ClientWorkerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/impl/ClientWorkerTest.java @@ -20,6 +20,7 @@ import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; +import com.alibaba.nacos.client.env.NacosClientProperties; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -36,7 +37,9 @@ public void testConstruct() throws NacosException { Properties prop = new Properties(); ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties()); ServerListManager agent = Mockito.mock(ServerListManager.class); - ClientWorker clientWorker = new ClientWorker(filter, agent, prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ClientWorker clientWorker = new ClientWorker(filter, agent, nacosClientProperties); Assert.assertNotNull(clientWorker); } @@ -45,7 +48,9 @@ public void testAddListenerWithoutTenant() throws NacosException { Properties prop = new Properties(); ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties()); ServerListManager agent = Mockito.mock(ServerListManager.class); - ClientWorker clientWorker = new ClientWorker(filter, agent, prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ClientWorker clientWorker = new ClientWorker(filter, agent, nacosClientProperties); String dataId = "a"; String group = "b"; @@ -73,7 +78,9 @@ public void testListenerWithTenant() throws NacosException { Properties prop = new Properties(); ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties()); ServerListManager agent = Mockito.mock(ServerListManager.class); - ClientWorker clientWorker = new ClientWorker(filter, agent, prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ClientWorker clientWorker = new ClientWorker(filter, agent, nacosClientProperties); Listener listener = new AbstractListener() { @Override @@ -117,7 +124,9 @@ public void testPublishConfig() throws NacosException { Properties prop = new Properties(); ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties()); ServerListManager agent = Mockito.mock(ServerListManager.class); - ClientWorker clientWorker = new ClientWorker(filter, agent, prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ClientWorker clientWorker = new ClientWorker(filter, agent, nacosClientProperties); ClientWorker.ConfigRpcTransportClient mockClient = Mockito.mock(ClientWorker.ConfigRpcTransportClient.class); String dataId = "a"; @@ -158,7 +167,9 @@ public void testShutdown() throws NacosException, NoSuchFieldException, IllegalA Properties prop = new Properties(); ConfigFilterChainManager filter = new ConfigFilterChainManager(new Properties()); ServerListManager agent = Mockito.mock(ServerListManager.class); - ClientWorker clientWorker = new ClientWorker(filter, agent, prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ClientWorker clientWorker = new ClientWorker(filter, agent, nacosClientProperties); String dataId = "a"; String group = "b"; String tenant = "c"; diff --git a/client/src/test/java/com/alibaba/nacos/client/config/impl/ServerListManagerTest.java b/client/src/test/java/com/alibaba/nacos/client/config/impl/ServerListManagerTest.java index 5363bb20a36..976d04efef7 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/impl/ServerListManagerTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/config/impl/ServerListManagerTest.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; import org.junit.Assert; import org.junit.Test; @@ -57,7 +58,9 @@ public void testGetter() throws NacosException { Properties properties = new Properties(); properties.put(PropertyKeyConst.CONTEXT_PATH, "aaa"); properties.put(PropertyKeyConst.ENDPOINT, "endpoint"); - final ServerListManager mgr2 = new ServerListManager(properties); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + final ServerListManager mgr2 = new ServerListManager(nacosClientProperties); Assert.assertEquals("aaa", mgr2.getContentPath()); } @@ -66,7 +69,8 @@ public void testGetter() throws NacosException { Properties properties = new Properties(); properties.put(PropertyKeyConst.CONTEXT_PATH, "aaa"); properties.put(PropertyKeyConst.SERVER_ADDR, "https://1.1.1.1:8848"); - final ServerListManager mgr2 = new ServerListManager(properties); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + final ServerListManager mgr2 = new ServerListManager(nacosClientProperties); Assert.assertEquals("aaa", mgr2.getContentPath()); Assert.assertEquals("[https://1.1.1.1:8848]", mgr2.getServerUrls().toString()); } @@ -75,8 +79,9 @@ public void testGetter() throws NacosException { Properties properties2 = new Properties(); properties2.put(PropertyKeyConst.CONTEXT_PATH, "aaa"); properties2.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1:8848"); - - final ServerListManager mgr3 = new ServerListManager(properties2); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties2); + final ServerListManager mgr3 = new ServerListManager(nacosClientProperties); Assert.assertEquals(1, mgr3.getServerUrls().size()); Assert.assertEquals("http://1.1.1.1:8848", mgr3.getServerUrls().get(0)); Assert.assertEquals("[http://1.1.1.1:8848]", mgr3.getUrlString()); @@ -88,8 +93,9 @@ public void testGetter() throws NacosException { Properties properties3 = new Properties(); properties3.put(PropertyKeyConst.CONTEXT_PATH, "aaa"); properties3.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1:8848,2.2.2.2:8848"); - - final ServerListManager mgr4 = new ServerListManager(properties3); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties3); + final ServerListManager mgr4 = new ServerListManager(nacosClientProperties); Assert.assertEquals(2, mgr4.getServerUrls().size()); Assert.assertEquals("http://1.1.1.1:8848", mgr4.getServerUrls().get(0)); Assert.assertEquals("http://2.2.2.2:8848", mgr4.getServerUrls().get(1)); @@ -101,8 +107,9 @@ public void testGetter() throws NacosException { Properties properties4 = new Properties(); properties4.put(PropertyKeyConst.CONTEXT_PATH, "aaa"); properties4.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1:8848;2.2.2.2:8848"); - - final ServerListManager mgr5 = new ServerListManager(properties4); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties4); + final ServerListManager mgr5 = new ServerListManager(nacosClientProperties); Assert.assertEquals(2, mgr5.getServerUrls().size()); Assert.assertEquals("http://1.1.1.1:8848", mgr5.getServerUrls().get(0)); Assert.assertEquals("http://2.2.2.2:8848", mgr5.getServerUrls().get(1)); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java index 5e94972143c..fce1db6dfc5 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/cache/ServiceInfoHolderTest.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.backups.FailoverReactor; import org.junit.Assert; import org.junit.Test; @@ -37,7 +38,9 @@ public class ServiceInfoHolderTest { @Test public void testGetServiceInfoMap() throws NoSuchFieldException, IllegalAccessException { Properties prop = new Properties(); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); Assert.assertEquals(0, holder.getServiceInfoMap().size()); Field fieldNotifierEventScope = ServiceInfoHolder.class.getDeclaredField("notifierEventScope"); fieldNotifierEventScope.setAccessible(true); @@ -55,7 +58,8 @@ public void testProcessServiceInfo() { info.setHosts(hosts); Properties prop = new Properties(); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); ServiceInfo actual1 = holder.processServiceInfo(info); Assert.assertEquals(info, actual1); @@ -83,7 +87,8 @@ private Instance createInstance(String ip, int port) { @Test public void testProcessServiceInfo2() { Properties prop = new Properties(); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); String json = "{\"groupName\":\"a\",\"name\":\"b\",\"clusters\":\"c\"}"; ServiceInfo actual = holder.processServiceInfo(json); @@ -104,7 +109,8 @@ public void testProcessServiceInfoWithPushEmpty() { Properties prop = new Properties(); prop.setProperty(PropertyKeyConst.NAMING_PUSH_EMPTY_PROTECTION, "true"); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); holder.processServiceInfo(oldInfo); ServiceInfo newInfo = new ServiceInfo("a@@b@@c"); @@ -124,7 +130,8 @@ public void testGetServiceInfo() { info.setHosts(hosts); Properties prop = new Properties(); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); ServiceInfo expect = holder.processServiceInfo(info); String serviceName = "b"; @@ -139,7 +146,8 @@ public void testGetServiceInfo() { @Test public void testShutdown() throws NacosException, NoSuchFieldException, IllegalAccessException { Properties prop = new Properties(); - ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", prop); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + ServiceInfoHolder holder = new ServiceInfoHolder("aa", "scope-001", nacosClientProperties); Field field = ServiceInfoHolder.class.getDeclaredField("failoverReactor"); field.setAccessible(true); FailoverReactor reactor = (FailoverReactor) field.get(holder); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateServiceTest.java index ba1df05802d..404722f79c7 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/core/ServiceInfoUpdateServiceTest.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.InstancesChangeNotifier; import com.alibaba.nacos.client.naming.remote.NamingClientProxy; @@ -46,7 +47,10 @@ public void testScheduleUpdateIfAbsent() throws InterruptedException, NacosExcep InstancesChangeNotifier notifyer = Mockito.mock(InstancesChangeNotifier.class); Properties prop = new Properties(); - final ServiceInfoUpdateService serviceInfoUpdateService = new ServiceInfoUpdateService(prop, holder, proxy, + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + nacosClientProperties.setProperty("namingAsyncQuerySubscribeService", "true"); + final ServiceInfoUpdateService serviceInfoUpdateService = new ServiceInfoUpdateService(nacosClientProperties, holder, proxy, notifyer); serviceInfoUpdateService.scheduleUpdateIfAbsent("aa", "bb", "cc"); @@ -71,7 +75,8 @@ public void testStopUpdateIfContain() throws NacosException { Properties prop = new Properties(); ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); - final ServiceInfoUpdateService serviceInfoUpdateService = new ServiceInfoUpdateService(prop, holder, proxy, + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + final ServiceInfoUpdateService serviceInfoUpdateService = new ServiceInfoUpdateService(nacosClientProperties, holder, proxy, notifyer); serviceInfoUpdateService.scheduleUpdateIfAbsent(serviceName, group, clusters); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java index 751321c8698..15ae5c1b9ae 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/NamingClientProxyDelegateTest.java @@ -25,6 +25,7 @@ import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.ExpressionSelector; import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.InstancesChangeNotifier; import com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy; @@ -50,8 +51,9 @@ public void testRegisterServiceByGrpc() throws NacosException, NoSuchFieldExcept ServiceInfoHolder holder = Mockito.mock(ServiceInfoHolder.class); Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -76,7 +78,9 @@ public void testBatchRegisterServiceByGrpc() throws NacosException, NoSuchFieldE Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -102,7 +106,9 @@ public void testRegisterServiceByHttp() throws NacosException, NoSuchFieldExcept Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class); Field mockHttpClientField = NamingClientProxyDelegate.class.getDeclaredField("httpClientProxy"); mockHttpClientField.setAccessible(true); @@ -128,7 +134,9 @@ public void testDeregisterServiceGrpc() throws NacosException, NoSuchFieldExcept Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -154,7 +162,9 @@ public void testDeregisterServiceHttp() throws NacosException, NoSuchFieldExcept Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingHttpClientProxy mockHttpClient = Mockito.mock(NamingHttpClientProxy.class); Field mockHttpClientField = NamingClientProxyDelegate.class.getDeclaredField("httpClientProxy"); mockHttpClientField.setAccessible(true); @@ -180,7 +190,9 @@ public void testUpdateInstance() throws NacosException { Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); String serviceName = "service1"; String groupName = "group1"; Instance instance = new Instance(); @@ -198,7 +210,9 @@ public void testQueryInstancesOfService() throws NacosException, IllegalAccessEx Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -218,7 +232,9 @@ public void testQueryService() throws NacosException { Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); Service service = delegate.queryService("a", "b"); Assert.assertNull(service); } @@ -230,7 +246,9 @@ public void testCreateService() throws NacosException { Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); Service service = new Service(); try { delegate.createService(service, new NoneSelector()); @@ -246,7 +264,9 @@ public void testDeleteService() throws NacosException { Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); Assert.assertFalse(delegate.deleteService("service", "group1")); } @@ -257,7 +277,9 @@ public void testUpdateService() throws NacosException { Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); Service service = new Service(); try { delegate.updateService(service, new ExpressionSelector()); @@ -273,7 +295,9 @@ public void testGetServiceList() throws NacosException, NoSuchFieldException, Il Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -295,7 +319,9 @@ public void testSubscribe() throws NacosException, NoSuchFieldException, Illegal Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -324,7 +350,9 @@ public void testUnsubscribe() throws NacosException, IllegalAccessException, NoS Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -345,7 +373,9 @@ public void testServerHealthy() throws IllegalAccessException, NacosException, N Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); @@ -362,7 +392,9 @@ public void testShutdown() throws NacosException, IllegalAccessException, NoSuch Properties props = new Properties(); props.setProperty("serverAddr", "localhost"); InstancesChangeNotifier notifier = new InstancesChangeNotifier(); - NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, props, notifier); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingClientProxyDelegate delegate = new NamingClientProxyDelegate(ns, holder, nacosClientProperties, notifier); NamingGrpcClientProxy mockGrpcClient = Mockito.mock(NamingGrpcClientProxy.class); Field grpcClientProxyField = NamingClientProxyDelegate.class.getDeclaredField("grpcClientProxy"); grpcClientProxyField.setAccessible(true); diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxyTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxyTest.java index d136e8d1067..f78d6b91bd5 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxyTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/NamingGrpcClientProxyTest.java @@ -40,6 +40,7 @@ import com.alibaba.nacos.api.remote.response.Response; import com.alibaba.nacos.api.selector.AbstractSelector; import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.cache.ServiceInfoHolder; import com.alibaba.nacos.client.naming.event.ServerListChangedEvent; import com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService; @@ -126,7 +127,9 @@ public void setUp() throws NacosException, NoSuchFieldException, IllegalAccessEx when(factory.getServerList()).thenReturn(serverList); when(factory.genNextServer()).thenReturn(ORIGIN_SERVER); prop = new Properties(); - client = new NamingGrpcClientProxy(NAMESPACE_ID, proxy, factory, prop, holder); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(prop); + client = new NamingGrpcClientProxy(NAMESPACE_ID, proxy, factory, nacosClientProperties, holder); Field rpcClientField = NamingGrpcClientProxy.class.getDeclaredField("rpcClient"); rpcClientField.setAccessible(true); ((RpcClient) rpcClientField.get(client)).shutdown(); @@ -395,7 +398,7 @@ public RequestFuture requestFuture(Request request) throws NacosException { @Override public void asyncRequest(Request request, RequestCallBack requestCallBack) throws NacosException { - + } @Override diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxyTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxyTest.java index 14342bd79cf..714b8bd6d2a 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxyTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/http/NamingHttpClientProxyTest.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.api.naming.pojo.Service; import com.alibaba.nacos.api.naming.pojo.ServiceInfo; import com.alibaba.nacos.api.selector.NoneSelector; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.client.naming.core.ServerListManager; import com.alibaba.nacos.client.naming.utils.UtilAndComs; import com.alibaba.nacos.client.security.SecurityProxy; @@ -76,7 +77,8 @@ public class NamingHttpClientProxyTest { public void setUp() { when(mgr.getServerList()).thenReturn(Arrays.asList("localhost")); props = new Properties(); - clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, props); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + clientProxy = new NamingHttpClientProxy("namespaceId", proxy, mgr, nacosClientProperties); } @After @@ -528,7 +530,8 @@ public void testCallServerFail304() throws Exception { @Test public void testGetNamespaceId() { String namespaceId = "aaa"; - NamingHttpClientProxy clientProxy = new NamingHttpClientProxy(namespaceId, proxy, mgr, props); + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(props); + NamingHttpClientProxy clientProxy = new NamingHttpClientProxy(namespaceId, proxy, mgr, nacosClientProperties); String actualNamespaceId = clientProxy.getNamespaceId(); Assert.assertEquals(namespaceId, actualNamespaceId); } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/utils/InitUtilsTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/utils/InitUtilsTest.java index e3267391e35..aefc3b35f6e 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/utils/InitUtilsTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/utils/InitUtilsTest.java @@ -18,18 +18,17 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; +import com.alibaba.nacos.client.env.NacosClientProperties; import org.junit.Assert; import org.junit.Test; -import java.util.Properties; - public class InitUtilsTest { @Test public void testInitWebRootContext() { String ctx = "/aaa"; - Properties properties = new Properties(); - properties.put(PropertyKeyConst.CONTEXT_PATH, ctx); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + properties.setProperty(PropertyKeyConst.CONTEXT_PATH, ctx); InitUtils.initWebRootContext(properties); Assert.assertEquals(ctx, UtilAndComs.webContext); Assert.assertEquals(ctx + "/v1/ns", UtilAndComs.nacosUrlBase); @@ -42,17 +41,17 @@ public void testInitWebRootContext() { @Test public void testInitNamespaceForNamingDefault() { //DEFAULT - Properties prop = new Properties(); - String ns = InitUtils.initNamespaceForNaming(prop); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + String ns = InitUtils.initNamespaceForNaming(properties); Assert.assertEquals("public", ns); } @Test public void testInitNamespaceForNamingFromProp() { - Properties prop = new Properties(); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); String expect = "ns1"; - prop.put(PropertyKeyConst.NAMESPACE, expect); - String ns = InitUtils.initNamespaceForNaming(prop); + properties.setProperty(PropertyKeyConst.NAMESPACE, expect); + String ns = InitUtils.initNamespaceForNaming(properties); Assert.assertEquals(expect, ns); } @@ -61,9 +60,8 @@ public void testInitNamespaceForNamingFromSystem() { try { String expect1 = "ns1"; System.setProperty(PropertyKeyConst.NAMESPACE, expect1); - Properties prop = new Properties(); - prop.put(PropertyKeyConst.NAMESPACE, "cccccc"); - String ns = InitUtils.initNamespaceForNaming(prop); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + String ns = InitUtils.initNamespaceForNaming(properties); Assert.assertEquals(expect1, ns); } finally { System.clearProperty(PropertyKeyConst.NAMESPACE); @@ -77,11 +75,11 @@ public void testInitNamespaceForNamingFromSystem() { public void testInitNamespaceForNamingFromCloud() { try { String expect1 = "ns1"; - System.setProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, " true"); + System.setProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING, "true"); System.setProperty(SystemPropertyKeyConst.ANS_NAMESPACE, expect1); - Properties prop = new Properties(); - prop.put(PropertyKeyConst.NAMESPACE, "cccccc"); - String ns = InitUtils.initNamespaceForNaming(prop); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); + properties.setProperty(PropertyKeyConst.NAMESPACE, "cccccc"); + String ns = InitUtils.initNamespaceForNaming(properties); Assert.assertEquals(expect1, ns); } finally { System.clearProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING); @@ -92,12 +90,12 @@ public void testInitNamespaceForNamingFromCloud() { @Test public void testInitEndpoint() { - Properties prop = new Properties(); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); String endpoint = "1.1.1.1"; String endpointPort = "1234"; - prop.put(PropertyKeyConst.ENDPOINT, endpoint); - prop.put(PropertyKeyConst.ENDPOINT_PORT, endpointPort); - String actual = InitUtils.initEndpoint(prop); + properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); + properties.setProperty(PropertyKeyConst.ENDPOINT_PORT, endpointPort); + String actual = InitUtils.initEndpoint(properties); Assert.assertEquals(endpoint + ":" + endpointPort, actual); } @@ -105,10 +103,10 @@ public void testInitEndpoint() { public void testInitEndpointAns() { try { System.setProperty(PropertyKeyConst.IS_USE_ENDPOINT_PARSING_RULE, "true"); - Properties prop = new Properties(); + final NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive(); String endpoint = "${key:test.com}"; - prop.put(PropertyKeyConst.ENDPOINT, endpoint); - String actual = InitUtils.initEndpoint(prop); + properties.setProperty(PropertyKeyConst.ENDPOINT, endpoint); + String actual = InitUtils.initEndpoint(properties); //defaultEndpointPort is "8080"; Assert.assertEquals("test.com:8080", actual); } finally { diff --git a/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java b/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java index 358fdaa6cea..55818e272a4 100644 --- a/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/utils/ParamUtilTest.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.client.utils; import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.client.env.NacosClientProperties; import com.alibaba.nacos.common.utils.VersionUtils; import org.junit.After; import org.junit.Assert; @@ -147,7 +148,9 @@ public void testParseNamespace() { String expect = "test"; Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.NAMESPACE, expect); - String actual = ParamUtil.parseNamespace(properties); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + String actual = ParamUtil.parseNamespace(nacosClientProperties); Assert.assertEquals(expect, actual); } diff --git a/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java b/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java index e081a096616..c2bd4da05ec 100644 --- a/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/utils/ValidatorUtilsTest.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.env.NacosClientProperties; import org.junit.Assert; import org.junit.Test; @@ -66,7 +67,9 @@ public void testCheckInitParam() { try { Properties properties = new Properties(); properties.setProperty(PropertyKeyConst.CONTEXT_PATH, "test"); - ValidatorUtils.checkInitParam(properties); + + final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties); + ValidatorUtils.checkInitParam(nacosClientProperties); } catch (NacosException e) { Assert.fail(); }