聊聊dubbo的StatusChecker

本文主要研究一下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

小结

  • StatusChecker定义了check接口,返回Status;Status定义了三个属性,分别是level、message、description;其中level有OK、WARN、ERROR、UNKNOWN四个级别
  • LoadStatusChecker实现了StatusChecker接口,其check方法从operatingSystemMXBean的getSystemLoadAverage方法读取load,然后根据cpu个数来计算load
  • MemoryStatusChecker实现了实现了StatusChecker接口,其check方法从Runtime获取freeMemory、totalMemory、maxMemory,当maxMemory - (totalMemory - freeMemory) > 2048返回OK,否则返回WARN

doc

转载于:https://my.oschina.net/go4it/blog/3065121

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值