美文网首页
JHipster一知半解- 3.6metric-资源监控

JHipster一知半解- 3.6metric-资源监控

作者: 沉寂之舟 | 来源:发表于2018-06-13 17:15 被阅读283次

回文集目录:JHipster一知半解

JHipster基于spring-boot,因此本身已经自带了spring-boot-actuator的监控功能,但是除了配置功能外,对于应用的资源监控,JHipster选用模型更为成熟的dropwizard metric。

dropwizard metric

TODO:此处应该有/jhi-metrics页面的截图

pom.xml

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-annotation</artifactId>
    <version>${dropwizard-metrics.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-json</artifactId>
    <version>${dropwizard-metrics.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-jvm</artifactId>
    <version>${dropwizard-metrics.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-servlet</artifactId>
    <version>${dropwizard-metrics.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-servlets</artifactId>
</dependency>

简要说明

Dropwizard本身作为Restful框架,虽然简单,易上手;但是随着Spring-boot这种杀手级框架的发布,就有点像小孩子的玩具了。特别地,进阶的spring cloud就不仅仅是快速这么一个维度的优点了。但是Dropwizard提出了资源监控部分,还是有较高的认可和亮点的。JHipster采用了这部分作为内置的监控。

Metric类型主要有Gaugs,Countr,Meters,Histograms,TImers等5个维度的收集器。通过向Metric Registry注册进行收集,然后通过Reporter进行展示。

MetricsConfiguration

@EnableMetrics(proxyTargetClass = true)

有ryantenney提供的Dropwizard Metric适配器,并提供Spring-boot化的一键式配置,通过这个引入DelegatingMetricsConfiguration进行具体的配置。
值得注意的是,proxyTargetClass = true强制使用proxy代理类替代原始类,这样保证AOP能够正确应用。

@Bean
    public MetricRegistry getMetricRegistry() {
        return metricRegistry;
    }

这里把metricRegistry注册为一个Spring Bean以供后续使用。

@PostConstruct
init()函数

该函数为metricRegistry注册MemoryUsageGaugeSet,GarbageCollectorMetricSet,ThreadStatesGaugeSet,FileDescriptorRatioGauge,BufferPoolMetricSet,JvmAttributeGaugeSet,JCacheGaugeSet众多GaugeSet,进行日志收集.
后面判断jhipster.metrics.jmx.enabled和 jhipster.metrics.log.enabled设置情况,启动对应的reporter.
注意:jhipster.metrics.jmx.enabled默认为true,而jhipster.metrics.log.enabled默认为false.

另外一个值得注意的地方是,这里有可选注入HikariDataSource的地方,如果是HikariDataSource,还能额外获得Hikari和metric的集成.

@Autowired(required = false)
    public void setHikariDataSource(HikariDataSource hikariDataSource) {
        this.hikariDataSource = hikariDataSource;
    }
    
hikariDataSource.setMetricRegistry(metricRegistry);

为什么Services statistics只监控resource里面的??
实际上这是由metrics的com.codahale.metrics.servlet.InstrumentedFilter提供了一个"management/metrics"端点对外提供资源监控的Restful服务。其中“Services statistics (time in millisecond)”位于Timers节,因此这里只针对@Timed的方法进行监控。默认情况下,AccountResource,AuditResource,LogsResource,UserResource里面的方法会进入监控。

io.github.jhipster.config.metrics.SpectatorLogMetricWriter

这个是应用在微服务架构中的特殊的日志Writer,单体应用并没有使用该类

外部监控

TODO:此处应该有选择外部Metric的选择截图(cli)

JHipster还支持外部统一监控,功能的实现是以spring-boot风格完成的。默认的配置已经提供了,只需要引入相关的LIB包和少量自定义配置即可。

io.github.jhipster.config.metrics.GraphiteRegistry
io.github.jhipster.config.metrics.PrometheusRegistry

以Graphite为例

public GraphiteRegistry(MetricRegistry metricRegistry, JHipsterProperties jHipsterProperties) {
        this.jHipsterProperties = jHipsterProperties;
        if (this.
            jHipsterProperties.getMetrics().getGraphite().isEnabled()) {
            log.info("Initializing Metrics Graphite reporting");
            String graphiteHost = jHipsterProperties.getMetrics().getGraphite().getHost();
            Integer graphitePort = jHipsterProperties.getMetrics().getGraphite().getPort();
            String graphitePrefix = jHipsterProperties.getMetrics().getGraphite().getPrefix();
            Graphite graphite = new Graphite(new InetSocketAddress(graphiteHost, graphitePort));
            GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(metricRegistry)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .prefixedWith(graphitePrefix)
                .build(graphite);
            graphiteReporter.start(1, TimeUnit.MINUTES);
        }
    }

可以看出,代码读取Jhipster中的配置属性,通过GraphiteReporter工厂方法构建出一个graphiteReporter,并且调用start()方法启动.

yml中启用graphite或prometheus的位置(dev或prod)

jhipster:
    metrics:
        graphite: # Use the "graphite" Maven profile to have the Graphite dependencies
            enabled: false
            host: localhost
            port: 2003
            prefix: jhipster
        prometheus: # Use the "prometheus" Maven profile to have the Prometheus dependencies
            enabled: false
            endpoint: /prometheusMetrics
资源和书籍推荐

Metric 使用了dropwizard框架..
http://www.jianshu.com/p/070f615dfb57
http://blog.csdn.net/cloud_ll/article/details/47136715
http://www.jianshu.com/p/e4f70ddbc287
http://www.cnblogs.com/nexiyi/p/metrics_sample_2.html
http://metrics.dropwizard.io
http://www.cnblogs.com/java-zhao/category/806019.html

Metrics for Spring 集成(jihipster就用了这个)
https://github.com/ryantenney/metrics-spring

Metrics 框架基础
http://www.jdon.com/soa/dropwizard-vs-spring-boot.html
http://blog.csdn.net/qq_23660243/article/details/54406075
http://ningandjiao.iteye.com/blog/1766498
http://blog.csdn.net/kiwi_coder/article/details/17242605
http://hao.jobbole.com/dropwizard/
https://segmentfault.com/a/1190000000359827

相关文章

网友评论

      本文标题:JHipster一知半解- 3.6metric-资源监控

      本文链接:https://www.haomeiwen.com/subject/cyxseftx.html