From f9695957fa07065d7fddf916f4573566f8f3e71c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=96=87=E8=B6=85?= Date: Thu, 9 Mar 2023 10:05:37 +0800 Subject: [PATCH] [ISSUES #10014] clear confused logic about namespace properties (#10023) * [ISSUES #10014] clear confused logic about namespace properties - add getPropertyFrom method - optimize some code logic Close #10014 * fix ci error --- .../config/impl/LocalConfigInfoProcessor.java | 11 +++-------- .../client/env/NacosClientProperties.java | 9 +++++++++ .../client/env/SearchableProperties.java | 19 +++++++++++++++++++ .../alibaba/nacos/client/env/SourceType.java | 6 +++++- .../client/naming/NacosNamingService.java | 11 +---------- .../nacos/client/naming/utils/InitUtils.java | 11 ++++++----- .../alibaba/nacos/client/utils/ParamUtil.java | 2 +- .../client/env/NacosClientPropertiesTest.java | 18 ++++++++++++++++++ 8 files changed, 62 insertions(+), 25 deletions(-) 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 fd7258e66..76a2330f7 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 @@ -40,8 +40,6 @@ public class LocalConfigInfoProcessor { private static final Logger LOGGER = LogUtils.logger(LocalConfigInfoProcessor.class); - public static final String LOCAL_FILEROOT_PATH; - public static final String LOCAL_SNAPSHOT_PATH; private static final String SUFFIX = "_nacos"; @@ -59,12 +57,9 @@ public class LocalConfigInfoProcessor { private static final String SNAPSHOT_FILE_CHILD_2 = "snapshot-tenant"; static { - 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"; + LOCAL_SNAPSHOT_PATH = NacosClientProperties.PROTOTYPE.getProperty(com.alibaba.nacos.client.constant.Constants.SysEnv.JM_SNAPSHOT_PATH, + NacosClientProperties.PROTOTYPE.getProperty(com.alibaba.nacos.client.constant.Constants.SysEnv.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/env/NacosClientProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java index 021ae97d1..92f83f756 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/NacosClientProperties.java @@ -54,6 +54,15 @@ public interface NacosClientProperties { */ String getProperty(String key, String defaultValue); + /** + * get property from special property source. + * @param source source type + * @see SourceType + * @param key special key + * @return string value or null. + */ + String getPropertyFrom(SourceType source, String key); + /** * get boolean, if the value can not be got by the special key, the null will be returned. * diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java index 636682886..6925222b9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SearchableProperties.java @@ -107,6 +107,25 @@ class SearchableProperties implements NacosClientProperties { return this.search(key, String.class).orElse(defaultValue); } + @Override + public String getPropertyFrom(SourceType source, String key) { + if (source == null) { + return this.getProperty(key); + } + switch (source) { + case JVM: + return JVM_ARGS_PROPERTY_SOURCE.getProperty(key); + case ENV: + return SYSTEM_ENV_PROPERTY_SOURCE.getProperty(key); + case PROPERTIES: + return this.propertiesPropertySource.getProperty(key); + case DEFAULT_SETTING: + return DEFAULT_SETTING_PROPERTY_SOURCE.getProperty(key); + default: + return this.getProperty(key); + } + } + @Override public Boolean getBoolean(String key) { return getBoolean(key, null); diff --git a/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java b/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java index 4727199c6..91f0eb5e3 100644 --- a/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java +++ b/client/src/main/java/com/alibaba/nacos/client/env/SourceType.java @@ -16,7 +16,11 @@ package com.alibaba.nacos.client.env; -enum SourceType { +/** + * properties source type enum. + * @author onewe + */ +public enum SourceType { /** * get value from 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 c67907e98..f35b4f4d7 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 @@ -103,16 +103,7 @@ public class NacosNamingService implements NamingService { } private void initLogName(NacosClientProperties properties) { - logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); - if (StringUtils.isEmpty(logName)) { - - if (StringUtils - .isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) { - logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME); - } else { - logName = DEFAULT_NAMING_LOG_FILE_PATH; - } - } + logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH); } @Override 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 33916f317..d2da7e354 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 @@ -23,6 +23,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.env.SourceType; import com.alibaba.nacos.client.utils.ContextPathUtil; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; @@ -58,21 +59,21 @@ public class InitUtils { if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) { tmpNamespace = TenantUtil.getUserTenantForAns(); - LogUtils.NAMING_LOGGER.info("initializer namespace from System Property : {}", tmpNamespace); + LogUtils.NAMING_LOGGER.info("initializer namespace from ans.namespace attribute : {}", tmpNamespace); tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> { String namespace = properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE); - LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace); + LogUtils.NAMING_LOGGER.info("initializer namespace from ALIBABA_ALIWARE_NAMESPACE attribute :" + namespace); return namespace; }); } tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> { - String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE); - LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace); + String namespace = properties.getPropertyFrom(SourceType.JVM, PropertyKeyConst.NAMESPACE); + LogUtils.NAMING_LOGGER.info("initializer namespace from namespace attribute :" + namespace); return namespace; }); - + if (StringUtils.isEmpty(tmpNamespace)) { tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE); } 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 5c48293fb..316706ee3 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 @@ -223,7 +223,7 @@ public class ParamUtil { } String endpointUrlSource = TemplateUtils - .stringBlankAndThenExecute(NacosClientProperties.PROTOTYPE.getProperty(endpointUrl, System.getenv(endpointUrl)), + .stringBlankAndThenExecute(NacosClientProperties.PROTOTYPE.getProperty(endpointUrl), () -> NacosClientProperties.PROTOTYPE.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL)); if (StringUtils.isBlank(endpointUrlSource)) { diff --git a/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java b/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java index 6e0c361e1..871d84d1b 100644 --- a/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/env/NacosClientPropertiesTest.java @@ -305,4 +305,22 @@ public class NacosClientPropertiesTest { } + @Test + public void testGetPropertyFrom() { + System.setProperty("nacos.home.default.test", "/home/jvm_args"); + NacosClientProperties.PROTOTYPE.setProperty("nacos.home.default.test", "/home/properties_args"); + + Assert.assertEquals(NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.JVM, "nacos.home.default.test"), + "/home/jvm_args"); + Assert.assertEquals( + NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.DEFAULT_SETTING, "nacos.home.default.test"), + "/home/default_setting"); + Assert.assertEquals( + NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.PROPERTIES, "nacos.home.default.test"), + "/home/properties_args"); + Assert.assertEquals( + NacosClientProperties.PROTOTYPE.getPropertyFrom(null, "nacos.home.default.test"), + "/home/jvm_args"); + } + }