SpringBoot集成 Prometheus進行高效監(jiān)控的實現(xiàn)
引言
隨著微服務(wù)架構(gòu)的流行,對服務(wù)的監(jiān)控和管理變得尤為重要。Prometheus作為一個開源的監(jiān)控和告警工具,以其強大的數(shù)據(jù)采集、存儲和查詢能力,受到了眾多開發(fā)者的青睞。Spring Boot作為Java領(lǐng)域快速構(gòu)建微服務(wù)的框架,與Prometheus的結(jié)合可以實現(xiàn)對Spring Boot應(yīng)用的實時監(jiān)控。本文將介紹如何使用Prometheus監(jiān)控Spring Boot應(yīng)用。
一、 Prometheus 簡介
Prometheus 是一個開源的系統(tǒng)監(jiān)控和警報工具包,它通過采集和存儲指標(biāo)(metrics),提供了強大的數(shù)據(jù)查詢語言,可以幫助我們分析和理解應(yīng)用程序的行為。Prometheus 的核心組件是 Prometheus Server,它負(fù)責(zé)采集監(jiān)控指標(biāo)并提供查詢接口。
- Prometheus 官網(wǎng):https://prometheus.io/
- 項目 github 地址:https://github.com/prometheus/prometheus
二、 Spring Boot Actuator
Spring Boot Actuator 是 Spring Boot 提供的一系列用于監(jiān)控和管理 Spring Boot 應(yīng)用的工具。它提供了許多端點(endpoints),例如 /health、/info、/metrics 等,這些端點可以公開應(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 添加到項目的依賴中。
- Actuator 提供了一系列內(nèi)置端點,用于顯示運行應(yīng)用的性能信息,如健康狀況、指標(biāo)等。
- Micrometer Prometheus registry 會將這些指標(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
接下來,application.yml 文件中配置 Actuator 以暴露 Prometheus 端點:
management: endpoints: web: exposure: include: '*' metrics: export: prometheus: enabled: true
其他配置屬性:
- management.endpoints.web.exposure.include=* # 暴露所有端點
- 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 啟動 Prometheus
下載并運行 Prometheus Server??梢詮?nbsp;Prometheus官網(wǎng) 下載適用于您操作系統(tǒng)的版本。
- docker 方式 拉取安裝鏡像文件
docker pull prom/prometheus
- 創(chuàng)建并運行容器
docker run --name prometheus -d -p 9090:9090 prom/prometheus
對于需要自定義配置的部署,可以將主機上的自定義 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
瀏覽器訪問 http://localhost:9090
3.4 配置 Prometheus
- 拷貝 prometheus.yml 文件到宿主機 :
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)的 宿主機
和端口。
- scrape_interval 指定 Prometheus 從應(yīng)用中抓取指標(biāo)的頻率。
- metrics_path 中 prometheus-demo為 springboot 應(yīng)用的contextPath,
/actuator/prometheus
為默認(rèn)路徑
3.5 訪問監(jiān)控數(shù)據(jù)
啟動 Spring Boot 應(yīng)用后,Prometheus 將定期從 /actuator/prometheus 端點抓取指標(biāo)數(shù)據(jù)。
四、 Grafana 可視化指標(biāo)
雖然 Prometheus 提供了基本的數(shù)據(jù)查詢和展示功能,但通常我們會使用 Grafana 來實現(xiàn)更豐富的數(shù)據(jù)可視化。Grafana 支持 Prometheus 作為數(shù)據(jù)源,可以方便地創(chuàng)建儀表板展示監(jiān)控數(shù)據(jù)。
4.1 安裝 Grafana
- docker 方式 拉取安裝鏡像文件
docker pull grafana/grafana
- 創(chuàng)建并運行容器
docker run -d --name=grafana -p 3000:3000 grafana/grafana
- 瀏覽器訪問
http://localhost:3000
默認(rèn)用戶名/密碼:admin/admin
4.2 配置數(shù)據(jù)源
在 Grafana 中配置 Prometheus 作為數(shù)據(jù)源,指向 Prometheus Server 的地址。
4.3 創(chuàng)建儀表板
創(chuàng)建新的儀表板,并添加面板來展示關(guān)心的監(jiān)控指標(biāo)。
- 點擊左側(cè)邊欄的圖標(biāo),選擇 “Dashboard”,創(chuàng)建一個新的儀表盤。
- 在儀表盤中添加一個全新的面板。在這里,選擇要顯示的指標(biāo),決定可視化類型(圖表、儀表、表格等),并自定義面板的外觀。
- 選擇 Prometheus 記錄源,并使用 Prometheus 查詢語言 (PromQL) 選擇希望可視化的指標(biāo)。例如,要顯示 HTTP 請求的消耗,可以使用 price(http_requests_total[5m]) 這樣的查詢。
- 保存面板和儀表盤??梢詣?chuàng)建盡可能多的面板,以可視化 Spring Boot 應(yīng)用中的特殊指標(biāo)。
五、 自定義監(jiān)控指標(biāo)
除了 Spring Boot Actuator 提供的內(nèi)置指標(biāo),我們還可以通過 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)。
參考文獻:
- 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進行高效監(jiān)控的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Prometheus監(jiān)控內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring mvc中@PathVariable / 帶斜杠方式獲取
這篇文章主要介紹了spring mvc中@PathVariable / 帶斜杠方式獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08基于springboot bean的實例化過程和屬性注入過程
這篇文章主要介紹了基于springboot bean的實例化過程和屬性注入過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java語言的Comparable和Comparator區(qū)別
這篇文章主要介紹了Java語言的Comparable和Comparator區(qū)別,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Java KindEditor粘貼圖片自動上傳到服務(wù)器功能實現(xiàn)
這篇文章主要介紹了Java KindEditor粘貼圖片自動上傳到服務(wù)器功能實現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04