[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
This commit is contained in:
毛文超 2023-03-09 10:05:37 +08:00 committed by GitHub
parent 62ef024da4
commit f9695957fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 25 deletions

View File

@ -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);
}

View File

@ -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.
*

View File

@ -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);

View File

@ -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.
*/

View File

@ -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

View File

@ -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,18 +59,18 @@ 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;
});

View File

@ -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)) {

View File

@ -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");
}
}