From eb29cc929f4a4470464380fc90b039e9c6d9cbc2 Mon Sep 17 00:00:00 2001 From: "nick.tan" Date: Mon, 3 Dec 2018 09:48:15 +0800 Subject: [PATCH] Add heartbeat client ip config to resolve ip problem in Docker env (#261) --- .../transport/config/TransportConfig.java | 17 +++++++++++++ .../transport/config/TransportConfigTest.java | 25 +++++++++++++++++++ .../heartbeat/HttpHeartbeatSender.java | 2 +- .../transport/heartbeat/HeartbeatMessage.java | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java 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 5f934a98..ab150175 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 @@ -16,6 +16,8 @@ package com.alibaba.csp.sentinel.transport.config; import com.alibaba.csp.sentinel.config.SentinelConfig; +import com.alibaba.csp.sentinel.util.HostNameUtil; +import com.alibaba.csp.sentinel.util.StringUtil; /** * @author leyou @@ -25,6 +27,7 @@ public class TransportConfig { public static final String CONSOLE_SERVER = "csp.sentinel.dashboard.server"; public static final String SERVER_PORT = "csp.sentinel.api.port"; public static final String HEARTBEAT_INTERVAL_MS = "csp.sentinel.heartbeat.interval.ms"; + public static final String HEARTBEAT_CLIENT_IP = "csp.sentinel.heartbeat.client.ip"; private static int runtimePort = -1; @@ -66,4 +69,18 @@ public class TransportConfig { public static void setRuntimePort(int port) { runtimePort = port; } + + /** + * Get heartbeat client local ip. + * If the client ip not configured,it will be the address of local host + * + * @return the local ip. + */ + public static String getHeartbeatClientIp() { + String ip = SentinelConfig.getConfig(HEARTBEAT_CLIENT_IP); + if (StringUtil.isBlank(ip)) { + ip = HostNameUtil.getIp(); + } + return ip; + } } diff --git a/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java new file mode 100644 index 00000000..d35c77fa --- /dev/null +++ b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java @@ -0,0 +1,25 @@ +package com.alibaba.csp.sentinel.transport.config; + +import com.alibaba.csp.sentinel.config.SentinelConfig; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TransportConfigTest { + + @Test + public void getClientIp() { + //config heartbeat client ip + System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, "10.10.10.10"); + String ip = TransportConfig.getHeartbeatClientIp(); + + assertNotNull(ip); + assertEquals(ip, "10.10.10.10"); + + //no heartbeat client ip + SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, ""); + ip = TransportConfig.getHeartbeatClientIp(); + assertNotNull(ip); + + } +} \ No newline at end of file diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index f92e4744..090cd09b 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -85,7 +85,7 @@ public class HttpHeartbeatSender implements HeartbeatSender { .setParameter("v", Constants.SENTINEL_VERSION) .setParameter("version", String.valueOf(System.currentTimeMillis())) .setParameter("hostname", HostNameUtil.getHostName()) - .setParameter("ip", HostNameUtil.getIp()) + .setParameter("ip", TransportConfig.getHeartbeatClientIp()) .setParameter("port", TransportConfig.getPort()) .setParameter("pid", String.valueOf(PidUtil.getPid())); diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java index 8292c7a3..5695632c 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java @@ -36,7 +36,7 @@ public class HeartbeatMessage { public HeartbeatMessage() { message.put("hostname", HostNameUtil.getHostName()); - message.put("ip", HostNameUtil.getIp()); + message.put("ip", TransportConfig.getHeartbeatClientIp()); message.put("app", AppNameUtil.getAppName()); message.put("port", String.valueOf(TransportConfig.getPort())); }