Bläddra i källkod

Support setting config file path via system env and improve error handling in SentinelConfigLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 år sedan
förälder
incheckning
a63c1841ee
2 ändrade filer med 30 tillägg och 11 borttagningar
  1. +13
    -5
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java
  2. +17
    -6
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java

+ 13
- 5
sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java Visa fil

@@ -35,7 +35,8 @@ import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator;
*/
public final class SentinelConfigLoader {

public static final String SENTINEL_CONFIG = "csp.sentinel.config.file";
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";
@@ -45,13 +46,21 @@ public final class SentinelConfigLoader {
private static Properties properties = new Properties();

static {
load();
try {
load();
} catch (Throwable t) {
RecordLog.warn("[SentinelConfigLoader] Failed to initialize configuration items", t);
}
}

private static void load() {
String fileName = System.getProperty(SENTINEL_CONFIG);
// Order: system property -> system env -> default file (classpath:sentinel.properties) -> legacy path
String fileName = System.getProperty(SENTINEL_CONFIG_PROPERTY_KEY);
if (StringUtil.isBlank(fileName)) {
fileName = DEFAULT_SENTINEL_CONFIG_FILE;
fileName = System.getenv(SENTINEL_CONFIG_ENV_KEY);
if (StringUtil.isBlank(fileName)) {
fileName = DEFAULT_SENTINEL_CONFIG_FILE;
}
}

Properties p = ConfigUtil.loadProperties(fileName);
@@ -88,5 +97,4 @@ public final class SentinelConfigLoader {
return properties;
}


}

+ 17
- 6
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java Visa fil

@@ -30,23 +30,34 @@ import java.util.concurrent.CopyOnWriteArraySet;
*/
public class LogConfigLoader {

public static final String LOG_CONFIG = "csp.sentinel.config.file";
public static final String LOG_CONFIG_ENV_KEY = "CSP_SENTINEL_CONFIG_FILE";
public static final String LOG_CONFIG_PROPERTY_KEY = "csp.sentinel.config.file";

private static final String DEFAULT_LOG_CONFIG_FILE = "classpath:sentinel.properties";

private static final Properties properties = new Properties();

static {
load();
try {
load();
} catch (Throwable t) {
// NOTE: do not use RecordLog here, or there will be circular class dependency!
System.err.println("[LogConfigLoader] Failed to initialize configuration items");
t.printStackTrace();
}
}

private static void load() {
String file = System.getProperty(LOG_CONFIG);
if (StringUtil.isBlank(file)) {
file = DEFAULT_LOG_CONFIG_FILE;
// Order: system property -> system env -> default file (classpath:sentinel.properties) -> legacy path
String fileName = System.getProperty(LOG_CONFIG_PROPERTY_KEY);
if (StringUtil.isBlank(fileName)) {
fileName = System.getenv(LOG_CONFIG_ENV_KEY);
if (StringUtil.isBlank(fileName)) {
fileName = DEFAULT_LOG_CONFIG_FILE;
}
}

Properties p = ConfigUtil.loadProperties(file);
Properties p = ConfigUtil.loadProperties(fileName);
if (p != null && !p.isEmpty()) {
properties.putAll(p);
}


Laddar…
Avbryt
Spara