1、Prometheus抓取Java應用的指標
1、數據來源:Java應用自身暴露的指標
Java應用的指標數據來源于應用代碼中定義的指標對象(如Counter、Gauge、Histogram等),通過Prometheus客戶端庫(如io.prometheus:client_java)將這些指標注冊到默認的CollectorRegistry,并通過HTTP服務器暴露/metrics端點。
簡單來說:
在你的java應用程序中引入Prometheus相關的jar包(即Prometheus的客戶端),在進行適當的配置即可。Prometheus server端會主動從你的服務中獲取java應用的相關指標。
2、實現步驟
1、Java端示例
(1)、引入依賴
在pom.xml中添加Prometheus客戶端庫依賴。
pom.xml示例:
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient</artifactId><version>0.16.0</version>
</dependency>
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_httpserver</artifactId><version>0.16.0</version>
</dependency>
(2)、定義和注冊指標
在Java代碼中定義你需要監控的指標(如Counter計算器、Gauge儀表盤等),并注冊到CollectorRegistry收集器中。
java示例:
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.HTTPServer;
import java.io.IOException;public class MyApplication {// 定義一個Counter指標:記錄HTTP請求次數static final Counter requests = Counter.build().name("http_requests_total").help("Total number of HTTP requests.").labelNames("method", "status").register();// 定義一個Gauge指標:記錄當前在線用戶數static final Gauge onlineUsers = Gauge.build().name("online_users").help("Current number of online users.").register();public static void main(String[] args) throws IOException {// 啟動HTTP服務器,暴露/metrics端點(默認端口8080)HTTPServer server = new HTTPServer(8080);// 模擬業務邏輯:增加Counter和Gauge的值requests.labels("GET", "200").inc();onlineUsers.inc(); // 用戶上線onlineUsers.dec(); // 用戶下線}
}
(3)、驗證指標暴露
啟動應用后,訪問http://localhost:8080/metrics,會看到類似以下輸出。
輸出示例:
# HELP http_requests_total Total number of HTTP requests.
# TYPE http_requests_total counter
http_requests_total{method="GET",status="200"} 1# HELP online_users Current number of online users.
# TYPE online_users gauge
online_users 0
2、Prometheus Server端示例
(1)、Prometheus server配置抓取java客戶端指標
在prometheus.yml中配置Prometheus Server抓取該指標。
yaml示例:
scrape_configs:- job_name: "java-app"static_configs:- targets: ["localhost:8080"]
2、Redis中間件的指標獲取
1、數據來源:Redis本身暴露的指標
Redis本身不直接支持Prometheus格式的指標,因此需要通過Redis Exporter來采集Redis的指標(如內存使用、連接數、命中率等),并轉換為Prometheus格式。
簡單來說:
采集中間件相關的指標數據需要借助特定的Exporter組件。如Mysql有Mysql Exporter;Redis有Redis Exporter等。
- 以Redis為例,我們部署Redis Exporter,并指定監測的Redis服務。
- Redis Exporter會檢測和獲取到相關Redis實例的指標,如:調用Redis的Info命令。
- Prometheus Server會調用Redis Exporter的接口間接獲取到Redis實例的指標數據。
常見的Exporter:
- 對Redis:使用redis_exporter
- 對MySQL:使用mysqld_exporter
- 對服務器環境:使用node_exporter
Exporter的作用:
- 連接目標系統(如連接Redis實例)
- 獲取原始指標數據(如 Redis 的 INFO命令輸出)
- 將這些數據格式化為 Prometheus 能識別的格式
- 暴露一個/metrics接口供Prometheus抓取
2、實現步驟
1、Redis Exporter部分示例
(1)、部署Redis Exporter
可以通過Docker或二進制方式部署Redis Exporter。
bash示例:
# 使用Docker部署
docker run -d \--name redis-exporter \-p 9121:9121 \-e REDIS_ADDR=redis-host:6379 \quay.io/prometheuscommunity/redis-exporter
(2)、驗證指標暴露
訪問http://localhost:9121/metrics,會看到Redis的指標數據。
輸出示例:
# HELP redis_connected_clients Number of client connections (excluding connections from slaves).
# TYPE redis_connected_clients gauge
redis_connected_clients 10# HELP redis_memory_used_bytes Total number of bytes allocated by Redis using its allocator.
# TYPE redis_memory_used_bytes gauge
redis_memory_used_bytes 1048576
2、Prometheus Server端示例
(1)、Prometheus配置抓取
在prometheus.yml中配置抓取Redis Exporter的指標。
yaml示例:
scrape_configs:- job_name: "redis"static_configs:- targets: ["localhost:9121"]
附:常見Redis指標
- redis_connected_clients:當前連接的客戶端數量。
- redis_memory_used_bytes:Redis使用的內存字節數。
- redis_keyspace_hits:緩存命中次數。
- redis_keyspace_misses:緩存未命中次數。
3、服務器運行環境的指標獲取
1、數據來源:服務器操作系統指標
服務器環境指標(如CPU、內存、磁盤I/O)需要通過Node Exporter來采集,它會讀取/proc文件系統或調用系統命令獲取數據。
簡單來說:
在需要檢測指標的服務器上部署Node Exporter組件。該組件會通過相關Linux命令獲取到機器的指標。之后Prometheus Serve調用Node Exporter的接口,間接獲取到了服務器的相關指標數據。
2、實現步驟
(1)、安裝Node Exporter
下載并啟動 Node Exporter。
bash示例:
# 下載
wget https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-*.linux-amd64.tar.gz
# 解壓
tar xvfz node_exporter-*.linux-amd64.tar.gz
# 進入指定目錄
cd node_exporter-*.linux-amd64
# 啟動(默認端口9100)
./node_exporter
(2)、驗證指標暴露
訪問http://localhost:9100/metrics,會看到服務器的指標數據。
輸出示例:
# HELP node_cpu_seconds_total Seconds the CPUs spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{mode="idle",device="cpu0"} 12345.67# HELP node_memory_MemFree_bytes Free memory in bytes.
# TYPE node_memory_MemFree_bytes gauge
node_memory_MemFree_bytes 1073741824
(3)、Prometheus配置抓取
在prometheus.yml中配置抓取Node Exporter的指標。
yaml示例:
scrape_configs:- job_name: "node"static_configs:- targets: ["localhost:9100"]
附:常見服務器指標
- node_cpu_seconds_total:CPU使用時間(按模式分組)。
- node_memory_MemFree_bytes:空閑內存大小。
- node_disk_io_time_seconds_total:磁盤I/O時間。
- node_network_receive_bytes_total:網絡接收字節數。
4、總結:三類指標的獲取流程
5、驗證和調試
1、檢查指標是否暴露
- 使用curl或瀏覽器訪問/metrics端點。
bash示例:
curl http://localhost:8080/metrics
2、Prometheus界面驗證
- 登錄Prometheus Web UI(默認http://localhost:9090)。
- 輸入指標名稱(如http_requests_total)進行查詢。
3、Grafana可視化
- 安裝Grafana,配置Prometheus數據源。
- 導入預定義的Dashboard(如Node Exporter Full、Redis Exporter)。
通過以上步驟,你可以將Java應用、Redis中間件和服務器環境的指標集成到Prometheus監控體系中,并通過Grafana可視化展示。
向陽前行,Dare To Be!!!