當面對一個複雜的系統時,我們往往需要監控工具來幫助我們解決一些效能問題。比如之前我們使用
SpringBoot Admin
來監控應用,從而獲取到
SpringBoot Actuator
暴露的指標資訊。今天給大家介紹一個功能強大的監控工具Grafana,只要需要用到監控的地方,用它做視覺化就對了!
Grafana簡介
Grafana是一款開源的資料視覺化和分析工具,不管你的指標資訊儲存在哪裡,你都可以用它來視覺化這些資料。同時它還具有告警功能,當指標超出指定範圍時會提醒你。
Prometheus簡介
Prometheus是一款時序資料庫,可以簡單理解為帶時間的MySQL資料庫。由於Grafana只能將資料轉換成視覺化圖表,並沒有儲存功能,所以我們需要結合Prometheus這類時序資料庫一起使用。
安裝
使用Docker安裝Grafana和Prometheus無疑是最簡單的,我們接下來將採用此種方式。
首先下載Grafana的Docker映象;
docker pull grafana/grafana
下載完成後執行Grafana;
docker run -p 3000:3000 ——name grafana \-d grafana/grafana
接下來下載Prometheus的Docker映象;
docker pull prom/prometheus
在
/mydata/prometheus/
目錄下建立Prometheus的配置檔案
prometheus。yml
:
global: scrape_interval: 5s
執行Prometheus,把宿主機中的配置檔案
掛載到容器中去;
docker run -p 9090:9090 ——name prometheus \-v /mydata/prometheus/prometheus。yml:/etc/prometheus/prometheus。yml \-d prom/prometheus
至此安裝完成,是不是很簡單!可以透過如下地址訪問Grafana,登入賬號密碼為
admin:admin
image
登入Grafana後顯示介面如下;
使用
Grafana已經安裝完後,是時候來波實踐了,接下來我們來介紹下使用Grafana來監控Linux系統和SpringBoot應用。
監控系統資訊
使用
node_explorer
可以暴露Linux系統的指標資訊,然後Prometheus就可以透過定時掃描的方式獲取並存儲指標資訊了。
這次我們直接把
安裝到Linux伺服器上(如果使用Docker容器安裝,監控的會是Docker容器的指標資訊),將下載的安裝包解壓到指定目錄,並修改資料夾名稱:
cd /mydatatar -zxvf node_exporter-1。1。2。linux-amd64。tar。gzmv node_exporter-1。1。2。linux-amd64 node_exporter
進入解壓目錄,使用如下命令執行
,服務將執行在
9100
埠上;
cd node_exporter。/node_exporter >log。file 2>&1 &
使用
curl
命令訪問獲取指標資訊介面,獲取到資訊表示執行成功;
//localhost:9100/metrics
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served。# TYPE promhttp_metric_handler_requests_in_flight gaugepromhttp_metric_handler_requests_in_flight 1# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code。# TYPE promhttp_metric_handler_requests_total counterpromhttp_metric_handler_requests_total{code=“200”} 2175promhttp_metric_handler_requests_total{code=“500”} 0promhttp_metric_handler_requests_total{code=“503”} 0
接下來修改Prometheus的配置檔案
,建立一個任務定時掃描
暴露的指標資訊;
scrape_configs: - job_name: node static_configs: - targets: [‘192。168。5。78:9100’]
重啟Prometheus容器,可以透過
加號->Dashboard
來建立儀表盤;
選擇匯入Dashboard並輸入ID,最後點選
Load
即可;
選擇資料來源為Prometheus,最後點選
Import
;
匯入成功後就可以在Grafana中看到實時監控資訊了,是不是夠炫酷!
監控SpringBoot應用
監控SpringBoot應用需要依靠
actuator
及
micrometer
,透過暴露
的端點,Prometheus可以定時獲取並存儲指標資訊。
修改專案的
pom。xml
檔案,新增
及
依賴;
修改應用配置檔案
application。yml
,透過
暴露監控埠
/actuator/prometheus
;
management: endpoints: web: exposure: # 暴露端點`/actuator/prometheus` include: ‘prometheus’ metrics: tags: application: ${spring。application。name}
在監控SpringBoot應用之前,我們需要先執行一個SpringBoot應用,使用如下命令執行即可;
docker run -p 8088:8088 ——name mall-tiny-grafana \-v /etc/localtime:/etc/localtime \-v /mydata/app/mall-tiny-grafana/logs:/var/logs \-e TZ=“Asia/Shanghai” \-d mall-tiny/mall-tiny-grafana:1。0-SNAPSHOT
修改Prometheus的配置檔案
暴露的指標資訊,這裡需要注意下,由於SpringBoot應用執行在Docker容器中,需要使用
docker inspect mall-tiny-grafana |grep IPAddress
來獲取容器IP地址;
scrape_configs: # 採集任務名稱 - job_name: ‘mall-tiny-grafana’ # 採集時間間隔 scrape_interval: 5s # 採集超時時間 scrape_timeout: 10s # 採集資料路徑 metrics_path: ‘/actuator/prometheus’ # 採集服務的地址 static_configs: - targets: [‘172。17。0。5:8088’]
我們可以透過Prometheus的視覺化介面,來確定Prometheus是否能獲取到指標資訊;
匯入成功後就可以在Grafana中看到SpringBoot實時監控資訊了,果然夠炫酷!
總結
透過對Grafana的一波實踐,我們可以發現,使用Grafana來進行資料視覺化的過程是這樣的:首先我們得讓被監控方將指標資訊暴露出來,然後用Prometheus定時獲取並存儲指標資訊,最後將Prometheus配置為Grafana的視覺化資料來源。