瀏覽代碼

Improve logic for configuring the heartbeat API path in TransportConfig

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 年之前
父節點
當前提交
9d02e84674
共有 3 個文件被更改,包括 16 次插入7 次删除
  1. +8
    -4
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java
  2. +7
    -2
      sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java
  3. +1
    -1
      sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java

+ 8
- 4
sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/config/TransportConfig.java 查看文件

@@ -99,16 +99,20 @@ public class TransportConfig {
}

/**
* Get dashboard heartbeat api path.
* If the context path not configured,it will be the default api path
* Get the heartbeat api path. If the machine registry path of the dashboard
* is modified, then the API path should also be consistent with the API path of the dashboard.
*
* @return api path.
* @return the heartbeat api path
* @since 1.7.1
*/
public static String getHeartbeatApiPath() {
String apiPath = SentinelConfig.getConfig(HEARTBEAT_API_PATH);
if (StringUtil.isBlank(apiPath)) {
return HEARTBEAT_DEFAULT_PATH;
}
return apiPath + HEARTBEAT_DEFAULT_PATH;
if (!apiPath.startsWith("/")) {
apiPath = "/" + apiPath;
}
return apiPath;
}
}

+ 7
- 2
sentinel-transport/sentinel-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/config/TransportConfigTest.java 查看文件

@@ -66,7 +66,7 @@ public class TransportConfigTest {
}

@Test
public void getHeartbeatApiPath() {
public void testGetHeartbeatApiPath() {
// use default heartbeat api path
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());
@@ -74,7 +74,12 @@ public class TransportConfigTest {
// config heartbeat api path
SentinelConfig.setConfig(TransportConfig.HEARTBEAT_API_PATH, "/demo");
assertTrue(StringUtil.isNotEmpty(TransportConfig.getHeartbeatApiPath()));
assertEquals("/demo" + TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());
assertEquals("/demo", TransportConfig.getHeartbeatApiPath());

SentinelConfig.setConfig(TransportConfig.HEARTBEAT_API_PATH, "demo/registry");
assertEquals("/demo/registry", TransportConfig.getHeartbeatApiPath());

SentinelConfig.removeConfig(TransportConfig.HEARTBEAT_API_PATH);
assertEquals(TransportConfig.HEARTBEAT_DEFAULT_PATH, TransportConfig.getHeartbeatApiPath());
}
}

+ 1
- 1
sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/heartbeat/SimpleHttpHeartbeatSender.java 查看文件

@@ -73,7 +73,7 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
return true;
}
} catch (Exception e) {
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr + " : ", e);
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addr, e);
}
return false;
}


Loading…
取消
儲存