From a63c1841ee27b0f50b19ed8be06cb82b784f9115 Mon Sep 17 00:00:00 2001 From: Eric Zhao Date: Wed, 18 Dec 2019 19:00:52 +0800 Subject: [PATCH] Support setting config file path via system env and improve error handling in SentinelConfigLoader Signed-off-by: Eric Zhao --- .../sentinel/config/SentinelConfigLoader.java | 18 +++++++++++---- .../csp/sentinel/log/LogConfigLoader.java | 23 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java index aeab115a..67cd4805 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfigLoader.java @@ -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; } - } diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java index f8cffaa2..3cdb541b 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogConfigLoader.java @@ -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); }