spring boot metrics監(jiān)控指標(biāo)使用教程
spring boot metrics是什么?
針對應(yīng)用監(jiān)控指標(biāo)暴露,spring boot有一套完整的解決方案,并且內(nèi)置了好很多的指標(biāo)收集器,如tomcat、jvm、cpu、kafka、DataSource、spring mvc(缺少直方圖的數(shù)據(jù))等?;趍icrometer技術(shù),幾乎支持所有主流的監(jiān)控服務(wù)的指標(biāo)數(shù)據(jù)收集,這其中就包含了我們線上使用的Prometheus,這份指南旨在最快速接入boot的metrics功能,暴露prometheus的數(shù)據(jù)監(jiān)控指標(biāo)服務(wù)。
micrometer地址:https://micrometer.io/
一、引入依賴
implementation ('org.springframework.boot:spring-boot-starter-actuator') implementation ('io.micrometer:micrometer-registry-prometheus:1.6.1') implementation ('io.micrometer:micrometer-core:1.6.1')
actuator是spring boot中負責(zé)運維功能的包,這里主要是通過它來暴露和管理metrics接口的。其他兩個依賴是為了包兼容引入的,在sprinr boot2.x中,actuator中默認引入的prometheus支持包存在兼容性問題,如果你的環(huán)境不存在兼容性問題,可以不用引入下面兩個依賴。
二、配置啟用
通過如下的配置,來開啟prometheus的端點接口服務(wù)
management.endpoints.web.exposure.include=prometheus
開啟服務(wù)后,會暴露/actuator/prometheus 端點接口服務(wù)。
在瀏覽器中,輸入http://localhost:8080/actuator/prometheus ??梢钥吹絻?nèi)置的指標(biāo)收集器收集到的監(jiān)控指標(biāo)
三、獨立的web服務(wù)
默認情況下,/actuator/prometheus端點服務(wù)跟隨應(yīng)用的web容器一起發(fā)布,但是當(dāng)我們的web服務(wù)面向公網(wǎng)需要授權(quán)認證時,可以使用如下配置啟用獨立的容器暴露服務(wù)
management.server.port=8081
四、全局標(biāo)簽設(shè)置
在metrics監(jiān)控系統(tǒng)設(shè)計中,tag用來標(biāo)記區(qū)分一組指標(biāo)集。比如我們在監(jiān)控grpc時,servicename就是是監(jiān)控指標(biāo)的其中一個tag。有的時候為了區(qū)分環(huán)境和應(yīng)用,我們會設(shè)置一些全局的tag:
management.metrics.tags.application = ${spring.application.name} management.metrics.tags.region = bj
如上配置,我們添加了一個應(yīng)用的名字和一個區(qū)域的tag。這種配置是全局的。雖然grpc的組件可能只記錄了servicename,但是最終數(shù)據(jù)呈現(xiàn)時,也會帶上全局配置的tag
五、自定義指標(biāo)收集
spring boot所有的指標(biāo)最終都是通過MeterRegistry來注冊的,這個實例被spring托管,所以你可以在spring的上下文中注入這個實例,結(jié)合micrometer指標(biāo)定義,自定義自己的監(jiān)控指標(biāo)
六、推送or拉取指標(biāo)
目前,我們線上是通過k8s的monitoring.coreos.com/v1 api定義指定prometheus主動拉取應(yīng)用pod的監(jiān)控指標(biāo)信息,主要是因為之前的metrics系統(tǒng)是基于prometheus client模式暴露的。在基于spring boot的metrics系統(tǒng)中,主動推送數(shù)據(jù)的模式非常容易實現(xiàn),這里需要prometheus-gateway支持
引入依賴
implementation("io.prometheus:simpleclient_pushgateway")
啟用push模式
#開啟prometheus的數(shù)據(jù)推送模式 management.metrics.export.prometheus.pushgateway.enabled=true #prometheus服務(wù)端地址 management.metrics.export.prometheus.pushgateway.base-url=localhost:9091 #推送數(shù)據(jù)的頻率,默認1m(單位分鐘) management.metrics.export.prometheus.pushgateway.push-rate=1m #在jvm關(guān)閉之前將數(shù)據(jù)推送出去 management.metrics.export.prometheus.pushgateway.shutdown-operation=push
以上就是spring boot metrics監(jiān)控指標(biāo)使用教程的詳細內(nèi)容,更多關(guān)于spring boot metrics監(jiān)控指標(biāo)教程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實現(xiàn)解析JSON大文件JsonReader工具詳解
這篇文章主要介紹了Java實現(xiàn)解析JSON大文件的工具JsonReader使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01詳解Java編程中protected修飾符與static修飾符的作用
這篇文章主要介紹了Java編程中protected關(guān)鍵字與static關(guān)鍵字的作用,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01idea 在springboot中使用lombok插件的方法
這篇文章主要介紹了idea 在springboot中使用lombok的相關(guān)資料,通過代碼給大家介紹在pom.xml中引入依賴的方法,本文給大家介紹的非常詳細,需要的朋友可以參考下2021-08-08SpringBoot多環(huán)境切換的配置實現(xiàn)
在日常的開發(fā)中,一般都會分好幾種環(huán)境,本文就來介紹一下SpringBoot多環(huán)境切換的配置實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03JSON--List集合轉(zhuǎn)換成JSON對象詳解
這篇文章主要介紹了List集合轉(zhuǎn)換成JSON對象,小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。2017-01-01Fluent Mybatis學(xué)習(xí)之Update語法實踐
Fluent MyBatis是一個MyBatis的增強工具,沒有對mybatis做任何修改。本篇文章將詳細介紹對Fluent Mybatis中的update語法進行驗證。代碼具有一定價值,感興趣的小伙伴可以學(xué)習(xí)一下2021-11-11