From 9beb655b421fa530fc11f88f16f8e5ab0f6c7523 Mon Sep 17 00:00:00 2001 From: "nick.tan" Date: Tue, 10 Dec 2019 10:25:00 +0800 Subject: [PATCH] Support configuration for the heartbeat API path in transport module (#515) --- .../transport/config/TransportConfig.java | 17 +++++++++++++++++ .../transport/config/TransportConfigTest.java | 13 +++++++++++++ .../heartbeat/HttpHeartbeatSender.java | 2 +- .../heartbeat/SimpleHttpHeartbeatSender.java | 3 +-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java index 84b53cbd..03914963 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java @@ -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; + } } diff --git a/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java index d77a88b7..c0ad8fa4 100644 --- a/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java +++ b/sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java @@ -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()); + + } } \ No newline at end of file diff --git a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java index e150f266..be5c7fa2 100755 --- a/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-netty-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/HttpHeartbeatSender.java @@ -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) diff --git a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java index aa4e571a..dbd4c160 100755 --- a/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java +++ b/sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java @@ -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);