簡介
本文介紹在springboot3.x里配置啟用系統(tǒng)指標監(jiān)控功能,來監(jiān)視系統(tǒng)各項Metrics指標,從而實時了解應(yīng)用的運行健康狀態(tài),通過promtheus服務(wù)提供數(shù)據(jù)收集與指標可視化方案;
Metrics指標
metrics指標表示應(yīng)用程序代碼運行中提供的多維度指標數(shù)據(jù),每一條指標數(shù)據(jù)通常由時間序列及一組標簽鍵值對結(jié)構(gòu)數(shù)據(jù)組成,常見的運行維度指標有:服務(wù)響應(yīng)時間,HTTP請求量,CPU利用率,內(nèi)存使用大小,磁盤讀寫大小,JVM內(nèi)存使用率等等;
Spring Boot Actuator為Micrometer提供依賴管理和自動配置,Micrometer 是一個支持眾多監(jiān)控系統(tǒng),并提供應(yīng)用程序的可觀察性度量指標數(shù)據(jù)組件;
Micrometer官方描述為計時器、計量器、計數(shù)器、分布摘要和長任務(wù)計時器提供供應(yīng)商中立的接口,并具有維度數(shù)據(jù)模型,當與維度監(jiān)控系統(tǒng)配合使用時,可以有效訪問特定的命名指標,并具有向下鉆取的能力跨越它的維度。
SpringBoot啟用metrics
在springboot生態(tài)體系spring-boot-starter-actuator組件中已集成相關(guān)Metrics指標配置與服務(wù),可以方便支持從 Spring Boot 應(yīng)用程序中獲取應(yīng)用程序可觀察性指標。
相關(guān)的配置與使用方式,在springboot官方文檔中都有進行描述;詳情參見:Production-ready Features
以一個springboot3.x項目做為示例,演示如何使用metrics指標服務(wù),并對接到prometheus中做可觀察性指標展示;
項目信息:springboot.3.1.0 + jdk17
pom.xml
創(chuàng)建任意一個springboot項目,引入以下pom配置項
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<!-- prometheus依賴 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.11.0</version>
</dependency>
</dependencies>
application.yml
# 本地服務(wù)訪問
server:
# 服務(wù)端口
port: 8080
# 服務(wù)IP
address: 0.0.0.0
spring:
application:
# 應(yīng)用服務(wù)名
name: testDemo
# 是否啟用springboot的debug調(diào)試模式,會打印詳細日志信息
debug: false
# 開啟健康檢查
management:
# 自定義管理服務(wù)地址和端口,默認和應(yīng)用程序地址端口一致
# server:
# port: 8081
# address: 0.0.0.0
endpoint:
shutdown:
enabled: true
health:
show-details: always
endpoints:
web:
exposure:
# 開啟端點,*表示開啟所有
include:
- '*'
# 某些端點除外,不做開啟
exclude:
- env
- beans
# 跨域配置
cors:
# 開放所有域訪問
allowed-origins:
- '*'
metrics:
# 指標采集標簽名
tags:
application: ${spring.application.name}
# 啟用對接prometheus服務(wù)采集指標數(shù)據(jù)
prometheus:
metrics:
export:
enabled: true
ApplicationStart.java
@SpringBootApplication
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
}
瀏覽器訪問
啟動springboot程序后,通過以下URL在瀏覽器中打開
http://127.0.0.1:8080/actuator
查看已開啟的端點與端點數(shù)據(jù)展示鏈接
{
"_links": {
"self": {
"href": "http://127.0.0.1:8080/actuator",
"templated": false
},
"caches-cache": {
"href": "http://127.0.0.1:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://127.0.0.1:8080/actuator/caches",
"templated": false
},
"health": {
"href": "http://127.0.0.1:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://127.0.0.1:8080/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://127.0.0.1:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://127.0.0.1:8080/actuator/conditions",
"templated": false
},
"shutdown": {
"href": "http://127.0.0.1:8080/actuator/shutdown",
"templated": false
},
"configprops": {
"href": "http://127.0.0.1:8080/actuator/configprops",
"templated": false
},
"configprops-prefix": {
"href": "http://127.0.0.1:8080/actuator/configprops/{prefix}",
"templated": true
},
"loggers": {
"href": "http://127.0.0.1:8080/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://127.0.0.1:8080/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://127.0.0.1:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://127.0.0.1:8080/actuator/threaddump",
"templated": false
},
"prometheus": {
"href": "http://127.0.0.1:8080/actuator/prometheus",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://127.0.0.1:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://127.0.0.1:8080/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://127.0.0.1:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://127.0.0.1:8080/actuator/mappings",
"templated": false
}
}
}
安裝prometheus
prometheus是一個開源監(jiān)控系統(tǒng),用于收集和聚合指標作為時間序列數(shù)據(jù),它前身是SoundCloud的警告工具包。
prometheus通過抓取或輪詢獲取各個應(yīng)用程序?qū)嵗闹笜?。Spring Boot 提供了一個執(zhí)行器端點/actuator/prometheus輸出數(shù)據(jù)格式,以提供prometheus服務(wù)抓取。
下載
Download | Prometheus
prometheus支持windows環(huán)境安裝與使用,本文采用prometheus-2.37.0(windows-amd64)版本做為演示使用;
prometheus.yml
解壓下載包后,進入prometheus解壓根目錄,編輯prometheus.yml配置,添加拉取的springboot應(yīng)用服務(wù)配置;
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# 增加spring任務(wù)節(jié)點,從/actuator/prometheus拉取目標指標數(shù)據(jù)
- job_name: "spring"
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["localhost:8080"]
瀏覽器訪問
完成prometheus.yml配置后,直接雙擊prometheus.exe程序運行服務(wù);服務(wù)啟動完畢后,通過以下URL在瀏覽器中訪問:
http://127.0.0.1:9090/
點擊 “菜單欄》Status》Targets” 查看已配置并啟用的采集端點應(yīng)用
Metrics指標跟蹤
點擊菜單欄的“Graph”項,在搜索框中輸入jvm,則會自動聯(lián)想多個帶有jvm內(nèi)容的可觀察性指標;
我們以常見的java應(yīng)用程序內(nèi)存使用為例,選擇jvm_memory_used_bytes,觀察應(yīng)用服務(wù)運行過程中的jvm內(nèi)存使用情況,進行實時監(jiān)控以便及時了解應(yīng)用服務(wù)運行狀況,為應(yīng)用服務(wù)提供有效的健康跟蹤方案;
Grafana大盤展示
由于metrics維度指標眾多,通過prometheus服務(wù)采集后,通過數(shù)據(jù)指標轉(zhuǎn)化成可觀察性圖形,為應(yīng)用程序運維提供了合適的監(jiān)控技術(shù)與管理平臺,同時也可以將prometheus采集的指標輸出到第三方各類平臺上進行展示,比如運維中常見的Grafana,提供了許多豐富的圖標插件,高大上的的漂亮UI,以及Grafana官網(wǎng)第三方上傳的展示模板,可以非常便捷的套用開放模板后立即呈現(xiàn)出可視化監(jiān)控圖形界面;
- 注:此處以一個已有的Grafana服務(wù)為例,不額外描述Grafana安裝過程,安裝請自行百度;
默認在explore界面查找指標展示;
添加SpringBoot監(jiān)控大盤,官方有很多共享的儀表盤資源可下載,從而減少自已不熟悉儀表盤的配置與腳本,避免無法操作出想要的效果,通過獲得第三方共享資源可以快速進行展示。
Dashboards | Grafana Labs
以下為共享ID:12900 (SpringBoot APM Dashboard)
在共享大盤頁面復(fù)制共享ID后,在Grafana》Dashboards瀏覽界面,點擊import按鈕進入導(dǎo)入共享資源界面;
輸入共享大盤ID:12900,點擊load后,進入大盤配置盤面,底部的Prometheus選擇已安裝的Prometheus數(shù)據(jù)源選擇(如沒有,需要先在插件庫中提前安裝);
導(dǎo)入共享大盤后,進入到大盤展示界面,開始加載Prometheus監(jiān)控的維度指標數(shù)據(jù),通過各種儀表盤進行詳細展示;
文章來源:http://www.zghlxwxcb.cn/news/detail-760318.html
到此,一個完整的springboot項目從集成metrics指標組件,到Prometheus監(jiān)控服務(wù)搭建,到最后Grafana大盤儀表展示流程結(jié)束;文章來源地址http://www.zghlxwxcb.cn/news/detail-760318.html
到了這里,關(guān)于SpringBoot+Prometheus采集Metrics指標數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!