@@ -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; | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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())); | |||
@@ -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())); | |||
} | |||