Search in sources :

Example 1 with StatusChecker

use of org.apache.dubbo.common.status.StatusChecker in project dubbo by alibaba.

the class DubboHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    ExtensionLoader<StatusChecker> extensionLoader = getExtensionLoader(StatusChecker.class);
    Map<String, String> statusCheckerNamesMap = resolveStatusCheckerNamesMap();
    boolean hasError = false;
    boolean hasUnknown = false;
    // Up first
    builder.up();
    for (Map.Entry<String, String> entry : statusCheckerNamesMap.entrySet()) {
        String statusCheckerName = entry.getKey();
        String source = entry.getValue();
        StatusChecker checker = extensionLoader.getExtension(statusCheckerName);
        org.apache.dubbo.common.status.Status status = checker.check();
        org.apache.dubbo.common.status.Status.Level level = status.getLevel();
        if (!hasError && level.equals(org.apache.dubbo.common.status.Status.Level.ERROR)) {
            hasError = true;
            builder.down();
        }
        if (!hasError && !hasUnknown && level.equals(org.apache.dubbo.common.status.Status.Level.UNKNOWN)) {
            hasUnknown = true;
            builder.unknown();
        }
        Map<String, Object> detail = new LinkedHashMap<>();
        detail.put("source", source);
        detail.put("status", status);
        builder.withDetail(statusCheckerName, detail);
    }
}
Also used : StatusChecker(org.apache.dubbo.common.status.StatusChecker) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with StatusChecker

use of org.apache.dubbo.common.status.StatusChecker in project dubbo by alibaba.

the class ServerStatusCheckerTest method testServerStatusChecker.

@Test
public void testServerStatusChecker() throws Exception {
    int port = NetUtils.getAvailablePort(7000);
    URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName());
    DemoService service = new DemoServiceImpl();
    DubboProtocol.getDubboProtocol().export(proxy.getInvoker(service, DemoService.class, url));
    StatusChecker server = ExtensionLoader.getExtensionLoader(StatusChecker.class).getExtension("server");
    Assertions.assertEquals(ServerStatusChecker.class, server.getClass());
    Status status = server.check();
    Assertions.assertEquals(Status.Level.OK, status.getLevel());
    ProtocolUtils.closeAll();
}
Also used : Status(org.apache.dubbo.common.status.Status) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) StatusChecker(org.apache.dubbo.common.status.StatusChecker) URL(org.apache.dubbo.common.URL) DemoServiceImpl(org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 3 with StatusChecker

use of org.apache.dubbo.common.status.StatusChecker in project dubbo by alibaba.

the class StatusTelnetHandler method telnet.

@Override
public String telnet(Channel channel, String message) {
    if ("-l".equals(message)) {
        List<StatusChecker> checkers = extensionLoader.getActivateExtension(channel.getUrl(), STATUS_KEY);
        String[] header = new String[] { "resource", "status", "message" };
        List<List<String>> table = new ArrayList<List<String>>();
        Map<String, Status> statuses = new HashMap<String, Status>();
        if (CollectionUtils.isNotEmpty(checkers)) {
            for (StatusChecker checker : checkers) {
                String name = extensionLoader.getExtensionName(checker);
                Status stat;
                try {
                    stat = checker.check();
                } catch (Throwable t) {
                    stat = new Status(Status.Level.ERROR, t.getMessage());
                }
                statuses.put(name, stat);
                if (stat.getLevel() != null && stat.getLevel() != Status.Level.UNKNOWN) {
                    List<String> row = new ArrayList<String>();
                    row.add(name);
                    row.add(String.valueOf(stat.getLevel()));
                    row.add(stat.getMessage() == null ? "" : stat.getMessage());
                    table.add(row);
                }
            }
        }
        Status stat = StatusUtils.getSummaryStatus(statuses);
        List<String> row = new ArrayList<String>();
        row.add("summary");
        row.add(String.valueOf(stat.getLevel()));
        row.add(stat.getMessage());
        table.add(row);
        return TelnetUtils.toTable(header, table);
    } else if (message.length() > 0) {
        return "Unsupported parameter " + message + " for status.";
    }
    String status = channel.getUrl().getParameter(STATUS_KEY);
    Map<String, Status> statuses = new HashMap<String, Status>();
    if (StringUtils.isNotEmpty(status)) {
        String[] ss = COMMA_SPLIT_PATTERN.split(status);
        for (String s : ss) {
            StatusChecker handler = extensionLoader.getExtension(s);
            Status stat;
            try {
                stat = handler.check();
            } catch (Throwable t) {
                stat = new Status(Status.Level.ERROR, t.getMessage());
            }
            statuses.put(s, stat);
        }
    }
    Status stat = StatusUtils.getSummaryStatus(statuses);
    return String.valueOf(stat.getLevel());
}
Also used : Status(org.apache.dubbo.common.status.Status) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) StatusChecker(org.apache.dubbo.common.status.StatusChecker)

Example 4 with StatusChecker

use of org.apache.dubbo.common.status.StatusChecker in project incubator-dubbo-spring-boot-project by apache.

the class DubboHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    ExtensionLoader<StatusChecker> extensionLoader = getExtensionLoader(StatusChecker.class);
    Map<String, String> statusCheckerNamesMap = resolveStatusCheckerNamesMap();
    boolean hasError = false;
    boolean hasUnknown = false;
    // Up first
    builder.up();
    for (Map.Entry<String, String> entry : statusCheckerNamesMap.entrySet()) {
        String statusCheckerName = entry.getKey();
        String source = entry.getValue();
        StatusChecker checker = extensionLoader.getExtension(statusCheckerName);
        org.apache.dubbo.common.status.Status status = checker.check();
        org.apache.dubbo.common.status.Status.Level level = status.getLevel();
        if (!hasError && level.equals(org.apache.dubbo.common.status.Status.Level.ERROR)) {
            hasError = true;
            builder.down();
        }
        if (!hasError && !hasUnknown && level.equals(org.apache.dubbo.common.status.Status.Level.UNKNOWN)) {
            hasUnknown = true;
            builder.unknown();
        }
        Map<String, Object> detail = new LinkedHashMap<>();
        detail.put("source", source);
        detail.put("status", status);
        builder.withDetail(statusCheckerName, detail);
    }
}
Also used : StatusChecker(org.apache.dubbo.common.status.StatusChecker) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

StatusChecker (org.apache.dubbo.common.status.StatusChecker)4 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Status (org.apache.dubbo.common.status.Status)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 URL (org.apache.dubbo.common.URL)1 DemoService (org.apache.dubbo.rpc.protocol.dubbo.support.DemoService)1 DemoServiceImpl (org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl)1 Test (org.junit.jupiter.api.Test)1