赞
踩
就像我们操作系统里面的资源管理器一样,如果能够实时或者准实时的看到整个系统耗费的CPU,内存等资源,对我们快速对系统做出响应,以及优化很重要。同样,对于对外提供接口或者服务的WebService的监控,比如在哪个地方,哪台机器上,花了多少CPU,多少内存,每一个服务的响应时间,出错的次数频率等,这些信息记录下来之后,我们就可以看到服务在运行时的动态的表现,更加容易找出错误或者定位问题点来进行优化。
那么,最简单的做法是,在应用系统的关键地方,或者所有程序的入口,出口进行埋点,然后将这些采样信息不断的发送到某一个消息队列或者内存DB中,然后其他系统进行读取分析和展示。
之前谈到了AOP,它确实能够一定程度上解决你的这些问题。但是你要相信这个世界的轮子之多以及轮子之好,基本都会有集成好的东西来用的。
————————————————我是分割线——————————————
作为一款监控指标的度量类库,Metrics可以为你的代码的运行提供无与伦比的洞察力,它能够捕获JVM以及应用层面的性能参数,同时它提供了很多模块可以为第三方库或者应用提供辅助统计信息, 比如Jetty, Logback, Log4j, Apache HttpClient, Ehcache, JDBI, Jersey, 它还可以将度量数据发送给Ganglia和Graphite以提供图形化的监控。
1、将metrics-core加入到maven pom.xml中:
<dependencies>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.version}</version>
</dependency>
</dependencies>
2、core包核- 列表内容心功能
Gauge代表一个度量的即时值。 当你开汽车的时候, 当前速度是Gauge值。 你测体温的时候, 体温计的刻度是一个Gauge值。 当你的程序运行的时候, 内存使用量和CPU占用率都可以通过Gauge值来度量。或者你也可以理解为统计瞬时状态的数据信息。比如我们可以查看一个队列当前的size。
package com.netease.test.metrics;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
/**
* 测试Gauges,实时统计pending状态的job个数
*/
public class TestGauges {
/**
* 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
*/
private static final MetricRegistry metrics = new MetricRegistry();
private static Queue<String> queue = new LinkedBlockingDeque<String>();
/**
* 在控制台上打印输出
*/
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
public static void main(String[] args) throws InterruptedException {
reporter.start(3, TimeUnit.SECONDS);
//实例化一个Gauge
Gauge<Integer> gauge = new Gauge<Integer>() {
@Override
public Integer getValue() {
return queue.size();
}
};
//注册到容器中
metrics.register(MetricRegistry.name(TestGauges.class, "pending-job", "size"), gauge);
//测试JMX
JmxReporter jmxReporter = JmxReporter.forRegistry(metrics).build();
jmxReporter.start();
//模拟数据
for (int i=0; i<20; i++){
queue.add("a");
Thread.sleep(1000);
}
}
}
/*
console output:
14-2-17 15:29:35 ===============================================================
-- Gauges ----------------------------------------------------------------------
com.netease.test.metrics.TestGauges.pending-job.size
value = 4
14-2-17 15:29:38 ===============================================================
-- Gauges ----------------------------------------------------------------------
com.netease.test.metrics.TestGauges.pending-job.size
value = 6
14-2-17 15:29:41 ===============================================================
-- Gauges ----------------------------------------------------------------------
com.netease.test.metrics.TestGauges.pending-job.size
value = 9
*/
registry 中每一个metric都有唯一的名字。 MetricRegistry 提供了一个静态的辅助方法用来生成这个名字:
MetricRegistry.name(QueueManager.class, "pending-job", "size")
生成的name为com.netease.test.metrics.TestGauges.pending-job.size。
另外,Core包种还扩展了几种特定的Gauge:
如上面的代码中jmxReporter.start()被启动后, 所有registry中注册的metric都可以通过JConsole或者VisualVM查看。
———————————————我是小分割线,介绍下VisualVM———————————————
首次接触VisualVM,这里介绍一下VisualVM的简单使用,以及利用VisualVM查看metric变量。
首先需要在IDEA中进行如下配置:在你要做测试的类上,通过Edit Configurations配置好VM options:
-Dcom.sun.management.jmxremote.port=8088
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
接下来配置VisualVM。打开jdk安装目录下bin/jvisualvm.exe,首先配置MBean这样这一个插件。工具→插件→可用插件→MBeans→安装。
接下来配置jxm链接,与IDEA中配置的端口号保持一致。(注:这步一定要在程序跑起来之后,再配置,否则会连接失败)
接下里就可以通过MBean来查看你metrics中的值了。
但是遗憾的是并没有找到MBeans生成图表的功能,希望知道的大神指点一手。
Counter是一个AtomicLong实例,它维护一个计数器,可以通过inc()和dec()方法对计数器进行修改。使用步骤与Gauge基本类似,在MetricRegistry中提供了静态方法可以直接实例化一个Counter。
package com.netease.test.metrics;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import static com.codahale.metrics.MetricRegistry.*;
/**
* User: hzwangxx
* Date: 14-2-14
* Time: 14:02
* 测试Counter
*/
public class TestCounter {
/**
* 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
*/
private static final MetricRegistry metrics = new MetricRegistry();
/**
* 在控制台上打印输出
*/
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
/**
* 实例化一个counter,同样可以通过如下方式进行实例化再注册进去
* pendingJobs = new Counter();
* metrics.register(MetricRegistry.name(TestCounter.class, "pending-jobs"), pendingJobs);
*/
private static Counter pendingJobs = metrics.counter(name(TestCounter.class, "pedding-jobs"));
// private static Counter pendingJobs = metrics.counter(MetricRegistry.name(TestCounter.class, "pedding-jobs"));
private static Queue<String> queue = new LinkedList<String>();
public static void add(String str) {
pendingJobs.inc();
queue.offer(str);
}
public String take() {
pendingJobs.dec();
return queue.poll();
}
public static void main(String[]args) throws InterruptedException {
reporter.start(3, TimeUnit.SECONDS);
while(true){
add("1");
Thread.sleep(1000);
}
}
}
/*
console output:
14-2-17 17:52:34 ===============================================================
-- Counters --------------------------------------------------------------------
com.netease.test.metrics.TestCounter.pedding-jobs
count = 4
14-2-17 17:52:37 ===============================================================
-- Counters --------------------------------------------------------------------
com.netease.test.metrics.TestCounter.pedding-jobs
count = 6
14-2-17 17:52:40 ===============================================================
-- Counters --------------------------------------------------------------------
com.netease.test.metrics.TestCounter.pedding-jobs
count = 9
*/
Meters用来度量某个时间段的平均处理次数(request per second),每1、5、15分钟的TPS。比如一个service的请求数,通过metrics.meter()实例化一个Meter之后,然后通过meter.mark()方法就能将本次请求记录下来。统计结果有总的请求数,平均每秒的请求数,以及最近的1、5、15分钟的平均TPS。
package com.netease.test.metrics;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import java.util.concurrent.TimeUnit;
import static com.codahale.metrics.MetricRegistry.*;
/**
* Date: 14-2-17
* Time: 18:34
* 测试Meters
*/
public class TestMeters {
/**
* 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
*/
private static final MetricRegistry metrics = new MetricRegistry();
/**
* 在控制台上打印输出
*/
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
/**
* 实例化一个Meter
*/
private static final Meter requests = metrics.meter(name(TestMeters.class, "request"));
public static void handleRequest() {
requests.mark();
}
public static void main(String[] args) throws InterruptedException {
reporter.start(3, TimeUnit.SECONDS);
while(true){
handleRequest();
Thread.sleep(100);
}
}
}
/*
14-2-17 18:43:08 ===============================================================
-- Meters ----------------------------------------------------------------------
com.netease.test.metrics.TestMeters.request
count = 30
mean rate = 9.95 events/second
1-minute rate = 0.00 events/second
5-minute rate = 0.00 events/second
15-minute rate = 0.00 events/second
14-2-17 18:43:11 ===============================================================
-- Meters ----------------------------------------------------------------------
com.netease.test.metrics.TestMeters.request
count = 60
mean rate = 9.99 events/second
1-minute rate = 10.00 events/second
5-minute rate = 10.00 events/second
15-minute rate = 10.00 events/second
14-2-17 18:43:14 ===============================================================
-- Meters ----------------------------------------------------------------------
com.netease.test.metrics.TestMeters.request
count = 90
mean rate = 9.99 events/second
1-minute rate = 10.00 events/second
5-minute rate = 10.00 events/second
15-minute rate = 10.00 events/second
*/
Histograms主要使用来统计数据的分布情况,最大值、最小值、平均值、标准偏差、中位数,百分比(75%、90%、95%、98%、99%和99.9%)。例如,需要统计某个请求的参数值的分部情况,可以使用该种类型的Metrics进行统计。具体的样例代码如下:
package com.netease.test.metrics;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static com.codahale.metrics.MetricRegistry.name;
/**
* Date: 14-2-17
* Time: 18:34
* 测试Histograms
*/
public class TestHistograms {
/**
* 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
*/
private static final MetricRegistry metrics = new MetricRegistry();
/**
* 在控制台上打印输出
*/
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
/**
* 实例化一个Histograms
*/
private static final Histogram randomNums = metrics.histogram(name(TestHistograms.class, "random"));
public static void handleRequest(double random) {
randomNums.update((int) (random*100));
}
public static void main(String[] args) throws InterruptedException {
reporter.start(3, TimeUnit.SECONDS);
Random rand = new Random();
while(true){
handleRequest(rand.nextDouble());
Thread.sleep(100);
}
}
}
/*
14-2-17 19:39:11 ===============================================================
-- Histograms ------------------------------------------------------------------
com.netease.test.metrics.TestHistograms.random
count = 30
min = 1
max = 97
mean = 45.93
stddev = 29.12
median = 39.50
75% <= 71.00
95% <= 95.90
98% <= 97.00
99% <= 97.00
99.9% <= 97.00
14-2-17 19:39:14 ===============================================================
-- Histograms ------------------------------------------------------------------
com.netease.test.metrics.TestHistograms.random
count = 60
min = 0
max = 97
mean = 41.17
stddev = 28.60
median = 34.50
75% <= 69.75
95% <= 92.90
98% <= 96.56
99% <= 97.00
99.9% <= 97.00
14-2-17 19:39:17 ===============================================================
-- Histograms ------------------------------------------------------------------
com.netease.test.metrics.TestHistograms.random
count = 90
min = 0
max = 97
mean = 44.67
stddev = 28.47
median = 43.00
75% <= 71.00
95% <= 91.90
98% <= 96.18
99% <= 97.00
99.9% <= 97.00
*/
这个还真的蛮方便的。
这个或许可能是我们相对来讲更想要用到的了:Timer用来测量一段代码被调用的速率和用时。实际上Histogram也能做到,这个Timer就是基于Histograms和Meters来实现的。
package com.netease.test.metrics;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import static com.codahale.metrics.MetricRegistry.name;
/**
* Date: 14-2-17
* Time: 18:34
* 测试Timers
*/
public class TestTimers {
/**
* 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
*/
private static final MetricRegistry metrics = new MetricRegistry();
/**
* 在控制台上打印输出
*/
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
/**
* 实例化一个Meter
*/
// private static final Timer requests = metrics.timer(name(TestTimers.class, "request"));
private static final Timer requests = metrics.timer(name(TestTimers.class, "request"));
public static void handleRequest(int sleep) {
Timer.Context context = requests.time();
try {
//some operator
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
context.stop();
}
}
public static void main(String[] args) throws InterruptedException {
reporter.start(3, TimeUnit.SECONDS);
Random random = new Random();
while(true){
handleRequest(random.nextInt(1000));
}
}
}
/*
14-2-18 9:31:54 ================================================================
-- Timers ----------------------------------------------------------------------
com.netease.test.metrics.TestTimers.request
count = 4
mean rate = 1.33 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 483.07 milliseconds
max = 901.92 milliseconds
mean = 612.64 milliseconds
stddev = 196.32 milliseconds
median = 532.79 milliseconds
75% <= 818.31 milliseconds
95% <= 901.92 milliseconds
98% <= 901.92 milliseconds
99% <= 901.92 milliseconds
99.9% <= 901.92 milliseconds
14-2-18 9:31:57 ================================================================
-- Timers ----------------------------------------------------------------------
com.netease.test.metrics.TestTimers.request
count = 8
mean rate = 1.33 calls/second
1-minute rate = 1.40 calls/second
5-minute rate = 1.40 calls/second
15-minute rate = 1.40 calls/second
min = 41.07 milliseconds
max = 968.19 milliseconds
mean = 639.50 milliseconds
stddev = 306.12 milliseconds
median = 692.77 milliseconds
75% <= 885.96 milliseconds
95% <= 968.19 milliseconds
98% <= 968.19 milliseconds
99% <= 968.19 milliseconds
99.9% <= 968.19 milliseconds
14-2-18 9:32:00 ================================================================
-- Timers ----------------------------------------------------------------------
com.netease.test.metrics.TestTimers.request
count = 15
mean rate = 1.67 calls/second
1-minute rate = 1.40 calls/second
5-minute rate = 1.40 calls/second
15-minute rate = 1.40 calls/second
min = 41.07 milliseconds
max = 968.19 milliseconds
mean = 591.35 milliseconds
stddev = 302.96 milliseconds
median = 650.56 milliseconds
75% <= 838.07 milliseconds
95% <= 968.19 milliseconds
98% <= 968.19 milliseconds
99% <= 968.19 milliseconds
99.9% <= 968.19 milliseconds
*/
Metrics提供了一个独立的模块:Health Checks,用于对Application、其子模块或者关联模块的运行是否正常做检测。该模块是独立metrics-core模块的,使用时则导入metrics-healthchecks包。
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>3.0.1</version>
</dependency>
使用起来和与上述几种类型的Metrics有点类似,但是需要重新实例化一个Metrics容器HealthCheckRegistry,待检测模块继承抽象类HealthCheck并实现check()方法即可,然后将该模块注册到HealthCheckRegistry中,判断的时候通过isHealthy()接口即可。接下来以检查两个数据库的状态为例子:
package com.netease.test.metrics;
import com.codahale.metrics.health.HealthCheck;
import com.codahale.metrics.health.HealthCheckRegistry;
import java.util.Map;
import java.util.Random;
/**
* Date: 14-2-18
* Time: 9:57
*/
public class DatabaseHealthCheck extends HealthCheck{
private final Database database;
public DatabaseHealthCheck(Database database) {
this.database = database;
}
@Override
protected Result check() throws Exception {
if (database.ping()) {
return Result.healthy();
}
return Result.unhealthy("Can't ping database.");
}
/**
* 模拟Database对象
*/
static class Database {
/**
* 模拟database的ping方法
* @return 随机返回boolean值
*/
public boolean ping() {
Random random = new Random();
return random.nextBoolean();
}
}
public static void main(String[] args) {
// MetricRegistry metrics = new MetricRegistry();
// ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
HealthCheckRegistry registry = new HealthCheckRegistry();
registry.register("database1", new DatabaseHealthCheck(new Database()));
registry.register("database2", new DatabaseHealthCheck(new Database()));
while (true) {
for (Map.Entry<String, Result> entry : registry.runHealthChecks().entrySet()) {
if (entry.getValue().isHealthy()) {
System.out.println(entry.getKey() + ": OK");
} else {
System.err.println(entry.getKey() + ": FAIL, error message: " + entry.getValue().getMessage());
final Throwable e = entry.getValue().getError();
if (e != null) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
/*
console output:
database1: OK
database2: FAIL, error message: Can't ping database.
database1: FAIL, error message: Can't ping database.
database2: OK
database1: OK
database2: FAIL, error message: Can't ping database.
database1: FAIL, error message: Can't ping database.
database2: OK
database1: FAIL, error message: Can't ping database.
database2: FAIL, error message: Can't ping database.
database1: FAIL, error message: Can't ping database.
database2: FAIL, error message: Can't ping database.
database1: OK
database2: OK
database1: OK
database2: FAIL, error message: Can't ping database.
database1: FAIL, error message: Can't ping database.
database2: OK
database1: OK
database2: OK
database1: FAIL, error message: Can't ping database.
database2: OK
database1: OK
database2: OK
database1: OK
database2: OK
database1: OK
database2: FAIL, error message: Can't ping database.
database1: FAIL, error message: Can't ping database.
database2: FAIL, error message: Can't ping database.
*/
metrics-spring这个库为Spring增加了Metric库, 提供基于XML或者注解方式。
1、引入包:
<dependency>
<groupId>com.ryantenney.metrics</groupId>
<artifactId>metrics-spring</artifactId>
<version>3.0.1</version>
</dependency>
2、xml文件配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:metrics="http://www.ryantenney.com/schema/metrics"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.ryantenney.com/schema/metrics
http://www.ryantenney.com/schema/metrics/metrics-3.0.xsd">
<!-- Registry should be defined in only one context XML file -->
<metrics:metric-registry id="metrics" />
<!-- annotation-driven must be included in all context files -->
<metrics:annotation-driven metric-registry="metrics" />
<!-- (Optional) Registry should be defined in only one context XML file -->
<metrics:reporter type="console" metric-registry="metrics" period="1m" />
<!-- (Optional) The metrics in this example require the metrics-jvm jar-->
<metrics:register metric-registry="metrics">
<bean metrics:name="jvm.gc" class="com.codahale.metrics.jvm.GarbageCollectorMetricSet" />
<bean metrics:name="jvm.memory" class="com.codahale.metrics.jvm.MemoryUsageGaugeSet" />
<bean metrics:name="jvm.thread-states" class="com.codahale.metrics.jvm.ThreadStatesGaugeSet" />
<bean metrics:name="jvm.fd.usage" class="com.codahale.metrics.jvm.FileDescriptorRatioGauge" />
</metrics:register>
<!-- Beans and other Spring config -->
</beans>
3、java注解的方式
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
@Configuration
@EnableMetrics
public class SpringConfiguringClass extends MetricsConfigurerAdapter {
@Override
public void configureReporters(MetricRegistry metricRegistry) {
ConsoleReporter
.forRegistry(metricRegistry)
.build()
.start(1, TimeUnit.MINUTES);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。