溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot 默認(rèn)指標(biāo)從哪來(lái)

發(fā)布時(shí)間:2021-10-20 17:38:57 來(lái)源:億速云 閱讀:129 作者:柒染 欄目:大數(shù)據(jù)

Spring Boot 默認(rèn)指標(biāo)從哪來(lái),相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

您是否注意到 Spring Boot 和 Micrometer 為您的應(yīng)用生成的所有默認(rèn)指標(biāo)? 如果沒(méi)有 - 您可以將 actuator 依賴項(xiàng)添加到項(xiàng)目中,然后點(diǎn)擊 / actuator / metrics 端點(diǎn),在那里您將找到有關(guān) JVM 、進(jìn)程、Tomcat、流量等的有用信息。 然后,添加一些緩存,數(shù)據(jù)源 或 JPA 依賴項(xiàng),甚至?xí)霈F(xiàn)更多指標(biāo)。如果您想知道它們是如何結(jié)束的,我們可以在哪里找到關(guān)于它們所描述的參數(shù)的解釋,那么這篇文章就是為您準(zhǔn)備的。

顯示指標(biāo)

為了讓它井然有序,讓我們從如何在 Spring Boot 應(yīng)用程序中顯示指標(biāo)開(kāi)始。如果您已經(jīng)知道了,可以跳過(guò)這一部分。

Spring Boot中的指標(biāo)由 micrometer.io 處理。但是,如果您使用 actuator ,則不需要向項(xiàng)目添加 micrometer 依賴項(xiàng),因?yàn)?actuator 已經(jīng)依賴于它。即使您對(duì)它提供的端點(diǎn)不感興趣,也希望您使用 actuator ,因?yàn)檫@是通過(guò)其 AutoConfigurations 注冊(cè)許多指標(biāo)的模塊。稍后我們會(huì)詳細(xì)討論。

因此,首先,只需將執(zhí)行器依賴項(xiàng)添加到項(xiàng)目中(這里是 build.gradle.kts )

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-actuator")
}

并在執(zhí)行器端點(diǎn)中顯示指標(biāo)名稱,點(diǎn)擊 http://localhost:8080/actuator/metrics.

{
  "names": [
    "jvm.threads.states",
    "process.files.max",
    "jvm.memory.used",
    "jvm.gc.memory.promoted",
    "jvm.memory.max",
    "system.load.average.1m",
    ...
  ]
}

然后,要查看詳細(xì)信息,請(qǐng)?jiān)?URL 路徑中添加指標(biāo)名稱,例如: http://localhost:8080/actuator/metrics/system.cpu.count.

{
  "name": "system.cpu.count",
  "description": "The number of processors available to the Java virtual machine",
  "baseUnit": null,
  "measurements": [
    {
      "statistic": "VALUE",
      "value": 8
    }
  ],
  "availableTags": [
  ]
}

通過(guò)提供特定的儀表注冊(cè)表,可以定期將這些指標(biāo)發(fā)送到您選擇的指標(biāo)系統(tǒng)( PrometheusNew Relic,CloudWatch,Graphite 等)。 讓我們用最簡(jiǎn)單的注冊(cè)表來(lái)做 - LoggingMeterRegistry,它只是定期記錄所有指標(biāo)。

@Configuration
class MetricsConfig {
    @Bean
    LoggingMeterRegistry loggingMeterRegistry() {
        return new LoggingMeterRegistry();
    }
}

現(xiàn)在,指標(biāo)也顯示在日志中:

2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.count{id=direct} value=0 buffers
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.count{id=mapped} value=0 buffers
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.memory.used{id=direct} value=0 B
2019-07-17 11:07:09.406  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.buffer.memory.used{id=mapped} value=0 B
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.classes.loaded{} value=8530 classes
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.gc.live.data.size{} value=0 B
2019-07-17 11:07:09.408  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.gc.max.data.size{} value=0 B
2019-07-17 11:07:09.410  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.memory.committed{area=nonheap,id=Compressed Class Space} value=6.25 MiB
2019-07-17 11:07:09.410  INFO 91283 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : jvm.memory.committed{area=heap,id=G1 Eden Space} value=168 MiB
...

指標(biāo)供應(yīng)

那么,如何提供這些指標(biāo)呢?一個(gè)示例可能是 WebMvcMetricsFilter ,向所有 Spring Web MVC 端點(diǎn)添加性能指標(biāo) (http.server.requests metric) 。

但是這個(gè)例子很簡(jiǎn)單。當(dāng)所有請(qǐng)求都由 Spring 框架處理時(shí),在內(nèi)部添加調(diào)用生成指標(biāo)是沒(méi)有必要的(只檢查 WebMvcMetricsFilter.record() 方法)。

但是,如果您使用純 ehcachehibernate 或其他數(shù)據(jù)源,然后生成指標(biāo),情況又會(huì)如何呢?

那么 cache. * 指標(biāo)呢,即使我 @Autowirednet.sf.ehcache.Cache 也會(huì)生成?

那么 hibernate. * 指標(biāo)呢,即使我 @Autowiredorg.hibernate.SessionFactory 也會(huì)生成?

然后, jvm.* , process.* , tomcat.* 等如何自動(dòng)生成?

它似乎比人們想象的更簡(jiǎn)單,因?yàn)檫@些統(tǒng)計(jì)數(shù)據(jù)是由受監(jiān)控的組件本身提供的。 有時(shí),它將直接提供,例如cache.getStatistics()EhCache 提供 StatisticsGateway *,*或 sessionFactory.getStatistics()Hibernate SessionFactory 提供 statistics 等等。

有時(shí),這可以通過(guò)其他方式實(shí)現(xiàn),比如托管 bean 。例如,將 RuntimeMXBean 用于 JVM process.* 指標(biāo)以及 將(如GlobalRequestProcessorServlet 等) Tomcat mbeans 用于 tomcat. * 指標(biāo)

為了訪問(wèn)這些統(tǒng)計(jì)數(shù)據(jù)并將其轉(zhuǎn)換為特定指標(biāo),Micrometer 引入了 MeterBinder 的概念。

檢查 MeterBinder implementation 層次結(jié)構(gòu),您將了解更多關(guān)于可用的指標(biāo)組的信息。

Spring Boot 默認(rèn)指標(biāo)從哪來(lái)

您也可以直接在 micrometer repo 上檢查。

打開(kāi),例如, EhCache2Metrics ,您將找到 Ehcache 統(tǒng)計(jì)信息映射到特定 Micrometer 指標(biāo)的內(nèi)容和方式。

cache.size -> StatisticsGateway:getSize cache.gets{result=miss} -> StatisticsGateway:cacheMissCount cache.gets{result=hit} -> StatisticsGateway:cacheHitCount cache.puts -> StatisticsGateway:cachePutCount cache.evictions -> StatisticsGateway:cacheEvictedCount cache.remoteSize -> StatisticsGateway::getRemoteSize cache.removals -> StatisticsGateway::cacheRemoveCount cache.puts.added{result=added} -> StatisticsGateway::cachePutAddedCount cache.puts.added{result=updated} -> StatisticsGateway::cachePutAddedCount cache.misses{reason=expired} -> StatisticsGateway::cacheMissExpiredCount) cache.misses{reason=notFound} -> StatisticsGateway::cacheMissNotFoundCount) cache.xa.commits{result=readOnly} -> StatisticsGateway::xaCommitReadOnlyCount cache.xa.commits{result=exception} -> StatisticsGateway::xaCommitExceptionCount cache.xa.commits{result=committed} -> StatisticsGateway::xaCommitCommittedCount cache.xa.rollbacks{result=exception} -> StatisticsGateway::xaRollbackExceptionCount cache.xa.rollbacks{result=success} -> StatisticsGateway::xaRollbackSuccessCount cache.xa.recoveries{result=nothing} -> StatisticsGateway::xaRecoveryNothingCount cache.xa.recoveries{result=success} -> StatisticsGateway::xaRecoveryRecoveredCount cache.local.offheap.size -> StatisticsGateway::getLocalOffHeapSize) cache.local.heap.size -> StatisticsGateway::getLocalHeapSizeInBytes cache.local.disk.size -> StatisticsGateway::getLocalDiskSizeInBytes

注冊(cè) MeterBinders 是非常簡(jiǎn)單的,示例可以在 micrometer 文檔 中被找到。

記住,您可以手動(dòng)操作:

new ClassLoaderMetrics().bindTo(registry);
new JvmMemoryMetrics().bindTo(registry);
new EhCache2Metrics(cache, Tags.of("name", cache.getName())).bindTo(registry)
new TomcatMetrics(manager, tags).bindTo(registry)
...

或者,您可以使用 Spring Boot ,它會(huì)在引擎下為您做這件事。

正如我之前提到的,actuator 將提供許多 AutoConfiguration s 和 MetricsBinders ,只要添加給定的依賴項(xiàng),它就會(huì)注冊(cè) MeterBinders 。

例如, TomcatMetricsBinder 將注冊(cè) TomcatMetrics (為您的嵌入式容器)。MeterRegistryConfigurer 將注冊(cè) JVM 、運(yùn)行時(shí)間 和其他系統(tǒng)指標(biāo)。

現(xiàn)在,假設(shè)您想在您的應(yīng)用程序中使用 Ehcache 。 您可以添加兩個(gè)依賴項(xiàng):

    implementation("org.springframework.boot:spring-boot-starter-cache")
    implementation("net.sf.ehcache:ehcache")

然后注冊(cè)緩存(您也可以通過(guò) ehcache.xml 來(lái)實(shí)現(xiàn))

  @Bean
    Cache playCache(EhCacheCacheManager cacheManager) {
        CacheConfiguration cacheConfiguration = new CacheConfiguration()
            .name(CACHE_NAME)
            .maxEntriesLocalHeap(MAX_ELEMENTS_IN_MEMORY);
        Cache cache = new Cache(cacheConfiguration);
        cacheManager.getCacheManager().addCache(cache);
        cacheManager.initializeCaches();
        return cache;
    }

現(xiàn)在, CacheMetricsRegistrarConfiguration 將通過(guò) Spring 緩存管理器為每一個(gè)緩存管理注冊(cè) EhCache2Metrics 。

如果您不想使用 Spring 緩存管理,您也可以自己注冊(cè) EhCache2Metrics 。

現(xiàn)在,啟動(dòng)應(yīng)用程序,您將看到其他 ehcache 指標(biāo)。

2019-07-17 13:08:45.113  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.gets{cache=playCache,cacheManager=cacheManager,name=playCache,result=hit} throughput=12.95/s
2019-07-17 13:08:45.124  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.misses{cache=playCache,cacheManager=cacheManager,name=playCache,reason=notFound} throughput=3.7/s
2019-07-17 13:08:45.124  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.gets{cache=playCache,cacheManager=cacheManager,name=playCache,result=miss} throughput=3.7/s
2019-07-17 13:08:48.840  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts{cache=playCache,cacheManager=cacheManager,name=playCache} throughput=16.65/s
2019-07-17 13:08:48.840  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.misses{cache=playCache,cacheManager=cacheManager,name=playCache,reason=notFound} throughput=3.7/s
2019-07-17 13:08:48.841  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts{cache=playCache,cacheManager=cacheManager,name=playCache} throughput=16.65/s
2019-07-17 13:08:48.841  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=updated} throughput=0.116667/s
2019-07-17 13:08:48.841  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=updated} throughput=0.116667/s
2019-07-17 13:08:48.841  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=added} throughput=0.116667/s
2019-07-17 13:08:48.842  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.puts.added{cache=playCache,cacheManager=cacheManager,name=playCache,result=added} throughput=0.116667/s
2019-07-17 13:08:48.847  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.disk.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.847  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.disk.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.908  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.heap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=1.039062 KiB
2019-07-17 13:08:48.908  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.heap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=1.039062 KiB
2019-07-17 13:08:48.909  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.offheap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.local.offheap.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=0 B
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.remoteSize{} value=0
2019-07-17 13:08:48.909  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.remoteSize{} value=0
2019-07-17 13:08:48.909  INFO 93052 --- [       Thread-4] i.m.c.i.logging.LoggingMeterRegistry     : cache.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=7
2019-07-17 13:08:48.909  INFO 93052 --- [trics-publisher] i.m.c.i.logging.LoggingMeterRegistry     : cache.size{cache=playCache,cacheManager=cacheManager,name=playCache} value=7

在這種情況下,指標(biāo)上下文中每個(gè)組件的職責(zé)可歸納為:

Spring Boot 默認(rèn)指標(biāo)從哪來(lái)

您可以在 此處 提供的示例應(yīng)用中查看所有這些概念。

看完上述內(nèi)容,你們掌握Spring Boot 默認(rèn)指標(biāo)從哪來(lái)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI