SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)
引言
隨著微服務(wù)架構(gòu)的流行,對(duì)服務(wù)的監(jiān)控和管理變得尤為重要。Prometheus作為一個(gè)開(kāi)源的監(jiān)控和告警工具,以其強(qiáng)大的數(shù)據(jù)采集、存儲(chǔ)和查詢能力,受到了眾多開(kāi)發(fā)者的青睞。Spring Boot作為Java領(lǐng)域快速構(gòu)建微服務(wù)的框架,與Prometheus的結(jié)合可以實(shí)現(xiàn)對(duì)Spring Boot應(yīng)用的實(shí)時(shí)監(jiān)控。本文將介紹如何使用Prometheus監(jiān)控Spring Boot應(yīng)用。
一、 Prometheus 簡(jiǎn)介
Prometheus 是一個(gè)開(kāi)源的系統(tǒng)監(jiān)控和警報(bào)工具包,它通過(guò)采集和存儲(chǔ)指標(biāo)(metrics),提供了強(qiáng)大的數(shù)據(jù)查詢語(yǔ)言,可以幫助我們分析和理解應(yīng)用程序的行為。Prometheus 的核心組件是 Prometheus Server,它負(fù)責(zé)采集監(jiān)控指標(biāo)并提供查詢接口。
- Prometheus 官網(wǎng):https://prometheus.io/
- 項(xiàng)目 github 地址:https://github.com/prometheus/prometheus
二、 Spring Boot Actuator
Spring Boot Actuator 是 Spring Boot 提供的一系列用于監(jiān)控和管理 Spring Boot 應(yīng)用的工具。它提供了許多端點(diǎn)(endpoints),例如 /health、/info、/metrics 等,這些端點(diǎn)可以公開(kāi)應(yīng)用的內(nèi)部信息,如健康狀態(tài)、配置信息和度量指標(biāo)。
三、 集成 Prometheus 和 Spring Boot
要將 Prometheus 與 Spring Boot 應(yīng)用集成,我們需要執(zhí)行以下步驟:
3.1 添加依賴
首先,將 Spring Boot Actuator 和 Micrometer Prometheus Registry 添加到項(xiàng)目的依賴中。
- Actuator 提供了一系列內(nèi)置端點(diǎn),用于顯示運(yùn)行應(yīng)用的性能信息,如健康狀況、指標(biāo)等。
- Micrometer Prometheus registry 會(huì)將這些指標(biāo)格式化為 Prometheus 可讀格式。
<dependencies> <!-- Spring Boot Actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.7.15</version> </dependency> <!-- Micrometer Prometheus Registry --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.9.14</version> </dependency> </dependencies>
3.2 配置 Actuator
接下來(lái),application.yml 文件中配置 Actuator 以暴露 Prometheus 端點(diǎn):
management: endpoints: web: exposure: include: '*' metrics: export: prometheus: enabled: true
其他配置屬性:
- management.endpoints.web.exposure.include=* # 暴露所有端點(diǎn)
- management.metrics.export.prometheus.enabled=true #啟用Prometheus導(dǎo)出器
- management.endpoints.web.base-path=“/status” # 將/actuator/xxx修改為/status/xxx,防止被猜到
- management.endpoints.server.request.metric-name=“application:request” # 自定義接口指標(biāo)名
- management.server.port=10001 #指定端口,默認(rèn)跟server.port一樣,可以防止被猜到
3.3 啟動(dòng) Prometheus
下載并運(yùn)行 Prometheus Server??梢詮?nbsp;Prometheus官網(wǎng) 下載適用于您操作系統(tǒng)的版本。
- docker 方式 拉取安裝鏡像文件
docker pull prom/prometheus
- 創(chuàng)建并運(yùn)行容器
docker run --name prometheus -d -p 9090:9090 prom/prometheus
對(duì)于需要自定義配置的部署,可以將主機(jī)上的自定義 prometheus.yml 文件掛載到容器中:
docker run -d --name prometheus -p 9090:9090 -v D:\developsoft\docker\DockerDesktopWSL\data\prometheus\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
瀏覽器訪問(wèn) http://localhost:9090
3.4 配置 Prometheus
- 拷貝 prometheus.yml 文件到宿主機(jī) :
docker cp prometheus:/etc/prometheus/prometheus.yml D:\developsoft\docker\DockerDesktopWSL\data\prometheus\prometheus.yml
- 修改 Prometheus 的配置文件
prometheus.yml
,添加 Spring Boot 應(yīng)用作為目標(biāo)(target):
scrape_configs: - job_name: 'spring-boot-application' metrics_path: 'prometheus-demo/actuator/prometheus' scrape_interval: 15s static_configs: - targets: ['192.168.10.108:8091']
如上,localhost:8080 應(yīng)替換為 Spring Boot 應(yīng)用相應(yīng)的 宿主機(jī)
和端口。
- scrape_interval 指定 Prometheus 從應(yīng)用中抓取指標(biāo)的頻率。
- metrics_path 中 prometheus-demo為 springboot 應(yīng)用的contextPath,
/actuator/prometheus
為默認(rèn)路徑
3.5 訪問(wèn)監(jiān)控?cái)?shù)據(jù)
啟動(dòng) Spring Boot 應(yīng)用后,Prometheus 將定期從 /actuator/prometheus 端點(diǎn)抓取指標(biāo)數(shù)據(jù)。
四、 Grafana 可視化指標(biāo)
雖然 Prometheus 提供了基本的數(shù)據(jù)查詢和展示功能,但通常我們會(huì)使用 Grafana 來(lái)實(shí)現(xiàn)更豐富的數(shù)據(jù)可視化。Grafana 支持 Prometheus 作為數(shù)據(jù)源,可以方便地創(chuàng)建儀表板展示監(jiān)控?cái)?shù)據(jù)。
4.1 安裝 Grafana
- docker 方式 拉取安裝鏡像文件
docker pull grafana/grafana
- 創(chuàng)建并運(yùn)行容器
docker run -d --name=grafana -p 3000:3000 grafana/grafana
- 瀏覽器訪問(wèn)
http://localhost:3000
默認(rèn)用戶名/密碼:admin/admin
4.2 配置數(shù)據(jù)源
在 Grafana 中配置 Prometheus 作為數(shù)據(jù)源,指向 Prometheus Server 的地址。
4.3 創(chuàng)建儀表板
創(chuàng)建新的儀表板,并添加面板來(lái)展示關(guān)心的監(jiān)控指標(biāo)。
- 點(diǎn)擊左側(cè)邊欄的圖標(biāo),選擇 “Dashboard”,創(chuàng)建一個(gè)新的儀表盤。
- 在儀表盤中添加一個(gè)全新的面板。在這里,選擇要顯示的指標(biāo),決定可視化類型(圖表、儀表、表格等),并自定義面板的外觀。
- 選擇 Prometheus 記錄源,并使用 Prometheus 查詢語(yǔ)言 (PromQL) 選擇希望可視化的指標(biāo)。例如,要顯示 HTTP 請(qǐng)求的消耗,可以使用 price(http_requests_total[5m]) 這樣的查詢。
- 保存面板和儀表盤??梢詣?chuàng)建盡可能多的面板,以可視化 Spring Boot 應(yīng)用中的特殊指標(biāo)。
五、 自定義監(jiān)控指標(biāo)
除了 Spring Boot Actuator 提供的內(nèi)置指標(biāo),我們還可以通過(guò) Micrometer 添加自定義監(jiān)控指標(biāo),以監(jiān)控特定的業(yè)務(wù)邏輯或性能瓶頸。
5.1 添加自定義指標(biāo)
在 Spring Boot 應(yīng)用中,使用 Micrometer 的 API 添加自定義指標(biāo):
import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; @RestController public class CustomMetricsController { private final Counter ordersCounter; public CustomMetricsController(MeterRegistry registry) { this.ordersCounter = Counter.builder("orders_count") .description("The total number of orders") .register(registry); } @GetMapping("/order") public String createOrder() { ordersCounter.increment(); return "Order created"; } }
5.2 在 Grafana 中展示自定義指標(biāo)
在 Grafana 中,可以像展示其他 Prometheus 指標(biāo)一樣展示自定義指標(biāo)。
參考文獻(xiàn):
- https://spring4all.com/forum-post/6888.html
- prometheus 官方文檔: https://prometheus.io/docs/prometheus/latest/getting_started/
- https://www.jianshu.com/p/9480634bbfeb
- https://springdoc.cn/spring-boot-prometheus/
- https://springdoc.cn/spring-boot-actuators/
到此這篇關(guān)于SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Prometheus監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Prometheus如何實(shí)現(xiàn)資源監(jiān)控
- SpringBoot結(jié)合prometheus自定義埋點(diǎn)方式
- springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
- Java服務(wù)端服務(wù)監(jiān)控:Prometheus與Spring Boot Actuator的集成方式
相關(guān)文章
spring mvc中@PathVariable / 帶斜杠方式獲取
這篇文章主要介紹了spring mvc中@PathVariable / 帶斜杠方式獲取,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08OpenTelemetry?Java?SDK?高級(jí)用法解析
這篇文章主要介紹了OpenTelemetry?Java?SDK?的高級(jí)用法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Java實(shí)現(xiàn)圖書管理系統(tǒng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08基于springboot bean的實(shí)例化過(guò)程和屬性注入過(guò)程
這篇文章主要介紹了基于springboot bean的實(shí)例化過(guò)程和屬性注入過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java語(yǔ)言的Comparable和Comparator區(qū)別
這篇文章主要介紹了Java語(yǔ)言的Comparable和Comparator區(qū)別,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn)
這篇文章主要介紹了Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04