netflix程序内部数据监控

编程

JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。 

JMX最常见的场景是监控Java程序的基本信息和运行情况,任何Java程序都可以开启JMX,然后使用JConsole或Visual VM进行预览。

标准的MBean :   定义 XyMBean接口,Xy 类实现 XyMBean接口 。

public interface XyMBean {

String getStatus();

void setStatus(String status);

}

public class Xy implements XyMBean{

private String status = "default" ;

public String getStatus() {

return this.status;

}

public void setStatus(String status) {

this.status = status ;

}

}

注册MBean :

public class Main {

public static void main(String[] args) throws Exception{

XyMBean bean = new Xy() ;

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

ObjectName name = new ObjectName("com.lzf.study:type=String");

mbs.registerMBean(bean, name);

Thread.sleep(Integer.MAX_VALUE);

}

}

通过jconsole管理MBean 。


 

 

如果某些内部运行数据需要被外部知晓,则可以考虑MBean 。

 

以上玩法简单,但是限制太多,一是需要预先定义接口,二是每个地方都得命名,手动注册 。  下面介绍一个开箱即用的MBean工具箱。 

(官方推荐使用Spectator  !!! )

<dependencies>

<dependency>

<groupId>com.netflix.servo</groupId>

<artifactId>servo-core</artifactId>

<version>0.12.7</version>

</dependency>

</dependencies>

关于 servo 的官方介绍

Servo provides a simple interface for exposing and publishing application metrics in Java. The primary goals are:

  • Leverage JMX: JMX is the standard monitoring interface for Java and can be queried by many existing tools.
  • Keep It Simple: It should be trivial to expose metrics and publish metrics without having to write lots of code such as MBean interfaces.
  • Flexible Publishing: Once metrics are exposed, it should be easy to regularly poll the metrics and make them available for internal reporting systems, logs, and services like Amazon CloudWatch.

This has already been implemented inside of Netflix and most of our applications currently use it.

 

 

servo  基于  DynamicMBean

用法:  

public class Server {

@Monitor(name="Status", type=INFORMATIONAL)

private AtomicReference<String> status = new AtomicReference<String>("UP");

@Monitor(name="CurrentConnections", type=GAUGE)

private AtomicInteger currentConnections = new AtomicInteger(0);

@Monitor(name="TotalConnections", type=COUNTER)

private AtomicInteger totalConnections = new AtomicInteger(0);

@Monitor(name="BytesIn", type=COUNTER)

private AtomicLong bytesIn = new AtomicLong(0L);

@Monitor(name="BytesOut", type=COUNTER)

private AtomicLong bytesOut = new AtomicLong(0L);

private volatile Counter counter = Monitors.newCounter("LoadBalancer_ChooseServer");

}

 

1: 使用@Monitor  注解, 修饰成员变量或者成员方法。     注意成员方法不能是IO密集或者计算密集的方法 。

2:使用内置的 Monitor

注册: 

Server s1 = new Server();

Monitors.registerObject("s1", s1);

Server s2 = new Server();

Monitors.registerObject("s2", s2);

Thread.sleep(Integer.MAX_VALUE);

以上技巧在 com.netflix.*   中大量使用

 

通过jconsole 便能获取内部的运行状态了

 

除了支持标准的JMX 以外 , servo  还提供了自己的 publish 方案,这意味着可以通过jconsole 以外的工具连接 servo 。

 

 

 

 

 

以上是 netflix程序内部数据监控 的全部内容, 来源链接: utcz.com/z/518469.html

回到顶部