Search in sources :

Example 1 with Endpoint

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");
}
Also used : Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) SentinelEndpoint(com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Endpoint

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");
}
Also used : Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) SentinelEndpoint(com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint) SentinelEndpoint(com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint)

Example 3 with Endpoint

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);
    }
}
Also used : Status(org.springframework.boot.actuate.health.Status) HashMap(java.util.HashMap) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource) HeartbeatSender(com.alibaba.csp.sentinel.transport.HeartbeatSender) Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with Endpoint

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;
}
Also used : Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) SimpleHttpRequest(com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest) SimpleHttpResponse(com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse)

Example 5 with Endpoint

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;
}
Also used : Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint) ArrayList(java.util.ArrayList) Protocol(com.alibaba.csp.sentinel.transport.endpoint.Protocol) Endpoint(com.alibaba.csp.sentinel.transport.endpoint.Endpoint)

Aggregations

Endpoint (com.alibaba.csp.sentinel.transport.endpoint.Endpoint)5 SentinelEndpoint (com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint)2 AbstractDataSource (com.alibaba.csp.sentinel.datasource.AbstractDataSource)1 HeartbeatSender (com.alibaba.csp.sentinel.transport.HeartbeatSender)1 Protocol (com.alibaba.csp.sentinel.transport.endpoint.Protocol)1 SimpleHttpRequest (com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpRequest)1 SimpleHttpResponse (com.alibaba.csp.sentinel.transport.heartbeat.client.SimpleHttpResponse)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 Status (org.springframework.boot.actuate.health.Status)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1