西木-Lee

 

metrics+spring+influxdb整合

1.在maven项目的pom.xml引入metrics-spring和metrics-influxdb两个jar包

    <dependency>
            <groupId>com.ryantenney.metrics</groupId>
            <artifactId>metrics-spring</artifactId>
            <version>3.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.davidb</groupId>
            <artifactId>metrics-influxdb</artifactId>
            <version>0.9.3</version>
        </dependency>

2.配置InfluxdbReporter

import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Configuration;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;

import metrics_influxdb.HttpInfluxdbProtocol;
import metrics_influxdb.InfluxdbReporter;

@Configuration
@EnableMetrics
public class SpringConfiguringClass extends MetricsConfigurerAdapter {

    @Override
    public void configureReporters(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

 

posted on 2018-07-19 17:22  西木-Lee  阅读(2445)  评论(0编辑  收藏  举报

导航