Support setting project.name via the properties file and deprecate legacy config path (#1412)

This commit is contained in:
Lin.Liang 2020-04-26 21:06:32 +08:00 committed by GitHub
parent 5e9cfb0deb
commit b716bed370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 87 deletions

View File

@ -16,10 +16,10 @@
package com.alibaba.csp.sentinel.config;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
*
* @author leyou
* @author Eric Zhao
* @author Lin Liang
*/
public final class SentinelConfig {
@ -56,6 +57,12 @@ public final class SentinelConfig {
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;
static {
@ -64,6 +71,8 @@ public final class SentinelConfig {
loadProps();
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();
@ -134,7 +143,7 @@ public final class SentinelConfig {
}
public static String getAppName() {
return AppNameUtil.getAppName();
return appName;
}
/**
@ -211,5 +220,55 @@ public final class SentinelConfig {
}
}
/**
* method for getting application name. This class uses the flowing order to get app's name:
*
* <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"
* 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>
* <code>
* "test.Main" -> test.Main<br/>
* "/target/test.Main" -> test.Main<br/>
* "/target/test.Main args1" -> test.Main<br/>
* "Main.jar" -> Main<br/>
* "/target/Main.JAR args1" -> Main<br/>
* "Mai n.jar" -> Mai // whitespace in file name is not allowed<br/>
* </code>
* </p>
* </li>
* </ol>
*/
public static void resolveAppName() {
if (props.get(APP_NAME) != null) {
appName = props.get(APP_NAME);
return;
}
// parse sun.java.command property
String command = System.getProperty(SUN_JAVA_COMMAND);
if (StringUtil.isBlank(command)) {
return;
}
command = command.split("\\s")[0];
String separator = File.separator;
if (command.contains(separator)) {
String[] strs;
if ("\\".equals(separator)) {
strs = command.split("\\\\");
} else {
strs = command.split(separator);
}
command = strs[strs.length - 1];
}
if (command.endsWith(JAR_SUFFIX_LOWER) || command.endsWith(JAR_SUFFIX_UPPER)) {
command = command.substring(0, command.length() - 4);
}
appName = command;
}
private SentinelConfig() {}
}

View File

@ -38,9 +38,6 @@ public final class SentinelConfigLoader {
public static final String SENTINEL_CONFIG_ENV_KEY = "CSP_SENTINEL_CONFIG_FILE";
public static final String SENTINEL_CONFIG_PROPERTY_KEY = "csp.sentinel.config.file";
private static final String DIR_NAME = "logs" + File.separator + "csp";
private static final String USER_HOME = "user.home";
private static final String DEFAULT_SENTINEL_CONFIG_FILE = "classpath:sentinel.properties";
private static Properties properties = new Properties();
@ -64,17 +61,6 @@ public final class SentinelConfigLoader {
}
Properties p = ConfigUtil.loadProperties(fileName);
// Compatible with legacy config file path.
if (p == null) {
String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator;
fileName = path + AppNameUtil.getAppName() + ".properties";
File file = new File(fileName);
if (file.exists()) {
p = ConfigUtil.loadProperties(fileName);
}
}
if (p != null && !p.isEmpty()) {
RecordLog.info("[SentinelConfigLoader] Loading Sentinel config from " + fileName);
properties.putAll(p);

View File

@ -15,86 +15,21 @@
*/
package com.alibaba.csp.sentinel.util;
import com.alibaba.csp.sentinel.log.RecordLog;
import java.io.File;
import com.alibaba.csp.sentinel.config.SentinelConfig;
/**
* Util class for getting application name. This class uses the flowing order to get app's name:
*
* <ol>
* <li>get {@code project.name} from System Properties, 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"
* 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>
* <code>
* "test.Main" -> test.Main<br/>
* "/target/test.Main" -> test.Main<br/>
* "/target/test.Main args1" -> test.Main<br/>
* "Main.jar" -> Main<br/>
* "/target/Main.JAR args1" -> Main<br/>
* "Mai n.jar" -> Mai // whitespace in file name is not allowed<br/>
* </code>
* </p>
* </li>
* </ol>
*
* @author Eric Zhao
* @author leyou
*/
public final class AppNameUtil {
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";
private static String appName;
private AppNameUtil() {
}
static {
resolveAppName();
RecordLog.info("App name resolved: " + appName);
}
public static void resolveAppName() {
String app = System.getProperty(APP_NAME);
// use -Dproject.name first
if (!isEmpty(app)) {
appName = app;
return;
}
// parse sun.java.command property
String command = System.getProperty(SUN_JAVA_COMMAND);
if (isEmpty(command)) {
return;
}
command = command.split("\\s")[0];
String separator = File.separator;
if (command.contains(separator)) {
String[] strs;
if ("\\".equals(separator)) {
strs = command.split("\\\\");
} else {
strs = command.split(separator);
}
command = strs[strs.length - 1];
}
if (command.endsWith(JAR_SUFFIX_LOWER) || command.endsWith(JAR_SUFFIX_UPPER)) {
command = command.substring(0, command.length() - 4);
}
appName = command;
}
public static String getAppName() {
return appName;
return SentinelConfig.getAppName();
}
private static boolean isEmpty(String str) {
return str == null || "".equals(str);
}
}

View File

@ -20,6 +20,7 @@ import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.util.AppNameUtil;
@ -31,11 +32,11 @@ import com.alibaba.csp.sentinel.util.AppNameUtil;
public final class ConfigPropertyHelper {
public static void setAppNameProperty(String appName) {
System.setProperty(AppNameUtil.APP_NAME, appName);
System.setProperty(SentinelConfig.APP_NAME, appName);
}
public static void clearAppNameProperty() {
System.clearProperty(AppNameUtil.APP_NAME);
System.clearProperty(SentinelConfig.APP_NAME);
}
public static void runWithConfig(Properties prop, String appName, Task task) throws Exception {

View File

@ -69,7 +69,7 @@ public class SentinelConfigTest {
//add Jvm parameter
//-Dcsp.sentinel.config.file=sentinel-propertiesTest.properties
//-Dcsp.sentinel.config.file=classpath:sentinel-propertiesTest.properties
//-Dcsp.sentinel.flow.cold.factor=5
//-Dcsp.sentinel.statistic.max.rt=1000
//@Test
@ -92,6 +92,8 @@ public class SentinelConfigTest {
out.write(buildPropertyStr(COLD_FACTOR, "123"));
out.write("\n");
out.write(buildPropertyStr(STATISTIC_MAX_RT, "6000"));
out.write("\n");
out.write(buildPropertyStr(APP_NAME, "sentinel_test"));
out.flush();
out.close();
@ -100,6 +102,7 @@ public class SentinelConfigTest {
Assert.assertTrue(SentinelConfig.getConfig(TOTAL_METRIC_FILE_COUNT).equals("20"));
Assert.assertTrue(SentinelConfig.getConfig(COLD_FACTOR).equals("5"));
Assert.assertTrue(SentinelConfig.getConfig(STATISTIC_MAX_RT).equals("1000"));
Assert.assertTrue(SentinelConfig.getAppName().equals("sentinel_test"));
} finally {
if (file != null) {