1.在maven项目的pom.xml引入metrics-spring和metrics-influxdb两个jar包
com.ryantenney.metrics
metrics-spring
3.1.3
com.github.davidb
metrics-influxdb
0.9.3
2.配置InfluxdbReporter
importjava.util.concurrent.TimeUnit;importorg.springframework.context.annotation.Configuration;importcom.codahale.metrics.ConsoleReporter;importcom.codahale.metrics.MetricFilter;importcom.codahale.metrics.MetricRegistry;importcom.ryantenney.metrics.spring.config.annotation.EnableMetrics;importcom.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;importmetrics_influxdb.HttpInfluxdbProtocol;importmetrics_influxdb.InfluxdbReporter;
@Configuration
@EnableMetricspublic class SpringConfiguringClass extendsMetricsConfigurerAdapter {
@Overridepublic voidconfigureReporters(MetricRegistry metricRegistry) {//registerReporter allows the MetricsConfigurerAdapter to//shut down the reporter when the Spring context is closed//输出度量数据至控制台
registerReporter(ConsoleReporter.forRegistry(metricRegistry).build()).start(5, TimeUnit.SECONDS);//输出度数数据到influxdb
InfluxdbReporter.forRegistry(metricRegistry)
.protocol(new HttpInfluxdbProtocol("your influxdb server IP address", 8086, "test", "test"))
.convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL)
.skipIdleMetrics(false).tag("server", "127.0.0.1").build().start(1, TimeUnit.SECONDS);
}
}
3.在需要监控的代码方法体上引入相关注解
note:方法必须为public方法
4.参考文档
Metrics官网 https://metrics.dropwizard.io/4.0.0/getting-started.html
metrics-spring官网 https://github.com/ryantenney/metrics-spring
metrics-influxdb官网 https://github.com/davidB/metrics-influxdb
grafana官网文档 http://docs.grafana.org/features/panels/graph/
5.整合influxdb+grafana+metrics,可参考
https://blog.csdn.net/qq_35981283/article/details/76887173