1. 程式人生 > 其它 >一招搞定 Spring Boot 視覺化監控!

一招搞定 Spring Boot 視覺化監控!

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進行了重構,另外支援對接的監控系統也更加豐富(AtlasDatadogGangliaGraphiteInfluxJMXNewRelicPrometheusSignalFxStatsDWavefront)。

更新後的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開發手冊(嵩山版)》最新發布,速速下載!

覺得不錯,別忘了隨手點贊+轉發哦!