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