v2.14.0
v6.5.0
使用 Docker 部署上面的基礎環境,配置文件可見 docker-compose.yml
,輸入如下命令進行一鍵部署:git
# 檢查配置 docker-compose config # 啓動服務(-d 後臺啓動) docker-compose up -d # 中止並清除服務 docker-compose down
pom.xml
github
<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
web
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: 啓動服務spring
$ 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
端口docker
$ 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.yml
segmentfault
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
。更多配置參見 👉官方文檔瀏覽器
STEP 1:在瀏覽器中訪問 http://localhost:9090
tomcat
STEP 2:查看監控數據bash
Insert metric at cursor
,便可選擇監控指標;Graph
,便可讓指標以圖表方式展現;Execute
按鈕,便可看到相似下圖的結果。前面已經使用 docker-compose
啓動了 Grafana,下面進行配置。服務器
STEP 1:在瀏覽器中訪問 http://localhost:3000
,並使用初始賬號 admin:admin
進行登陸
STEP 2:點擊 Add Data Source
進行添加 Prometheus 數據源
STEP 3:建立 Dashboard
能夠添加不一樣的指標,指標的取值詳見 Spring Boot 應用的 /actuator/prometheus
端點。重複以上步驟操做,便可爲 Dashboard 添加新的圖表。
若是你以爲本身定製一個 Dashboard 過於複雜了,那你能夠逛一下 Grafana 的 Dashboard 市場(https://grafana.com/grafana/d...),這樣就能夠方便快捷地複用別人精美的 Dashboard 面板了。
感謝您的閱讀,本文由 楊斌的博客 版權全部。
如若轉載,請註明出處:楊斌的博客(https://y0ngb1n.github.io?utm...)
項目已託管於 GitHub:y0ngb1n/spring-boot-samples,歡迎 Star, Fork 😘