一、安裝Prometheus
Releases · prometheus/prometheus · GitHubhttps://github.com/prometheus/prometheus/releases
或
https://prometheus.io/download/https://prometheus.io/download/
1. 下載適用于 Windows 的二進制文件:
找到最新版本的發布頁面,下載 prometheus-<version>.windows-amd64.zip 文件(例如 prometheus-2.40.0.windows-amd64.zip)
2. 解壓文件
將下載的 .zip 文件解壓到你選擇的目錄(例如 C:\Prometheus)。
官網下載有點慢,這里推薦使用迅雷
3. 修改配置
這里我們使用默認的
4. 啟動
打開命令提示符或 PowerShell:
導航到 Prometheus 解壓目錄(例如 cd C:\Prometheus)。
啟動 Prometheus:
運行以下命令啟動 Prometheus:
.\prometheus.exe --config.file=prometheus.yml
如果一切正常,Prometheus 將開始運行并在控制臺輸出日志信息。
訪問 Prometheus Web UI:
打開瀏覽器并訪問 http://localhost:9090,你應該能看到 Prometheus 的 Web 界面
5. 驗證指標抓取
檢查目標狀態:
在 Prometheus Web UI 中,導航到 Status -> Targets,確保你的 Spring Boot 應用程序顯示為 UP 狀態。
查詢指標:
導航到 Graph 頁面,嘗試輸入一些 PromQL 查詢語句(例如 up),查看是否能獲取到預期的指標數據。
(可選)設置為 Windows 服務
為了方便管理,你可以將 Prometheus 設置為 Windows 服務,使其在系統啟動時自動運行。
下載 NSSM (Non-Sucking Service Manager):
訪問 NSSM Releases 頁面,下載適合你系統的版本。
安裝 Prometheus 服務:
使用命令提示符或 PowerShell,導航到 NSSM 解壓目錄。
運行以下命令安裝 Prometheus 服務:
nssm install Prometheus "C:\Prometheus\prometheus.exe" "--config.file=C:\Prometheus\prometheus.yml"
?啟動服務:
使用以下命令啟動 Prometheus 服務:
nssm start Prometheus
?管理服務:
你可以使用 Windows 服務管理器 (services.msc) 來管理和配置 Prometheus 服務。
注意事項
防火墻和網絡配置:確保防火墻允許 Prometheus 訪問你的應用程序的 /actuator/prometheus 端點。
依賴項:確保你的 Spring Boot 應用程序已經正確配置了 Micrometer 和 Prometheus 支持(如 micrometer-core 和 spring-boot-starter-actuator 依賴)。
二、安裝Grafana,也可通過Docker、包管理器等方式
1. 下載
Download Grafana | Grafana LabsOverview of how to download and install different versions of Grafana on different operating systems.https://grafana.com/grafana/download?pg=graf&plcmt=deploy-box-1&platform=windows
打開Web界面,默認地址:http://localhost:3000,初始用戶名和密碼都是 admin
2. 首次進入會根據提示修改默認密碼
3. 添加數據源,選擇Prometheus
?輸入需要檢測的項目地址
保存
?提示保存成功
三、SpringBoot代碼配置
1. maven文件
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
2. yml文件
server:port: 8080
spring:security:enabled: falsemanagement:endpoints:web:exposure:include: "prometheus,health,info" # 根據需要添加其他端點endpoint:prometheus:enabled: truemetrics:export:prometheus:enabled: truesecurity:enabled: false # 如果不需要安全保護,可以禁用
3. 配置文件
package com.coldwind.config;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*** Created by tpboreas@qq.com on 2025/01/09 10:40*/
@Configuration
public class MetricsConfiguration {@Beanpublic PrometheusMeterRegistry prometheusMeterRegistry() {return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);}
}
啟動項目
四、Grafana漢化
右上角設置
選擇中文
點擊保存,即可生效?
五、推薦配置
加載
樣式非常漂亮,非常使用
?大家快去體驗一下吧?