Support config from system env in SentinelConfig and polish TransportConfig (#2154)

- An improved method for obtaining IP address and port number from containers
This commit is contained in:
张志勇 2021-04-27 19:42:22 +08:00 committed by GitHub
parent 36b162cc70
commit 0d22aca591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -125,6 +125,24 @@ public final class SentinelConfig {
AssertUtil.notNull(key, "key cannot be null"); AssertUtil.notNull(key, "key cannot be null");
return props.get(key); 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) { public static void setConfig(String key, String value) {
AssertUtil.notNull(key, "key cannot be null"); AssertUtil.notNull(key, "key cannot be null");

View File

@ -140,7 +140,7 @@ public class TransportConfig {
if (runtimePort > 0) { if (runtimePort > 0) {
return String.valueOf(runtimePort); return String.valueOf(runtimePort);
} }
return SentinelConfig.getConfig(SERVER_PORT); return SentinelConfig.getConfig(SERVER_PORT, true);
} }
/** /**
@ -159,7 +159,7 @@ public class TransportConfig {
* @return the local ip. * @return the local ip.
*/ */
public static String getHeartbeatClientIp() { public static String getHeartbeatClientIp() {
String ip = SentinelConfig.getConfig(HEARTBEAT_CLIENT_IP); String ip = SentinelConfig.getConfig(HEARTBEAT_CLIENT_IP, true);
if (StringUtil.isBlank(ip)) { if (StringUtil.isBlank(ip)) {
ip = HostNameUtil.getIp(); ip = HostNameUtil.getIp();
} }