Browse Source

Add heartbeat client ip config to resolve ip problem in Docker env (#261)

master
nick.tan Eric Zhao 6 years ago
parent
commit
eb29cc929f
4 changed files with 44 additions and 2 deletions
  1. +17
    -0
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java
  2. +25
    -0
      sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java
  3. +1
    -1
      sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java
  4. +1
    -1
      sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java

+ 17
- 0
sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java View File

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

+ 25
- 0
sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java View File

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

}
}

+ 1
- 1
sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java View File

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



+ 1
- 1
sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HeartbeatMessage.java View File

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


Loading…
Cancel
Save