Browse Source

Support configuration for the heartbeat API path in transport module (#515)

master
nick.tan Eric Zhao 5 years ago
parent
commit
9beb655b42
4 changed files with 32 additions and 3 deletions
  1. +17
    -0
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java
  2. +13
    -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
    -2
      sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java

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

@@ -29,6 +29,9 @@ public class TransportConfig {
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";
public static final String HEARTBEAT_API_PATH = "csp.sentinel.heartbeat.api.path";

public static final String HEARTBEAT_DEFAULT_PATH = "/registry/machine";

private static int runtimePort = -1;

@@ -94,4 +97,18 @@ public class TransportConfig {
}
return ip;
}

/**
* Get dashboard heartbeat api path.
* If the context path not configured,it will be the default api path
*
* @return api path.
*/
public static String getHeartbeatApiPath() {
String apiPath = SentinelConfig.getConfig(HEARTBEAT_API_PATH);
if (StringUtil.isBlank(apiPath)) {
return HEARTBEAT_DEFAULT_PATH;
}
return apiPath + HEARTBEAT_DEFAULT_PATH;
}
}

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

@@ -64,4 +64,17 @@ public class TransportConfigTest {
SentinelConfig.setConfig(TransportConfig.HEARTBEAT_CLIENT_IP, "");
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatClientIp()));
}

@Test
public void getHeartbeatApiPath() {
// use default heartbeat api path
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());

// config heartbeat api path
SentinelConfig.setConfig(TransportConfig.HEARTBEAT_API_PATH, "/demo");
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
assertEquals("/demo" + TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());

}
}

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

@@ -109,7 +109,7 @@ public class HttpHeartbeatSender implements HeartbeatSender {
}
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort)
.setPath("/registry/machine")
.setPath(TransportConfig.getHeartbeatApiPath())
.setParameter("app", AppNameUtil.getAppName())
.setParameter("app_type", String.valueOf(SentinelConfig.getAppType()))
.setParameter("v", Constants.SENTINEL_VERSION)


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

@@ -36,7 +36,6 @@ import com.alibaba.csp.sentinel.util.StringUtil;
*/
public class SimpleHttpHeartbeatSender implements HeartbeatSender {

private static final String HEARTBEAT_PATH = "/registry/machine";
private static final int OK_STATUS = 200;

private static final long DEFAULT_INTERVAL = 1000 * 10;
@@ -66,7 +65,7 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
return false;
}

SimpleHttpRequest request = new SimpleHttpRequest(addr, HEARTBEAT_PATH);
SimpleHttpRequest request = new SimpleHttpRequest(addr, TransportConfig.getHeartbeatApiPath());
request.setParams(heartBeat.generateCurrentMessage());
try {
SimpleHttpResponse response = httpClient.post(request);


Loading…
Cancel
Save