1、簡介
當某個應用程式在生產環境中運行時,監控其運行狀況是必要的,通過實時了解應用程式的運行狀況,你能在問題出現之前得到警告,也可以在客戶注意到問題之前解決問題,
在本文中,我們將創建一個Spring Boot應用程式,在Spring Actuator,Micrometer,Prometheus和Grafana的幫助下來監控系統,其中,Spring Actuator和Micrometer是Spring Boot App的一部分,

簡要說明了不同組件的目的:
Spring Actuator:在應用程式里提供眾多 Web 介面,通過它們了解應用程式運行時的內部狀況,有關更多資訊,請參見Spring Boot 2.0中的Spring Boot Actuator,Micrometer:為 Java 平臺上的性能資料收集提供了一個通用的 API,它提供了多種度量指標型別(Timers、Guauges、Counters等),同時支持接入不同的監控系統,例如 Influxdb、Graphite、Prometheus 等,Spring Boot Actuator對此提供了支持,Prometheus:一個時間序列資料庫,用于收集指標,Grafana:用于顯示指標的儀表板,
下面,我們將分別介紹每個組件,本文中使用的代碼存檔在GitHub上,
2、創建示例應用
首先要做的是創建一個可以監控的應用程式,通過Spring Initializr,并添加Spring Boot Actuator,Prometheus和Spring Web依賴項, 我們創建了一個如下所示的Spring MVC應用程式,
@RestController
public class MetricsController {
@GetMapping("/endPoint1")
public String endPoint1() {
return "Metrics for endPoint1";
}
@GetMapping("/endPoint2")
public String endPoint2() {
return "Metrics for endPoint2";
}
}
啟動應用程式:
$ mvn spring-boot:run
驗證介面是否正常:
$ curl http://localhost:8080/endPoint1Metrics for endPoint1$ curl http://localhost:8080/endPoint2Metrics for endPoint2
驗證Spring Actuator介面,為了使回應資訊方便可讀,我們通過python -mjson.tool來格式化資訊,
$ curl http://localhost:8080/actuator | python -mjson.tool
...
{
"_links":{
"self":{
"href":"http://localhost:8080/actuator",
"templated":false
},
"health":{
"href":"http://localhost:8080/actuator/health",
"templated":false
},
"health-path":{
"href":"http://localhost:8080/actuator/health/{*path}",
"templated":true
},
"info":{
"href":"http://localhost:8080/actuator/info",
"templated":false
}
}
}
默認情況下,會顯示以上資訊,除此之外,Spring Actuator可以提供更多資訊,但是你需要啟用它,為了啟用Prometheus,你需要將以下資訊添加到application.properties檔案中,
management.endpoints.web.exposure.include=health,info,prometheus
重啟應用程式,訪問http://localhost:8080/actuator/prometheus從Prometheus拉取資料,回傳了大量可用的指標資訊,我們這里只顯示輸出的一小部分,因為它是一個很長的串列,
$ curl http://localhost:8080/actuator/prometheus
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="G1 Evacuation Pause",} 2.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="G1 Evacuation Pause",} 0.009
...
如前所述,還需要Micrometer,Micrometer為最流行的監控系統提供了一個簡單的儀表板,允許儀表化JVM應用,而無需關心是哪個供應商提供的指標,它的作用和SLF4J類似,只不過它關注的不是Logging(日志),而是application metrics(應用指標),簡而言之,它就是應用監控界的SLF4J,
Spring Boot Actuator為Micrometer提供了自動配置,Spring Boot2在spring-boot-actuator中引入了micrometer,對1.x的metrics進行了重構,另外支持對接的監控系統也更加豐富(Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront),
更新后的application.properties檔案如下所示:
management.endpoints.web.exposure.include=health,info,metrics,prometheus
重啟應用程式,并從http://localhost:8080/actuator/metrics中檢索資料,
$ curl http://localhost:8080/actuator/metrics | python -mjson.tool
...
{
"names": [
"http.server.requests",
"jvm.buffer.count",
"jvm.buffer.memory.used",
...
可以直接通過指標名來檢索具體資訊,例如,如果查詢http.server.requests指標,可以按以下方式檢索:
$ curl http://localhost:8080/actuator/metrics/http.server.requests | python -mjson.tool
...
{
"name": "http.server.requests",
"description": null,
"baseUnit": "seconds",
"measurements": [
{
"statistic": "COUNT",
"value": 3.0
},
{
"statistic": "TOTAL_TIME",
"value": 0.08918682
},
...
3、添加Prometheus
Prometheus是Cloud Native Computing Foundation的一個開源監控系統,由于我們的應用程式中有一個/actuator/Prometheus端點來供 Prometheus 抓取資料,因此你現在可以配置Prometheus來監控你的Spring Boot應用程式,
Prometheus有幾種安裝方法,在本文中,我們將在Docker容器中運行Prometheus,
你需要創建一個prometheus.yml檔案,以添加到Docker容器中,
global:
scrape_interval:15s
scrape_configs:
- job_name: 'myspringmetricsplanet'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:8080']
scrape_interval:Prometheus多久輪詢一次應用程式的指標job_name:輪詢任務名稱metrics_path:指標的URL的路徑targets:主機名和埠號,使用時,替換HOST為主機的IP地址
如果在Linux上查找IP地址有困難,則可以使用以下命令:
$ ip -f inet -o addr show docker0 | awk '{print $4}' | cut -d '/' -f 1
啟動Docker容器并將本地prometheus.yml檔案,映射到Docker容器中的檔案,
$ docker run \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
成功啟動Docker容器后,首先驗證Prometheus是否能夠通過 http://localhost:9090/targets收集資料,

如上圖所示,我們遇到context deadline exceeded錯誤,造成Prometheus無法訪問主機上運行的Spring Boot應用程式,如何解決呢?
可以通過將Docker容器添加到你的主機網路來解決此錯誤,這將使Prometheus能夠訪問Spring Boot應用程式,
$ docker run \
--name prometheus \
--network host \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
-d \
prom/prometheus
再次驗證,狀態指示為UP,

現在可以顯示Prometheus指標,通過訪問http://localhost:9090/graph,在搜索框中輸入http_server_requests_seconds_max并單擊“執行”按鈕,將為你提供請求期間的最長執行時間,

4、添加Grafana
最后添加的組件是Grafana,盡管Prometheus可以顯示指標,但Grafana可以幫助你在更精美的儀表板中顯示指標,Grafana也支持幾種安裝方式,在本文中,我們也將在Docker容器中運行它,
$ docker run --name grafana -d -p 3000:3000 grafana/grafana
點擊 http://localhost:3000/,就可以訪問Grafana,

默認的用戶名/密碼為admin/admin,單擊“登錄”按鈕后,你需要更改默認密碼,

接下來要做的是添加一個資料源,單擊左側邊欄中的“配置”圖示,然后選擇“Data Sources(資料源)”,

單擊Add data source(添加資料源)按鈕,

Prometheus在串列的頂部,選擇Prometheus,

填寫可訪問Prometheus的URL,將HTTP Access設定為Browser,然后單擊頁面底部的Save&Test按鈕,

一切正常后,將顯示綠色的通知標語,指示資料源正在作業,

現在該創建儀表板了,你可以自定義一個,但也可以使用開源的儀表板,用于顯示Spring Boot指標的一種常用儀表板是JVM儀表板,
在左側邊欄中,點擊+號,然后選擇匯入,

輸入JVM儀表板的URL https://grafana.com/grafana/dashboards/4701,然后單擊“Load(加載)”按鈕,

為儀表板輸入一個有意義的名稱(例如MySpringMonitoringPlanet),選擇Prometheus作為資料源,然后單擊Import按鈕,

到目前為止,你就可以使用一個很酷的Grafana儀表板,

也可以將自定義面板添加到儀表板,在儀表板頂部,單擊Add panel(添加面板)圖示,

單擊Add new panel(添加新面板),

在Metrics 欄位中,輸入http_server_requests_seconds_max,在右側欄中的Panel title欄位中,可以輸入面板的名稱,

最后,單擊右上角的Apply 按鈕,你的面板將添加到儀表板,不要忘記保存儀表板,
為應用程式設定一些負載,并查看儀表板上的http_server_requests_seconds_max指標發生了什么,
$ watch -n 5 curl http://localhost:8080/endPoint1$ watch -n 10 curl http://localhost:8080/endPoint2

5、結論
在本文中,我們學習了如何為Spring Boot應用程式添加一些基本監控,這非常容易,只需要通過將Spring Actuator,Micrometer,Prometheus和Grafana組合使用,
當然,這只是一個起點,但是從這里開始,你可以為Spring Boot應用程式擴展和配置更多、更具體的指標,
原文:https://mydeveloperplanet.com/2021/03/03/how-to-monitor-a-spring-boot-app/
譯文:https://www.kubernetes.org.cn/9020.html
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2021最新版)
2.別在再滿屏的 if/ else 了,試試策略模式,真香!!
3.臥槽!Java 中的 xx ≠ null 是什么新語法?
4.Spring Boot 2.5 重磅發布,黑暗模式太炸了!
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/300328.html
標籤:Java
下一篇:Java基礎之代理模式
