SpringBoot + Prometheus:打造高效監(jiān)控系統(tǒng)
引言
隨著微服務(wù)架構(gòu)的流行,對服務(wù)的監(jiān)控和管理變得尤為重要。Prometheus作為一個(gè)開源的監(jiān)控和告警工具,以其強(qiáng)大的數(shù)據(jù)采集、存儲和查詢能力,受到了眾多開發(fā)者的青睞。
Spring Boot作為Java領(lǐng)域快速構(gòu)建微服務(wù)的框架,與Prometheus的結(jié)合可以實(shí)現(xiàn)對Spring Boot應(yīng)用的實(shí)時(shí)監(jiān)控。
本文將介紹如何使用Prometheus監(jiān)控Spring Boot應(yīng)用。
一、 Prometheus 簡介
Prometheus 是一個(gè)開源的系統(tǒng)監(jiān)控和警報(bào)工具包,它通過采集和存儲指標(biāo)(metrics),提供了強(qiáng)大的數(shù)據(jù)查詢語言,可以幫助我們分析和理解應(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)可以公開應(yīng)用的內(nèi)部信息,如健康狀態(tài)、配置信息和度量指標(biāo)。關(guān)注公眾號:碼猿技術(shù)專欄,回復(fù)關(guān)鍵詞:1111 獲取阿里內(nèi)部Java性能調(diào)優(yōu)手冊!
三、 集成 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 會將這些指標(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 端點(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 啟動 Prometheus
下載并運(yùn)行 Prometheus Server。可以從 Prometheus官網(wǎng) 下載適用于您操作系統(tǒng)的版本。
1.docker 方式 拉取安裝鏡像文件
docker pull prom/prometheus
2.創(chuàng)建并運(yùn)行容器
docker run --name prometheus -d -p 9090:9090 prom/prometheus
對于需要自定義配置的部署,可以將主機(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
3.瀏覽器訪問 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 訪問監(jiān)控?cái)?shù)據(jù)
啟動 Spring Boot 應(yīng)用后,Prometheus 將定期從 /actuator/prometheus 端點(diǎn)抓取指標(biāo)數(shù)據(jù)。
四、 Grafana 可視化指標(biāo)
雖然 Prometheus 提供了基本的數(shù)據(jù)查詢和展示功能,但通常我們會使用 Grafana 來實(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
瀏覽器訪問 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)。
圖片
- 點(diǎn)擊左側(cè)邊欄的圖標(biāo),選擇 “Dashboard”,創(chuàng)建一個(gè)新的儀表盤。
- 在儀表盤中添加一個(gè)全新的面板。在這里,選擇要顯示的指標(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
publicclass CustomMetricsController {
privatefinal 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)。