1. 依赖
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.2.5</version>
</dependency>
2. 配置
@Configuration
public class JmxTpsConfig {
@Bean
public MetricRegistry metricRegistry() {
return new MetricRegistry();
}
@Bean
public JmxReporter jmxReporter(MetricRegistry registry) {
JmxReporter reporter = JmxReporter.forRegistry(registry).build();
reporter.start();
return reporter;
}
@Bean
public Meter xxxMeter(MetricRegistry registry) {
return registry.meter(MetricRegistry.name(Xxx.class, "request", "tps"));
}
}
3. 使用
在Xxx类中需要监控的方法开头添加:
xxxMeter.mark();
有几个需要监控的方法就配置几个xxxMeter Bean。
4. 查看
上面使用的report是jmxReporter,因此要打开jconsole,在mbean>metrics中查看相应的监控结果,包括请求总数/总的/1min/5min/15min的tps。