Improve logic for configuring the heartbeat API path in TransportConfig

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-12-10 10:54:41 +08:00
parent 9beb655b42
commit 9d02e84674
3 changed files with 16 additions and 7 deletions

View File

@ -99,16 +99,20 @@ public class TransportConfig {
} }
/** /**
* Get dashboard heartbeat api path. * Get the heartbeat api path. If the machine registry path of the dashboard
* If the context path not configured,it will be the default api path * 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;
} }
} }

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

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;
} }