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 fd7258e66e7..76a2330f70c 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 021ae97d177..92f83f75670 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 636682886e5..6925222b976 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 @@ public String getProperty(String key, String defaultValue) { 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 4727199c6d1..91f0eb5e3a9 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 c67907e98c4..f35b4f4d782 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 @@ private void init(Properties properties) throws NacosException { } 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 33916f3177f..d2da7e35498 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.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 static String initNamespaceForNaming(NacosClientProperties properties) { 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 5c48293fb2f..316706ee3de 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 static String parsingEndpointRule(String endpointUrl) { } 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 6e0c361e1ef..871d84d1b91 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 void testGetPropertyWithScope() { } + @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"); + } + }