Skip to content

Commit

Permalink
Support configuration for the heartbeat API path in transport module (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-tan authored and sczyh30 committed Dec 10, 2019
1 parent 49861f4 commit 9beb655
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -94,4 +97,18 @@ public static String getHeartbeatClientIp() {
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,17 @@ public void testGetHeartbeatClientIp() {
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());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public boolean sendHeartbeat() throws Exception {
}
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
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;
Expand Down Expand Up @@ -66,7 +65,7 @@ public boolean sendHeartbeat() throws Exception {
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);
Expand Down

0 comments on commit 9beb655

Please sign in to comment.