Prometheus+Grafana實現服務效能監控:windows主機監控、Spring Boot監控、Spring Cloud Alibaba Seata監控

sowler發表於2023-11-02

1、Prometheus介紹

Prometheus使用Go語言開發,中文名稱叫:普羅 米修斯。Prometheus是一個開源系統最初在SoundCloud構建的監控和警報工具包。自 2012 年成立以來,許多公司和組織已經採用了 Prometheus,它現在是一個獨立的開源專案並獨立於任何公司進行維護。Prometheus於2016年加入雲原生計算基金會作為繼 Kubernetes之後的第二個託管專案。Prometheus主要用於抓取目標資料,並記錄所有抓取的目標資料。

官網地址

https://prometheus.io/

Github地址

https://github.com/prometheus/prometheus

2、下載Prometheus並啟動

官網下載:

https://prometheus.io/download/

Github下載:

https://github.com/prometheus/prometheus/releases

透過訪問上面的地址,可以選擇下載Prometheus的版本。這裡選擇下載windows版本:

下載成功後,解壓壓縮包,進入prometheus-2.44.0-rc.0.windows-386目錄我們就可以啟動程式了。

啟動程式:prometheus.exe

程式啟動成功後,訪問埠9090進入系統:

http://localhost:9090/graph

成功進入系統後,我們可以訪問Targets來檢視接入的目標資料,由於我們還沒有配置其他目標資料採集,目前只能檢測到自己的資訊:

3、安裝視覺化工具Grafana

透過上面啟動Prometheus,會發現上面的展示資訊都是資料而不是透過圖表展示出來,這個看起來非常費力。所以需要透過第三方工具透過圖表的方式用來把Prometheus收集的資料展現出來。

官網地址:

https://grafana.com/

進入官網後,可以在下載裡面進行下載程式或者直接點選連結下載

https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

Github地址:

https://github.com/grafana/grafana

Github ChangeLog:

https://github.com/grafana/grafana/blob/main/CHANGELOG.md

選擇版本和作業系統進行下載,下載成功後進入grafana檔案裡面進入/bin目錄,啟動程式:

啟動程式:grafana-server.exe

等待視窗載入完畢,訪問地址:http://localhost:3000  預設賬號密碼:admin/admin 就可以登入系統了,首次登入需要修改admin密碼。

在grafana中新增資料來源

點選新增資料來源,選擇Prometheus:

配置連線資訊:

配置成功後點選儲存,這樣我們的資料來源就配置成功了。

4、Grafana官網模板

在grafana中我們可以自己配置儀表盤展示資訊也可以透過引入官方模板進行展示。模板地址:

https://grafana.com/grafana/dashboards/

記錄一下常用模板資訊:

模板編號ID:
9276:主機基礎監控(cpu,記憶體,磁碟,網路)
12900:SpringBoot Dashboard
4701:JVM模板
10467:windows_exporter-Windows Server監控模板匯入
11587:OhmGraphite-0.21.0-Ohm Windows Desktop

5、使用Prometheus+Grafana實現windows主機監控

要想監控windows系統,首先我們需要一個windows監控工具,然後把這個監控地址的連線整合到Prometheus中,最後在透過Grafana進行展示出來。這裡介紹兩個window監控工具

第一個windows_exporter下載地址:

https://github.com/prometheus-community/windows_exporter/releases

選擇版本進行下載,下載windows執行程式就可以了,直接啟動即可。啟動完成埠為:9182,如果啟動成功訪問:http://localhost:9182/metrics 就可以以資料的格式展示出我們當前主機的配置資訊。

接下來就需要把地址配入到Prometheus中,讓Prometheus進行管理。進入Prometheus目錄,修改Prometheus配置檔案prometheus.yml並重新啟動Prometheus

重新啟動Prometheus後,再次檢視Targets標籤,發現windows_exporter接入成功,狀態為 UP(正常)

最後我們就需要把資料透過Grafana以圖表形式展現出來,首先登入我們安裝成功的grafana系統,點選+號匯入官方提供的模板。

輸入模板ID:10467,點選Load:

選擇資料來源為Prometheus

點選匯入,我們就可以檢視到windows系統的配置資訊

透過上面的步驟,我們就成功配置成功了一個監控資訊了。下面介紹windows的另一個監控軟體:OhmGraphite,下載地址:

https://github.com/nickbabcock/OhmGraphite/releases

需要下載壓縮包檔案,下載成功後進行解壓。解壓成功後需要進入OhmGraphite-0.21.0目錄,編輯OhmGraphite.exe.config檔案,增加prometheus作為接收資料來源。支援Graphite、InfluxDB、Prometheus、Timescale / Postgres。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="host" value="localhost" />
    <add key="port" value="2003" />
    <add key="interval" value="5" />
	  <!--開啟prometheus-->
	<add key="type" value="prometheus" />
    <add key="prometheus_port" value="4000" />
	<add key="prometheus_host" value="*" />
  </appSettings>
</configuration>

透過cmd命令列執行:

OhmGraphite.exe install

啟動:

OhmGraphite.exe start

啟動成功後訪問:http://127.0.0.1:4000/metrics 就可以訪問的監控資料。後面步驟就和第一次整合一致修改Prometheus配置檔案在scrape_configs中新增新的路徑,匯入Grafana。匯入模板ID:11587:OhmGraphite-0.21.0-Ohm Windows Desktop

 #被監控端的配置,目前只有一個節點,就是prometheus本身
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "windows_exporter"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["localhost:9182"] 
  - job_name: "windows_ohm_graphite"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ["localhost:4000"] 

6、SpringBoot監控

首先在SpringBoot專案中匯入依賴

        <!--prometheus 監控平臺 整合micrometer,將監控資料儲存到 prometheus-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <!--監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

在配置檔案bootstrap.yml或application.yml中加入配置暴露端點

#暴露端點
management:
  endpoints:
    web:
      exposure:
        include: '*'

配置成功後,啟動專案訪問地址:http://IP:PORT/actuator/prometheus

http://localhost:8810/actuator/prometheus

就可以檢視到監控資料

資料獲取成功後,後面就和原來的步驟一樣了,修改Prometheus配置檔案在scrape_configs中新增新的路徑,匯入Grafana。

  - job_name: "spring-boot-order-8810"
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: "/actuator/prometheus" #路徑變了,記得要更改訪問路徑
    static_configs:
    - targets: ["localhost:8810"]

這裡還是使用官方提供的模板,模板ID:12900:SpringBoot Dashboard

7、Spring Cloud Alibaba Seata事務監控

Seata支援在TC開啟Metrics資料採集並輸出到Prometheus監控系統中。預設是關閉狀態,需要開啟 metrics 的採集配置。進入\seata\conf,修改或新增metrics

## metrics configuration, only used in server side
metrics {
  enabled = true
  registryType = "compact"
  # multi exporters use comma divided
  exporterList = "prometheus"
  exporterPrometheusPort = 9898
  registry-type: compact
}

修改成功後,重新啟動seata。輸入http://IP:9898/metrics,即可獲得最新的Metrics資料

http://localhost:9898/metrics

修改Prometheus配置檔案新增seata:

檢視Targets

在Grafana中點選新建控制檯,新增seata資訊:

選擇資料來源

搜尋seata_transactional

點選Run queries查詢,就可以成功獲取seata資訊資料

獲取成功儲存圖表即可。我們點選圖表下面的顏色標籤,也可以檢視不同狀態下seata事務的使用情況以及事務提交狀況。好了,本次關於Prometheus+Grafana的學習就到這裡,以後如果會遇到其他使用情況也會做相關的記錄,方便以後再次遇到相同的問題時再次快速查詢。最後感謝閱讀本篇的讀友,讓我們一起成長、共同進步。

相關文章