這段配置是 Spring Boot 應用中對 Actuator 和 Micrometer 監控系統的配置,用于將應用的指標暴露給 Prometheus 進行收集。下面我將詳細介紹這種配置方式及其提供的指標。
配置說明
這個配置主要涉及 Spring Boot Actuator 和 Micrometer 兩個核心組件:
- Actuator 配置:
management:endpoint:env:enabled: false # 禁用 env 端點,防止暴露敏感環境變量security:enabled: false # 禁用端點安全驗證(生產環境建議啟用)endpoints:web:exposure:include:- prometheus # 只暴露 prometheus 端點
這部分配置了 Actuator 端點的訪問權限和暴露范圍,特別啟用了 Prometheus 指標端點。
- Micrometer 配置:
management:metrics:tags:application: XXXX # 為所有指標添加 application=XXXX 標簽enable:all: true # 啟用所有默認指標收集distribution:percentiles:http.server.requests: 0.25,0.5,0.75,0.9,0.95 # 為 HTTP 請求指標計算指定百分位數
這部分配置了 Micrometer 的指標收集策略,包括標簽、啟用的指標類型和分布統計參數。
- 依賴配置:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
這兩個依賴分別引入了 Actuator 和 Prometheus 指標收集器。
提供的核心指標
Spring Boot Actuator 與 Micrometer 結合 Prometheus 會提供以下幾類核心指標:
1. JVM 相關指標
jvm_memory_used_bytes # JVM 各區域(堆、非堆)已使用內存
jvm_memory_max_bytes # JVM 各區域最大可用內存
jvm_memory_committed_bytes # JVM 各區域已提交的內存
jvm_gc_pause_seconds_total # GC 暫停總時間
jvm_threads_live_threads # 當前活躍線程數
jvm_classes_loaded_classes # 已加載的類數量
2. HTTP 請求指標
http_server_requests_seconds_count # HTTP 請求總數
http_server_requests_seconds_sum # HTTP 請求總耗時
http_server_requests_seconds_max # HTTP 請求最大耗時
http_server_requests_seconds # HTTP 請求耗時分布(通過 percentiles 配置)
可通過 method
、uri
、status
等標簽過濾不同請求路徑和狀態碼的性能。
3. 系統資源指標
system_cpu_usage # 系統 CPU 使用率
process_cpu_usage # 進程 CPU 使用率
system_load_average_1m # 系統 1 分鐘負載平均值
process_start_time_seconds # 進程啟動時間
4. 數據庫連接池指標(如 HikariCP)
hikaricp_connections_active # 活躍連接數
hikaricp_connections_idle # 空閑連接數
hikaricp_connections_pending # 等待獲取連接的線程數
hikaricp_connections_creation_seconds # 連接創建耗時
5. 緩存指標(如 Caffeine、Redis)
cache_gets_total # 緩存獲取次數
cache_hits_total # 緩存命中次數
cache_misses_total # 緩存未命中次數
cache_puts_total # 緩存放入次數
6. 自定義業務指標
通過 MeterRegistry
接口可以在代碼中添加自定義指標,例如:
@Autowired
private MeterRegistry meterRegistry;// 計數器
Counter requestCounter = meterRegistry.counter("http_requests", "endpoint", "/api/users");
requestCounter.increment();// 計時器
Timer apiTimer = meterRegistry.timer("api_response_time", "service", "user-service");
apiTimer.record(() -> {// 執行耗時操作
});
如何使用這些指標
- 訪問指標端點:
應用啟動后,可以通過http://<host>:<port>/actuator/prometheus
查看 Prometheus 格式的指標數據。 - Prometheus 配置:
在 Prometheus 的配置文件中添加抓取任務:
scrape_configs:- job_name: 'spring-boot-app'static_configs:- targets: ['localhost:8080']metrics_path: '/actuator/prometheus'
- Grafana 可視化:
導入 Spring Boot 官方提供的 Grafana 儀表盤模板(如 ID 12968),快速展示應用性能圖表。
性能優化建議
- 調整百分位計算:
配置中的percentiles
會增加計算開銷,生產環境建議只保留關鍵百分位(如 0.95、0.99)。 - 禁用不必要的指標:
如果某些指標對監控無幫助,可以通過management.metrics.enable.<metric-name>=false
禁用。 - 添加自定義標簽:
為指標添加有業務意義的標簽(如service
、operation
),便于聚合和分析。 - 監控告警:
基于關鍵指標設置告警規則,例如:- HTTP 請求 95% 響應時間超過 1 秒
- JVM 堆內存使用率超過 80%
- 數據庫連接池等待線程數持續大于 5
這種配置方式使 Spring Boot 應用能夠無縫集成 Prometheus 監控系統,提供全面的應用性能指標,幫助開發者及時發現和解決問題。