Selaa lähdekoodia

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 vuotta sitten
vanhempi
commit
a63c1841ee
2 muutettua tiedostoa jossa 30 lisäystä ja 11 poistoa
  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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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);
}


Loading…
Peruuta
Tallenna