JHipster一知半解- 3.6metric-資源監(jiān)控

回文集目錄:JHipster一知半解

JHipster基于spring-boot,因此本身已經(jīng)自帶了spring-boot-actuator的監(jiān)控功能,但是除了配置功能外,對于應(yīng)用的資源監(jiān)控,JHipster選用模型更為成熟的dropwizard metric。

dropwizard metric

TODO:此處應(yīng)該有/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這種殺手級框架的發(fā)布,就有點像小孩子的玩具了。特別地,進(jìn)階的spring cloud就不僅僅是快速這么一個維度的優(yōu)點了。但是Dropwizard提出了資源監(jiān)控部分,還是有較高的認(rèn)可和亮點的。JHipster采用了這部分作為內(nèi)置的監(jiān)控。

Metric類型主要有Gaugs,Countr,Meters,Histograms,TImers等5個維度的收集器。通過向Metric Registry注冊進(jìn)行收集,然后通過Reporter進(jìn)行展示。

MetricsConfiguration

@EnableMetrics(proxyTargetClass = true)

有ryantenney提供的Dropwizard Metric適配器,并提供Spring-boot化的一鍵式配置,通過這個引入DelegatingMetricsConfiguration進(jìn)行具體的配置。
值得注意的是,proxyTargetClass = true強(qiáng)制使用proxy代理類替代原始類,這樣保證AOP能夠正確應(yīng)用。

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

這里把metricRegistry注冊為一個Spring Bean以供后續(xù)使用。

@PostConstruct
init()函數(shù)

該函數(shù)為metricRegistry注冊MemoryUsageGaugeSet,GarbageCollectorMetricSet,ThreadStatesGaugeSet,FileDescriptorRatioGauge,BufferPoolMetricSet,JvmAttributeGaugeSet,JCacheGaugeSet眾多GaugeSet,進(jìn)行日志收集.
后面判斷jhipster.metrics.jmx.enabled和 jhipster.metrics.log.enabled設(shè)置情況,啟動對應(yīng)的reporter.
注意:jhipster.metrics.jmx.enabled默認(rèn)為true,而jhipster.metrics.log.enabled默認(rèn)為false.

另外一個值得注意的地方是,這里有可選注入HikariDataSource的地方,如果是HikariDataSource,還能額外獲得Hikari和metric的集成.

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

為什么Services statistics只監(jiān)控resource里面的??
實際上這是由metrics的com.codahale.metrics.servlet.InstrumentedFilter提供了一個"management/metrics"端點對外提供資源監(jiān)控的Restful服務(wù)。其中“Services statistics (time in millisecond)”位于Timers節(jié),因此這里只針對@Timed的方法進(jìn)行監(jiān)控。默認(rèn)情況下,AccountResource,AuditResource,LogsResource,UserResource里面的方法會進(jìn)入監(jiān)控。

io.github.jhipster.config.metrics.SpectatorLogMetricWriter

這個是應(yīng)用在微服務(wù)架構(gòu)中的特殊的日志W(wǎng)riter,單體應(yīng)用并沒有使用該類

外部監(jiān)控

TODO:此處應(yīng)該有選擇外部Metric的選擇截圖(cli)

JHipster還支持外部統(tǒng)一監(jiān)控,功能的實現(xiàn)是以spring-boot風(fēng)格完成的。默認(rèn)的配置已經(jīng)提供了,只需要引入相關(guān)的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工廠方法構(gòu)建出一個graphiteReporter,并且調(diào)用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.itdecent.cn/p/070f615dfb57
http://blog.csdn.net/cloud_ll/article/details/47136715
http://www.itdecent.cn/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 框架基礎(chǔ)
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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容