Browse Source

Improve logic for configuring the heartbeat API path in TransportConfig

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 years ago
parent
commit
9d02e84674
3 changed files with 16 additions and 7 deletions
  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 View File

@@ -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() { public static String getHeartbeatApiPath() {
String apiPath = SentinelConfig.getConfig(HEARTBEAT_API_PATH); String apiPath = SentinelConfig.getConfig(HEARTBEAT_API_PATH);
if (StringUtil.isBlank(apiPath)) { if (StringUtil.isBlank(apiPath)) {
return HEARTBEAT_DEFAULT_PATH; 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 View File

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


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

@@ -73,7 +73,7 @@ public class SimpleHttpHeartbeatSender implements HeartbeatSender {
return true; return true;
} }
} catch (Exception e) { } 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; return false;
} }


Loading…
Cancel
Save