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