From d03704b36f437da510e7d265910255056d03fae1 Mon Sep 17 00:00:00 2001 From: goodjava Date: Fri, 23 Apr 2021 10:17:54 +0800 Subject: [PATCH] An improved method for obtaining ip address and port number from containers --- .../csp/sentinel/config/SentinelConfig.java | 18 ++++++++++++++++++ .../transport/config/TransportConfig.java | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java index 9ef544e4ea..9635295659 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java @@ -125,6 +125,24 @@ public static String getConfig(String key) { AssertUtil.notNull(key, "key cannot be null"); return props.get(key); } + + /** + * Get config value of the specific key. + * + * @param key config key + * @param envVariableKey Get the value of the environment variable with the given key + * @return the config value. + */ + public static String getConfig(String key, boolean envVariableKey) { + AssertUtil.notNull(key, "key cannot be null"); + if (envVariableKey) { + String value = System.getenv(key); + if (StringUtil.isNotEmpty(value)) { + return value; + } + } + return getConfig(key); + } public static void setConfig(String key, String value) { AssertUtil.notNull(key, "key cannot be null"); diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java index 2294aebf43..b8db1a975e 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java @@ -140,7 +140,7 @@ public static String getPort() { if (runtimePort > 0) { return String.valueOf(runtimePort); } - return SentinelConfig.getConfig(SERVER_PORT); + return SentinelConfig.getConfig(SERVER_PORT, true); } /** @@ -159,7 +159,7 @@ public static void setRuntimePort(int port) { * @return the local ip. */ public static String getHeartbeatClientIp() { - String ip = SentinelConfig.getConfig(HEARTBEAT_CLIENT_IP); + String ip = SentinelConfig.getConfig(HEARTBEAT_CLIENT_IP, true); if (StringUtil.isBlank(ip)) { ip = HostNameUtil.getIp(); }