Enhance nacos client init properties logger. (#12166)

This commit is contained in:
杨翊 SionYang 2024-06-03 13:32:08 +08:00 committed by GitHub
parent da78aeea0d
commit 7cdf224157
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 84 additions and 13 deletions

View File

@ -62,7 +62,7 @@ public class PropertyKeyConst {
public static final String CONFIG_RETRY_TIME = "configRetryTime";
public static final String CLIENT_WORKER_MAX_THREAD_COUNT = "clientWorkerMaxThreadCount";
public static final String CLIENT_WORKER_THREAD_COUNT = "clientWorkerThreadCount";
public static final String MAX_RETRY = "maxRetry";
@ -91,6 +91,8 @@ public class PropertyKeyConst {
public static final String SIGNATURE_REGION_ID = "signatureRegionId";
public static final String LOG_ALL_PROPERTIES = "logAllProperties";
/**
* Get the key value of some variable value from the system property.
*/

View File

@ -76,6 +76,7 @@ public class NacosConfigService implements ConfigService {
public NacosConfigService(Properties properties) throws NacosException {
PreInitUtils.asyncPreLoadCostComponent();
final NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
LOGGER.info(ParamUtil.getInputParameters(clientProperties.asProperties()));
ValidatorUtils.checkInitParam(clientProperties);
initNamespace(clientProperties);

View File

@ -35,12 +35,13 @@ import com.alibaba.nacos.client.naming.event.InstancesChangeNotifier;
import com.alibaba.nacos.client.naming.event.InstancesDiff;
import com.alibaba.nacos.client.naming.remote.NamingClientProxy;
import com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate;
import com.alibaba.nacos.client.naming.selector.NamingSelectorFactory;
import com.alibaba.nacos.client.naming.selector.NamingSelectorWrapper;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.PreInitUtils;
import com.alibaba.nacos.client.naming.selector.NamingSelectorFactory;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.JacksonUtils;
@ -52,8 +53,8 @@ import java.util.List;
import java.util.Properties;
import java.util.UUID;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
import static com.alibaba.nacos.client.naming.selector.NamingSelectorFactory.getUniqueClusterString;
import static com.alibaba.nacos.client.utils.LogUtils.NAMING_LOGGER;
/**
* Nacos Naming Service.
@ -98,6 +99,7 @@ public class NacosNamingService implements NamingService {
private void init(Properties properties) throws NacosException {
PreInitUtils.asyncPreLoadCostComponent();
final NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
NAMING_LOGGER.info(ParamUtil.getInputParameters(nacosClientProperties.asProperties()));
ValidatorUtils.checkInitParam(nacosClientProperties);
this.namespace = InitUtils.initNamespaceForNaming(nacosClientProperties);
InitUtils.initSerialization();

View File

@ -20,11 +20,13 @@ import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.VersionUtils;
import org.slf4j.Logger;
import java.util.Properties;
import java.util.regex.Pattern;
/**
@ -80,8 +82,8 @@ public class ParamUtil {
// Client identity information
appKey = NacosClientProperties.PROTOTYPE.getProperty(NACOS_CLIENT_APP_KEY, BLANK_STR);
defaultContextPath = NacosClientProperties.PROTOTYPE
.getProperty(NACOS_CLIENT_CONTEXTPATH_KEY, DEFAULT_NACOS_CLIENT_CONTEXTPATH);
defaultContextPath = NacosClientProperties.PROTOTYPE.getProperty(NACOS_CLIENT_CONTEXTPATH_KEY,
DEFAULT_NACOS_CLIENT_CONTEXTPATH);
appName = AppNameUtils.getAppName();
@ -111,8 +113,8 @@ public class ParamUtil {
private static double initPerTaskConfigSize() {
try {
return Double.parseDouble(NacosClientProperties.PROTOTYPE
.getProperty(PER_TASK_CONFIG_SIZE_KEY, DEFAULT_PER_TASK_CONFIG_SIZE_KEY));
return Double.parseDouble(NacosClientProperties.PROTOTYPE.getProperty(PER_TASK_CONFIG_SIZE_KEY,
DEFAULT_PER_TASK_CONFIG_SIZE_KEY));
} catch (NumberFormatException e) {
LOGGER.error("[PER_TASK_CONFIG_SIZE] PER_TASK_CONFIG_SIZE invalid", e);
throw new IllegalArgumentException("invalid PER_TASK_CONFIG_SIZE, expected value type double", e);
@ -217,8 +219,8 @@ public class ParamUtil {
// If entered in the configuration file, the priority in ENV will be given priority.
if (endpointUrl == null || !PATTERN.matcher(endpointUrl).find()) {
// skip retrieve from system property and retrieve directly from system env
String endpointUrlSource = NacosClientProperties.PROTOTYPE
.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
String endpointUrlSource = NacosClientProperties.PROTOTYPE.getProperty(
PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
if (StringUtils.isNotBlank(endpointUrlSource)) {
endpointUrl = endpointUrlSource;
}
@ -234,10 +236,10 @@ public class ParamUtil {
endpointUrl = endpointUrl.substring(0, defStartOf);
}
String endpointUrlSource = TemplateUtils
.stringBlankAndThenExecute(NacosClientProperties.PROTOTYPE.getProperty(endpointUrl),
() -> NacosClientProperties.PROTOTYPE
.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL));
String endpointUrlSource = TemplateUtils.stringBlankAndThenExecute(
NacosClientProperties.PROTOTYPE.getProperty(endpointUrl),
() -> NacosClientProperties.PROTOTYPE.getProperty(
PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL));
if (StringUtils.isBlank(endpointUrlSource)) {
if (StringUtils.isNotBlank(defaultEndpointUrl)) {
@ -264,4 +266,41 @@ public class ParamUtil {
}
return envName;
}
public static String getInputParameters(Properties properties) {
boolean logAllParameters = ConvertUtils.toBoolean(properties.getProperty(PropertyKeyConst.LOG_ALL_PROPERTIES),
false);
StringBuilder result = new StringBuilder();
if (logAllParameters) {
result.append(
"Log nacos client init properties with Full mode, This mode is only used for debugging and troubleshooting. ");
result.append(
"Please close this mode by removing properties `logAllProperties` after finishing debug or troubleshoot.\n");
result.append("Nacos client all init properties: \n");
properties.forEach(
(key, value) -> result.append("\t").append(key.toString()).append("=").append(value.toString())
.append("\n"));
} else {
result.append("Nacos client key init properties: \n");
appendKeyParameters(result, properties, PropertyKeyConst.SERVER_ADDR);
appendKeyParameters(result, properties, PropertyKeyConst.NAMESPACE);
appendKeyParameters(result, properties, PropertyKeyConst.ENDPOINT);
appendKeyParameters(result, properties, PropertyKeyConst.ENDPOINT_PORT);
appendKeyParameters(result, properties, PropertyKeyConst.USERNAME);
appendKeyParameters(result, properties, PropertyKeyConst.PASSWORD);
appendKeyParameters(result, properties, PropertyKeyConst.ACCESS_KEY);
appendKeyParameters(result, properties, PropertyKeyConst.SECRET_KEY);
appendKeyParameters(result, properties, PropertyKeyConst.RAM_ROLE_NAME);
appendKeyParameters(result, properties, PropertyKeyConst.SIGNATURE_REGION_ID);
}
return result.toString();
}
private static void appendKeyParameters(StringBuilder result, Properties properties, String propertyKey) {
String propertyValue = properties.getProperty(propertyKey);
if (StringUtils.isBlank(propertyValue)) {
return;
}
result.append("\t").append(propertyKey).append("=").append(propertyValue).append("\n");
}
}

View File

@ -20,6 +20,7 @@ package com.alibaba.nacos.client.utils;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.env.SourceType;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.common.utils.VersionUtils;
import org.junit.jupiter.api.AfterEach;
@ -32,6 +33,7 @@ import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ParamUtilTest {
@ -225,4 +227,29 @@ class ParamUtilTest {
String expect = "test";
assertEquals(expect, ParamUtil.simplyEnvNameIfOverLimit(expect));
}
@Test
void testGetInputParametersWithFullMode() {
Properties properties = new Properties();
properties.setProperty("testKey", "testValue");
properties.setProperty(PropertyKeyConst.LOG_ALL_PROPERTIES, "true");
NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
String actual = ParamUtil.getInputParameters(clientProperties.asProperties());
assertTrue(actual.startsWith("Log nacos client init properties with Full mode, This mode is only used for debugging and troubleshooting."));
assertTrue(actual.contains("\ttestKey=testValue\n"));
Properties envProperties = clientProperties.getProperties(SourceType.ENV);
String envCaseKey = envProperties.stringPropertyNames().iterator().next();
String envCaseValue = envProperties.getProperty(envCaseKey);
assertTrue(actual.contains(String.format("\t%s=%s\n", envCaseKey, envCaseValue)));
}
@Test
void testGetInputParameters() {
Properties properties = new Properties();
properties.setProperty("testKey", "testValue");
properties.setProperty(PropertyKeyConst.SERVER_ADDR, "localhost:8848");
NacosClientProperties clientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
String actual = ParamUtil.getInputParameters(clientProperties.asProperties());
assertEquals("Nacos client key init properties: \n\tserverAddr=localhost:8848\n", actual);
}
}