使用 Prometheus & Grafana 監(jiān)控你的 Spring Boot 應(yīng)用

準(zhǔn)備工作

  • Prometheus v2.14.0
  • Grafana v6.5.0

使用 Docker 部署上面的基礎(chǔ)環(huán)境,配置文件可見 docker-compose.yml,輸入以下命令進(jìn)行一鍵部署:

# 檢查配置
docker-compose config
# 啟動(dòng)服務(wù)(-d 后臺(tái)啟動(dòng))
docker-compose up -d
# 停止并清除服務(wù)
docker-compose down

添加依賴項(xiàng)

pom.xml

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

  <!-- 借助 Micrometer 對(duì)接 Prometheus 監(jiān)控系統(tǒng) -->
  <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 端點(diǎn)暴露出來
        include: 'prometheus'
  metrics:
    tags: 
      application: ${spring.application.name}

logging:
  level:
    io.github.y0ngb1n.*: debug

使用方式

Step 1: 啟動(dòng)服務(wù)

$ 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
...

將應(yīng)用與 Prometheus 對(duì)接

添加 Prometheus 的配置文件:prometheus.yml

scrape_configs:
  # 任意寫,建議英文,不要包含特殊字符
  - job_name: 'spring-boot-app'
    # 多久采集一次數(shù)據(jù)
    scrape_interval: 15s
    # 采集時(shí)的超時(shí)時(shí)間
    scrape_timeout: 10s
    # 采集的路徑是啥
    metrics_path: '/actuator/prometheus'
    # 采集服務(wù)的地址,設(shè)置成 Spring Boot 應(yīng)用所在服務(wù)器的具體地址
    static_configs:
      - targets: ['192.168.31.44:8080']

可使用 ifconfig 查看宿主機(jī)的 IP 地址,Prometheus 服務(wù)器就會(huì)自動(dòng)每隔15秒請(qǐng)求 http://your-ip:8080/actuator/prometheus。更多配置參見 ??官方文檔

測(cè)試訪問 Prometheus

STEP 1:在瀏覽器中訪問 http://localhost:9090

STEP 2:查看監(jiān)控?cái)?shù)據(jù)

  • 點(diǎn)擊 Insert metric at cursor,即可選擇監(jiān)控指標(biāo);
  • 點(diǎn)擊 Graph,即可讓指標(biāo)以圖表方式展示;
  • 點(diǎn)擊 Execute 按鈕,即可看到類似下圖的結(jié)果。
查看監(jiān)控?cái)?shù)據(jù)

集成 Grafana 可視化

前面已經(jīng)使用 docker-compose 啟動(dòng)了 Grafana,下面進(jìn)行配置。

STEP 1:在瀏覽器中訪問 http://localhost:3000,并使用初始帳號(hào) admin:admin 進(jìn)行登錄

Grafana Login Page

STEP 2:點(diǎn)擊 Add Data Source 進(jìn)行添加 Prometheus 數(shù)據(jù)源

Add Data Source
Prometheus Data Source
image.png

STEP 3:創(chuàng)建 Dashboard

New Dashboard
Add Query
  • 設(shè)置監(jiān)控指標(biāo)
Add Query
  • 設(shè)置 Dashboard 信息
Title
  • 保存 Dashboard
Save Dashboard

可以添加不同的指標(biāo),指標(biāo)的取值詳見 Spring Boot 應(yīng)用的 /actuator/prometheus 端點(diǎn)。重復(fù)以上步驟操作,即可為 Dashboard 添加新的圖表。

如果你覺得自己定制一個(gè) Dashboard 過于復(fù)雜了,那你可以逛一下 Grafana 的 Dashboard 市場(chǎng)https://grafana.com/grafana/dashboards),這樣就可以方便快捷地復(fù)用別人精美的 Dashboard 面板了。

Demo

參考資料


感謝您的閱讀,本文由 楊斌的博客 版權(quán)所有。
如若轉(zhuǎn)載,請(qǐng)注明出處:楊斌的博客(https://y0ngb1n.github.io?utm_source=jianshu

項(xiàng)目已托管于 GitHub:y0ngb1n/spring-boot-samples,歡迎 Star, Fork :kissing_heart:


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容