Skip to content

Commit

Permalink
Support setting config file path via system env and improve error han…
Browse files Browse the repository at this point in the history
…dling in SentinelConfigLoader

Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Dec 18, 2019
1 parent 00dbb5c commit a63c184
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
*/
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";
Expand All @@ -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);
Expand Down Expand Up @@ -88,5 +97,4 @@ public static Properties getProperties() {
return properties;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,34 @@
*/
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);
}
Expand Down

0 comments on commit a63c184

Please sign in to comment.