APP下载
报价宝  ›  科技  › 

聊聊dubbo的StatusChecker

报价宝 来源:baojiabao.com 发布时间:2019-10-17 23:53:00 11月14日更新
报价宝综合消息聊聊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界面,返回StatusLoadStatusChecker

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 (load }

​

}

LoadStatusChecker实现了StatusChecker界面,其check方法从operatingSystemMXBean的getSystemLoadAverage方法读取load,然后根据cpu个数来计算loadMemoryStatusChecker

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 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个数来计算loadMemoryStatusChecker实现了实现了StatusChecker界面,其check方法从Runtime获取freeMemory、totalMemory、maxMemory,当maxMemory - (totalMemory - freeMemory) > 2048返回OK,否则返回WARNdoc

StatusCheckerLoadStatusCheckerMemoryStatusChecker

文章标签: 报价宝 降噪耳机价格 耳机价格 红米手机价格 华为手机价格 小米手机价格 电视机价格 笔记本电脑价格 笔记本价格 汽车价格 报价宝 手机价格 汽车价格 电视机价格 耳机价格