use of com.alibaba.csp.sentinel.transport.endpoint.Endpoint in project spring-cloud-alibaba by alibaba.
the class SentinelAutoConfigurationTests method testSentinelSystemProperties.
@Test
public void testSentinelSystemProperties() {
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
assertThat(TransportConfig.getConsoleServerList().toString()).isEqualTo(Arrays.asList(new Endpoint(Protocol.HTTP, "localhost", 8080), new Endpoint(Protocol.HTTP, "localhost", 8081)).toString());
assertThat(TransportConfig.getPort()).isEqualTo("9999");
assertThat(TransportConfig.getHeartbeatIntervalMs().longValue()).isEqualTo(20000L);
assertThat(TransportConfig.getHeartbeatClientIp()).isEqualTo("1.1.1.1");
assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999);
assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100);
assertThat(SentinelConfig.charset()).isEqualTo("UTF-8");
assertThat(SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY)).isEqualTo("/error");
}
use of com.alibaba.csp.sentinel.transport.endpoint.Endpoint in project spring-cloud-alibaba by alibaba.
the class SentinelAutoConfigurationTests method checkEndpoint.
private void checkEndpoint() {
SentinelEndpoint sentinelEndpoint = new SentinelEndpoint(sentinelProperties);
Map<String, Object> map = sentinelEndpoint.invoke();
assertThat(map.get("logUsePid")).isEqualTo(Boolean.TRUE);
assertThat(map.get("consoleServer").toString()).isEqualTo(Arrays.asList(new Endpoint(Protocol.HTTP, "localhost", 8080), new Endpoint(Protocol.HTTP, "localhost", 8081)).toString());
assertThat(map.get("clientPort")).isEqualTo("9999");
assertThat(map.get("heartbeatIntervalMs")).isEqualTo(20000L);
assertThat(map.get("clientIp")).isEqualTo("1.1.1.1");
assertThat(map.get("metricsFileSize")).isEqualTo(9999L);
assertThat(map.get("totalMetricsFileCount")).isEqualTo(100);
assertThat(map.get("metricsFileCharset")).isEqualTo("UTF-8");
assertThat(map.get("blockPage")).isEqualTo("/error");
}
use of com.alibaba.csp.sentinel.transport.endpoint.Endpoint in project spring-cloud-alibaba by alibaba.
the class SentinelHealthIndicator method doHealthCheck.
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
Map<String, Object> detailMap = new HashMap<>();
// detail
if (!sentinelProperties.isEnabled()) {
detailMap.put("enabled", false);
builder.up().withDetails(detailMap);
return;
}
detailMap.put("enabled", true);
// Check health of Dashboard
boolean dashboardUp = true;
List<Endpoint> consoleServerList = TransportConfig.getConsoleServerList();
if (CollectionUtils.isEmpty(consoleServerList)) {
// If Dashboard isn't configured, it's OK and mark the status of Dashboard
// with UNKNOWN.
detailMap.put("dashboard", new Status(Status.UNKNOWN.getCode(), "dashboard isn't configured"));
} else {
// If Dashboard is configured, send a heartbeat message to it and check the
// result
HeartbeatSender heartbeatSender = HeartbeatSenderProvider.getHeartbeatSender();
boolean result = heartbeatSender.sendHeartbeat();
if (result) {
detailMap.put("dashboard", Status.UP);
} else {
// If failed to send heartbeat message, means that the Dashboard is DOWN
dashboardUp = false;
detailMap.put("dashboard", new Status(Status.UNKNOWN.getCode(), String.format("the dashboard servers [%s] one of them can't be connected", consoleServerList)));
}
}
// Check health of DataSource
boolean dataSourceUp = true;
Map<String, Object> dataSourceDetailMap = new HashMap<>();
detailMap.put("dataSource", dataSourceDetailMap);
// Get all DataSources and each call loadConfig to check if it's OK
// If no Exception thrown, it's OK
// Note:
// Even if the dynamic config center is down, the loadConfig() might return
// successfully
// e.g. for Nacos client, it might retrieve from the local cache)
// But in most circumstances it's okay
Map<String, AbstractDataSource> dataSourceMap = beanFactory.getBeansOfType(AbstractDataSource.class);
for (Map.Entry<String, AbstractDataSource> dataSourceMapEntry : dataSourceMap.entrySet()) {
String dataSourceBeanName = dataSourceMapEntry.getKey();
AbstractDataSource dataSource = dataSourceMapEntry.getValue();
try {
dataSource.loadConfig();
dataSourceDetailMap.put(dataSourceBeanName, Status.UP);
} catch (Exception e) {
// If one DataSource failed to loadConfig, means that the DataSource is
// DOWN
dataSourceUp = false;
dataSourceDetailMap.put(dataSourceBeanName, new Status(Status.UNKNOWN.getCode(), e.getMessage()));
}
}
// If Dashboard and DataSource are both OK, the health status is UP
if (dashboardUp && dataSourceUp) {
builder.up().withDetails(detailMap);
} else {
builder.unknown().withDetails(detailMap);
}
}
use of com.alibaba.csp.sentinel.transport.endpoint.Endpoint in project Sentinel by alibaba.
the class SimpleHttpHeartbeatSender method sendHeartbeat.
@Override
public boolean sendHeartbeat() throws Exception {
if (TransportConfig.getRuntimePort() <= 0) {
RecordLog.info("[SimpleHttpHeartbeatSender] Command server port not initialized, won't send heartbeat");
return false;
}
Endpoint addrInfo = getAvailableAddress();
if (addrInfo == null) {
return false;
}
SimpleHttpRequest request = new SimpleHttpRequest(addrInfo, TransportConfig.getHeartbeatApiPath());
request.setParams(heartBeat.generateCurrentMessage());
try {
SimpleHttpResponse response = httpClient.post(request);
if (response.getStatusCode() == OK_STATUS) {
return true;
} else if (clientErrorCode(response.getStatusCode()) || serverErrorCode(response.getStatusCode())) {
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addrInfo + ", http status code: " + response.getStatusCode());
}
} catch (Exception e) {
RecordLog.warn("[SimpleHttpHeartbeatSender] Failed to send heartbeat to " + addrInfo, e);
}
return false;
}
use of com.alibaba.csp.sentinel.transport.endpoint.Endpoint in project Sentinel by alibaba.
the class TransportConfig method getConsoleServerList.
/**
* Get a list of Endpoint(protocol, ip/domain, port) indicating Sentinel Dashboard's address.<br>
* NOTE: only support <b>HTTP</b> and <b>HTTPS</b> protocol
*
* @return list of Endpoint(protocol, ip/domain, port). <br>
* <b>May not be null</b>. <br>
* An empty list returned when not configured.
*/
public static List<Endpoint> getConsoleServerList() {
String config = SentinelConfig.getConfig(CONSOLE_SERVER);
List<Endpoint> list = new ArrayList<Endpoint>();
if (StringUtil.isBlank(config)) {
return list;
}
int pos = -1;
int cur = 0;
while (true) {
pos = config.indexOf(',', cur);
if (cur < config.length() - 1 && pos < 0) {
// for single segment, pos move to the end
pos = config.length();
}
if (pos < 0) {
break;
}
if (pos <= cur) {
cur++;
continue;
}
// parsing
String ipPortStr = config.substring(cur, pos);
cur = pos + 1;
if (StringUtil.isBlank(ipPortStr)) {
continue;
}
ipPortStr = ipPortStr.trim();
int port = 80;
Protocol protocol = Protocol.HTTP;
if (ipPortStr.startsWith("http://")) {
ipPortStr = ipPortStr.substring(7);
} else if (ipPortStr.startsWith("https://")) {
ipPortStr = ipPortStr.substring(8);
port = 443;
protocol = Protocol.HTTPS;
}
int index = ipPortStr.indexOf(":");
if (index == 0) {
// skip
continue;
}
String host = ipPortStr;
if (index >= 0) {
try {
port = Integer.parseInt(ipPortStr.substring(index + 1));
if (port <= 1 || port >= 65535) {
throw new RuntimeException("Port number [" + port + "] over range");
}
} catch (Exception e) {
RecordLog.warn("Parse port of dashboard server failed: " + ipPortStr, e);
// skip
continue;
}
host = ipPortStr.substring(0, index);
}
list.add(new Endpoint(protocol, host, port));
}
return list;
}
Aggregations