聊聊dubbo的StatusChecker

栏目: Java · 发布时间: 6年前

内容简介:本文主要研究一下dubbo的StatusCheckerdubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/Status.javadubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/StatusChecker.java

本文主要研究一下dubbo的StatusChecker

Status

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/Status.java

public class Status {

    private final Level level;
    private final String message;
    private final String description;

    public Status(Level level) {
        this(level, null, null);
    }

    public Status(Level level, String message) {
        this(level, message, null);
    }

    public Status(Level level, String message, String description) {
        this.level = level;
        this.message = message;
        this.description = description;
    }

    public Level getLevel() {
        return level;
    }

    public String getMessage() {
        return message;
    }

    public String getDescription() {
        return description;
    }

    /**
     * Level
     */
    public enum Level {
        /**
         * OK
         */
        OK,

        /**
         * WARN
         */
        WARN,

        /**
         * ERROR
         */
        ERROR,

        /**
         * UNKNOWN
         */
        UNKNOWN
    }

}
  • Status定义了三个属性,分别是level、message、description;其中level有OK、WARN、ERROR、UNKNOWN四个级别

StatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/StatusChecker.java

@SPI
public interface StatusChecker {

    /**
     * check status
     *
     * @return status
     */
    Status check();

}
  • StatusChecker定义了check接口,返回Status

LoadStatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/support/LoadStatusChecker.java

@Activate
public class LoadStatusChecker implements StatusChecker {

    @Override
    public Status check() {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        double load;
        try {
            Method method = OperatingSystemMXBean.class.getMethod("getSystemLoadAverage", new Class<?>[0]);
            load = (Double) method.invoke(operatingSystemMXBean, new Object[0]);
            if (load == -1) {
                com.sun.management.OperatingSystemMXBean bean =
                        (com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;
                load = bean.getSystemCpuLoad();
            }
        } catch (Throwable e) {
            load = -1;
        }
        int cpu = operatingSystemMXBean.getAvailableProcessors();
        return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN),
                (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
    }

}
  • LoadStatusChecker实现了StatusChecker接口,其check方法从operatingSystemMXBean的getSystemLoadAverage方法读取load,然后根据cpu个数来计算load

MemoryStatusChecker

dubbo-2.7.2/dubbo-common/src/main/java/org/apache/dubbo/common/status/support/MemoryStatusChecker.java

@Activate
public class MemoryStatusChecker implements StatusChecker {

    @Override
    public Status check() {
        Runtime runtime = Runtime.getRuntime();
        long freeMemory = runtime.freeMemory();
        long totalMemory = runtime.totalMemory();
        long maxMemory = runtime.maxMemory();
        boolean ok = (maxMemory - (totalMemory - freeMemory) > 2048); // Alarm when spare memory < 2M
        String msg = "max:" + (maxMemory / 1024 / 1024) + "M,total:"
                + (totalMemory / 1024 / 1024) + "M,used:" + ((totalMemory / 1024 / 1024) - (freeMemory / 1024 / 1024)) + "M,free:" + (freeMemory / 1024 / 1024) + "M";
        return new Status(ok ? Status.Level.OK : Status.Level.WARN, msg);
    }

}
  • MemoryStatusChecker实现了实现了StatusChecker接口,其check方法从Runtime获取freeMemory、totalMemory、maxMemory,当 maxMemory - (totalMemory - freeMemory) > 2048 返回OK,否则返回WARN

小结

maxMemory - (totalMemory - freeMemory) > 2048

doc


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

解放战争(上)(1945年8月—1948年9月)

解放战争(上)(1945年8月—1948年9月)

王树增 / 人民文学出版社 / 2009-8 / 60.00

《解放战争》为王树增非虚构文学著述中规模最大的作品。武器简陋、兵力不足的军队对抗拥有现代武器装备的兵力庞大的军队,数量不多、面积有限的解放区最终扩展成为九百六十万平方公里的共和国,解放战争在短短四年时间里演绎的是人类历史上的战争传奇。国际风云,政治智慧,时事洞察,军事谋略,军队意志,作战才能,作品具有宏阔的视角和入微的体察,包含着惊心动魄的人生沉浮和变幻莫测的战场胜负,尽展中国历史上规模最大的一场......一起来看看 《解放战争(上)(1945年8月—1948年9月)》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具