項目已托管於 GitHub:y0ngb1n/spring-boot-samples,歡迎 Star, Fork 😘
準備工作#
- Prometheus
v2.14.0
- Grafana
v6.5.0
使用 Docker 部署上面的基礎環境,配置文件可見 docker-compose.yml
,輸入以下命令進行一鍵部署:
# 檢查配置
docker-compose config
# 啟動服務(-d 後台啟動)
docker-compose up -d
# 停止並清除服務
docker-compose down
添加依賴項#
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 借助 Micrometer 對接 Prometheus 監控系統 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
application.yml
spring:
application:
name: monitoring-prometheus-grafana
management:
endpoints:
web:
exposure:
# 將 Actuator 的 /actuator/prometheus 端點暴露出來
include: 'prometheus'
metrics:
tags:
application: ${spring.application.name}
logging:
level:
io.github.y0ngb1n.*: debug
使用方式#
Step 1: 啟動服務
$ mvn spring-boot:run
...
2019-12-08 22:28:11.916 INFO 36157 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
2019-12-08 22:28:12.045 INFO 36157 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-12-08 22:28:12.050 INFO 36157 --- [ main] i.g.y.m.p.PrometheusGrafanaApplication : Started PrometheusGrafanaApplication in 20.638 seconds (JVM running for 27.154)
Step 2: 訪問 /actuator/prometheus
端口
$ curl -sS http://127.0.0.1:8080/actuator/prometheus
# HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total{application="monitoring-prometheus-grafana",} 9986992.0
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="monitoring-prometheus-grafana",} 30.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="monitoring-prometheus-grafana",} 75.133
...
將應用與 Prometheus 對接#
添加 Prometheus 的配置文件:prometheus.yml
scrape_configs:
# 任意寫,建議英文,不要包含特殊字符
- job_name: 'spring-boot-app'
# 多久採集一次數據
scrape_interval: 15s
# 採集時的超時時間
scrape_timeout: 10s
# 採集的路徑是啥
metrics_path: '/actuator/prometheus'
# 採集服務的地址,設置成 Spring Boot 應用所在伺服器的具體地址
static_configs:
- targets: ['192.168.31.44:8080']
可使用 ifconfig
查看宿主機的 IP 地址,Prometheus 伺服器就會自動每隔 15 秒請求 http://your-ip:8080/actuator/prometheus
。更多配置參見 👉官方文檔
測試訪問 Prometheus#
STEP 1:在瀏覽器中訪問 http://localhost:9090
STEP 2:查看監控數據
- 點擊
Insert metric at cursor
,即可選擇監控指標; - 點擊
Graph
,即可讓指標以圖表方式展示; - 點擊
Execute
按鈕,即可看到類似下圖的結果。
集成 Grafana 可視化#
前面已經使用 docker-compose
啟動了 Grafana,下面進行配置。
STEP 1:在瀏覽器中訪問 http://localhost:3000
,並使用初始帳號 admin:admin
進行登錄
STEP 2:點擊 Add Data Source
進行添加 Prometheus 數據源
STEP 3:創建 Dashboard
- 設置監控指標
- 設置 Dashboard 信息
- 保存 Dashboard
可以添加不同的指標,指標的取值詳見 Spring Boot 應用的 /actuator/prometheus
端點。重複以上步驟操作,即可為 Dashboard 添加新的圖表。
如果你覺得自己定制一個 Dashboard 過於複雜了,那你可以逛一下 Grafana 的 Dashboard 市場(https://grafana.com/grafana/dashboards),這樣就可以方便快捷地復用別人精美的 Dashboard 面板了。
參考資料#
- https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/
- http://www.itmuch.com/spring-boot/actuator-prometheus-grafana/,by 周立
- https://www.baeldung.com/spring-boot-self-hosted-monitoring
- https://yunlzheng.gitbook.io/prometheus-book/
- https://micrometer.io/docs/registry/prometheus
- https://prometheus.io/docs/introduction/overview/
- https://hub.docker.com/r/prom/prometheus/
- https://grafana.com/docs/grafana/latest/