Search in sources :

Example 1 with HeartbeatSender

use of com.alibaba.csp.sentinel.transport.HeartbeatSender in project Sentinel by alibaba.

the class HeartbeatSenderProvider method resolveInstance.

private static void resolveInstance() {
    HeartbeatSender resolved = SpiLoader.of(HeartbeatSender.class).loadHighestPriorityInstance();
    if (resolved == null) {
        RecordLog.warn("[HeartbeatSenderProvider] WARN: No existing HeartbeatSender found");
    } else {
        heartbeatSender = resolved;
        RecordLog.info("[HeartbeatSenderProvider] HeartbeatSender activated: {}", resolved.getClass().getCanonicalName());
    }
}
Also used : HeartbeatSender(com.alibaba.csp.sentinel.transport.HeartbeatSender)

Example 2 with HeartbeatSender

use of com.alibaba.csp.sentinel.transport.HeartbeatSender in project Sentinel by alibaba.

the class HeartbeatSenderInitFunc method init.

@Override
public void init() {
    HeartbeatSender sender = HeartbeatSenderProvider.getHeartbeatSender();
    if (sender == null) {
        RecordLog.warn("[HeartbeatSenderInitFunc] WARN: No HeartbeatSender loaded");
        return;
    }
    initSchedulerIfNeeded();
    long interval = retrieveInterval(sender);
    setIntervalIfNotExists(interval);
    scheduleHeartbeatTask(sender, interval);
}
Also used : HeartbeatSender(com.alibaba.csp.sentinel.transport.HeartbeatSender)

Example 3 with HeartbeatSender

use of com.alibaba.csp.sentinel.transport.HeartbeatSender 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 HeartbeatSender

use of com.alibaba.csp.sentinel.transport.HeartbeatSender in project mogu_blog_v2 by moxi624.

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<Tuple2<String, Integer>> consoleServer = TransportConfig.getConsoleServerList();
    if (consoleServer == null) {
        // 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.DOWN.getCode(), consoleServer + " can't be connected"));
        }
    }
    // 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.DOWN.getCode(), e.getMessage()));
        }
    }
    // If Dashboard and DataSource are both OK, the health status is UP
    if (dashboardUp && dataSourceUp) {
        builder.up().withDetails(detailMap);
    } else {
        builder.down().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) Tuple2(com.alibaba.csp.sentinel.util.function.Tuple2) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with HeartbeatSender

use of com.alibaba.csp.sentinel.transport.HeartbeatSender in project spring-cloud-alibaba by alibaba.

the class SentinelHealthIndicatorTests method setUp.

@Before
public void setUp() {
    beanFactory = mock(DefaultListableBeanFactory.class);
    sentinelProperties = mock(SentinelProperties.class);
    sentinelHealthIndicator = new SentinelHealthIndicator(beanFactory, sentinelProperties);
    SentinelConfig.setConfig(TransportConfig.CONSOLE_SERVER, "");
    heartbeatSender = mock(HeartbeatSender.class);
    Field heartbeatSenderField = ReflectionUtils.findField(HeartbeatSenderProvider.class, "heartbeatSender");
    heartbeatSenderField.setAccessible(true);
    ReflectionUtils.setField(heartbeatSenderField, null, heartbeatSender);
}
Also used : Field(java.lang.reflect.Field) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) SentinelProperties(com.alibaba.cloud.sentinel.SentinelProperties) HeartbeatSender(com.alibaba.csp.sentinel.transport.HeartbeatSender) Before(org.junit.Before)

Aggregations

HeartbeatSender (com.alibaba.csp.sentinel.transport.HeartbeatSender)6 AbstractDataSource (com.alibaba.csp.sentinel.datasource.AbstractDataSource)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Status (org.springframework.boot.actuate.health.Status)2 SentinelProperties (com.alibaba.cloud.sentinel.SentinelProperties)1 Endpoint (com.alibaba.csp.sentinel.transport.endpoint.Endpoint)1 Tuple2 (com.alibaba.csp.sentinel.util.function.Tuple2)1 Field (java.lang.reflect.Field)1 Before (org.junit.Before)1 Test (org.junit.Test)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1