Update resolving logic of project name and polish SentinelConfig (#1437)
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
b716bed370
commit
7f0771df5c
|
|
@ -42,9 +42,13 @@ public final class SentinelConfig {
|
|||
public static final int APP_TYPE_COMMON = 0;
|
||||
|
||||
private static final Map<String, String> props = new ConcurrentHashMap<>();
|
||||
private static int appType = APP_TYPE_COMMON;
|
||||
|
||||
public static final String APP_TYPE = "csp.sentinel.app.type";
|
||||
private static int appType = APP_TYPE_COMMON;
|
||||
private static String appName = "";
|
||||
|
||||
public static final String PROJECT_NAME_PROP_KEY = "project.name";
|
||||
public static final String APP_NAME_PROP_KEY = "csp.sentinel.app.name";
|
||||
public static final String APP_TYPE_PROP_KEY = "csp.sentinel.app.type";
|
||||
public static final String CHARSET = "csp.sentinel.charset";
|
||||
public static final String SINGLE_METRIC_FILE_SIZE = "csp.sentinel.metric.file.single.size";
|
||||
public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count";
|
||||
|
|
@ -52,27 +56,19 @@ public final class SentinelConfig {
|
|||
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
|
||||
public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader";
|
||||
|
||||
static final String DEFAULT_CHARSET = "UTF-8";
|
||||
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
|
||||
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
|
||||
static final int DEFAULT_COLD_FACTOR = 3;
|
||||
|
||||
private static String appName = "";
|
||||
public static final String APP_NAME = "project.name";
|
||||
public static final String SUN_JAVA_COMMAND = "sun.java.command";
|
||||
private static final String JAR_SUFFIX_LOWER = ".jar";
|
||||
private static final String JAR_SUFFIX_UPPER = ".JAR";
|
||||
|
||||
public static final int DEFAULT_STATISTIC_MAX_RT = 4900;
|
||||
public static final String DEFAULT_CHARSET = "UTF-8";
|
||||
public static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;
|
||||
public static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6;
|
||||
public static final int DEFAULT_COLD_FACTOR = 3;
|
||||
public static final int DEFAULT_STATISTIC_MAX_RT = 5000;
|
||||
|
||||
static {
|
||||
try {
|
||||
initialize();
|
||||
loadProps();
|
||||
resolveAppName();
|
||||
resolveAppType();
|
||||
RecordLog.info("[SentinelConfig] Application type resolved: " + appType);
|
||||
resolveAppName();
|
||||
RecordLog.info("[SentinelConfig] Application name resolved: " + appName);
|
||||
} catch (Throwable ex) {
|
||||
RecordLog.warn("[SentinelConfig] Failed to initialize", ex);
|
||||
ex.printStackTrace();
|
||||
|
|
@ -81,7 +77,7 @@ public final class SentinelConfig {
|
|||
|
||||
private static void resolveAppType() {
|
||||
try {
|
||||
String type = getConfig(APP_TYPE);
|
||||
String type = getConfig(APP_TYPE_PROP_KEY);
|
||||
if (type == null) {
|
||||
appType = APP_TYPE_COMMON;
|
||||
return;
|
||||
|
|
@ -198,9 +194,7 @@ public final class SentinelConfig {
|
|||
}
|
||||
|
||||
/**
|
||||
* <p>Get the max RT value that Sentinel could accept.</p>
|
||||
* <p>Response time that exceeds {@code statisticMaxRt} will be recorded as this value.
|
||||
* The default value is {@link #DEFAULT_STATISTIC_MAX_RT}.</p>
|
||||
* <p>Get the max RT value that Sentinel could accept for system BBR strategy.</p>
|
||||
*
|
||||
* @return the max allowed RT value
|
||||
* @since 1.4.1
|
||||
|
|
@ -221,12 +215,13 @@ public final class SentinelConfig {
|
|||
}
|
||||
|
||||
/**
|
||||
* method for getting application name. This class uses the flowing order to get app's name:
|
||||
* Function for resolving project name. The order is elaborated below:
|
||||
*
|
||||
* <ol>
|
||||
* <li>get {@code project.name} from System Properties, if not null, use the value as app name;</li>
|
||||
* <li>get {@code project.name} from Properties file, if not null, use the value as app name;</li>
|
||||
* <li>get {@code sun.java.command} from System properties, remove path, arguments and ".jar" or ".JAR"
|
||||
* <li>Resolve the value from {@code CSP_SENTINEL_APP_NAME} system environment;</li>
|
||||
* <li>Resolve the value from {@code csp.sentinel.app.name} system property;</li>
|
||||
* <li>Resolve the value from {@code project.name} system property (for compatibility);</li>
|
||||
* <li>Resolve the value from {@code sun.java.command} system property, then remove path, arguments and ".jar" or ".JAR"
|
||||
* suffix, use the result as app name. Note that whitespace in file name or path is not allowed, or a
|
||||
* wrong app name may be gotten, For example:
|
||||
* <p>
|
||||
|
|
@ -242,15 +237,31 @@ public final class SentinelConfig {
|
|||
* </li>
|
||||
* </ol>
|
||||
*/
|
||||
public static void resolveAppName() {
|
||||
|
||||
if (props.get(APP_NAME) != null) {
|
||||
appName = props.get(APP_NAME);
|
||||
private static void resolveAppName() {
|
||||
// Priority: system env -> csp.sentinel.app.name -> project.name -> main class (or jar) name
|
||||
String envKey = toEnvKey(APP_NAME_PROP_KEY);
|
||||
String n = System.getenv(envKey);
|
||||
if (!StringUtil.isBlank(n)) {
|
||||
appName = n;
|
||||
RecordLog.info("App name resolved from system env {}: {}", envKey, appName);
|
||||
return;
|
||||
}
|
||||
// parse sun.java.command property
|
||||
String command = System.getProperty(SUN_JAVA_COMMAND);
|
||||
n = props.get(APP_NAME_PROP_KEY);
|
||||
if (!StringUtil.isBlank(n)) {
|
||||
appName = n;
|
||||
RecordLog.info("App name resolved from property {}: {}", APP_NAME_PROP_KEY, appName);
|
||||
return;
|
||||
}
|
||||
n = props.get(PROJECT_NAME_PROP_KEY);
|
||||
if (!StringUtil.isBlank(n)) {
|
||||
appName = n;
|
||||
RecordLog.info("App name resolved from property {}: {}", PROJECT_NAME_PROP_KEY, appName);
|
||||
return;
|
||||
}
|
||||
// Parse sun.java.command property by default.
|
||||
String command = System.getProperty("sun.java.command");
|
||||
if (StringUtil.isBlank(command)) {
|
||||
RecordLog.warn("Cannot resolve default appName from property sun.java.command");
|
||||
return;
|
||||
}
|
||||
command = command.split("\\s")[0];
|
||||
|
|
@ -258,16 +269,22 @@ public final class SentinelConfig {
|
|||
if (command.contains(separator)) {
|
||||
String[] strs;
|
||||
if ("\\".equals(separator)) {
|
||||
// Handle separator in Windows.
|
||||
strs = command.split("\\\\");
|
||||
} else {
|
||||
strs = command.split(separator);
|
||||
}
|
||||
command = strs[strs.length - 1];
|
||||
}
|
||||
if (command.endsWith(JAR_SUFFIX_LOWER) || command.endsWith(JAR_SUFFIX_UPPER)) {
|
||||
if (command.toLowerCase().endsWith(".jar")) {
|
||||
command = command.substring(0, command.length() - 4);
|
||||
}
|
||||
appName = command;
|
||||
RecordLog.info("App name resolved from default: {}", appName);
|
||||
}
|
||||
|
||||
private static String toEnvKey(/*@NotBlank*/ String propKey) {
|
||||
return propKey.toUpperCase().replace('.', '_');
|
||||
}
|
||||
|
||||
private SentinelConfig() {}
|
||||
|
|
|
|||
|
|
@ -32,11 +32,11 @@ import com.alibaba.csp.sentinel.util.AppNameUtil;
|
|||
public final class ConfigPropertyHelper {
|
||||
|
||||
public static void setAppNameProperty(String appName) {
|
||||
System.setProperty(SentinelConfig.APP_NAME, appName);
|
||||
System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, appName);
|
||||
}
|
||||
|
||||
public static void clearAppNameProperty() {
|
||||
System.clearProperty(SentinelConfig.APP_NAME);
|
||||
System.clearProperty(SentinelConfig.APP_NAME_PROP_KEY);
|
||||
}
|
||||
|
||||
public static void runWithConfig(Properties prop, String appName, Task task) throws Exception {
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ public class SentinelConfigTest {
|
|||
out.write("\n");
|
||||
out.write(buildPropertyStr(STATISTIC_MAX_RT, "6000"));
|
||||
out.write("\n");
|
||||
out.write(buildPropertyStr(APP_NAME, "sentinel_test"));
|
||||
out.write(buildPropertyStr(PROJECT_NAME_PROP_KEY, "sentinel_test"));
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue