diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 20e76332..996432d7 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -42,9 +42,13 @@ public final class SentinelConfig { public static final int APP_TYPE_COMMON = 0; private static final Map props = new ConcurrentHashMap<>(); + private static int appType = APP_TYPE_COMMON; + private static String appName = ""; - public static final String APP_TYPE = "csp.sentinel.app.type"; + 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 { } /** - *

Get the max RT value that Sentinel could accept.

- *

Response time that exceeds {@code statisticMaxRt} will be recorded as this value. - * The default value is {@link #DEFAULT_STATISTIC_MAX_RT}.

+ *

Get the max RT value that Sentinel could accept for system BBR strategy.

* * @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: * *
    - *
  1. get {@code project.name} from System Properties, if not null, use the value as app name;
  2. - *
  3. get {@code project.name} from Properties file, if not null, use the value as app name;
  4. - *
  5. get {@code sun.java.command} from System properties, remove path, arguments and ".jar" or ".JAR" + *
  6. Resolve the value from {@code CSP_SENTINEL_APP_NAME} system environment;
  7. + *
  8. Resolve the value from {@code csp.sentinel.app.name} system property;
  9. + *
  10. Resolve the value from {@code project.name} system property (for compatibility);
  11. + *
  12. 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: *

    @@ -242,15 +237,31 @@ public final class SentinelConfig { *

  13. *
*/ - 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; + } + 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; } - // parse sun.java.command property - String command = System.getProperty(SUN_JAVA_COMMAND); + 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() {} diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConfigPropertyHelper.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConfigPropertyHelper.java index d5c1f8a5..c64e0627 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConfigPropertyHelper.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/ConfigPropertyHelper.java @@ -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 { diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java index 456a2b75..44273e19 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/config/SentinelConfigTest.java @@ -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();