使用Grafana監(jiān)控Redis的操作方法
當面對一個復雜的系統(tǒng)時,我們往往需要監(jiān)控工具來幫助我們解決一些性能問題。比如之前我們使用
SpringBoot Admin
來監(jiān)控應用,從而獲取到SpringBoot Actuator
暴露的指標信息。今天給大家介紹一個功能強大的監(jiān)控工具Grafana,只要需要用到監(jiān)控的地方,用它做可視化就對了!
Grafana簡介
Grafana是一款開源的數(shù)據(jù)可視化和分析工具,不管你的指標信息存儲在哪里,你都可以用它來可視化這些數(shù)據(jù)。同時它還具有告警功能,當指標超出指定范圍時會提醒你。
Prometheus簡介
Prometheus是一款時序數(shù)據(jù)庫,可以簡單理解為帶時間的MySQL數(shù)據(jù)庫。由于Grafana只能將數(shù)據(jù)轉換成可視化圖表,并沒有存儲功能,所以我們需要結合Prometheus這類時序數(shù)據(jù)庫一起使用。
安裝
使用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/
目錄下創(chuàng)建Prometheus的配置文件prometheus.yml
:
global: ??scrape_interval:?5s
運行Prometheus,把宿主機中的配置文件
prometheus.yml
掛載到容器中去;
docker?run?-p?9090:9090?--name?prometheus?\ -v?/mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml?\ -d?prom/prometheus
至此安裝完成,是不是很簡單!可以通過如下地址訪問Grafana,登錄賬號密碼為
admin:admin
,訪問地址:http://192.168.5.78:3000/
登錄Grafana后顯示界面如下;
其實Prometheus也是有可視化界面的,就是有點簡陋,訪問地址:http://192.168.5.78:9090/
使用
Grafana已經(jīng)安裝完后,是時候來波實踐了,接下來我們來介紹下使用Grafana來監(jiān)控Linux系統(tǒng)和SpringBoot應用。
監(jiān)控系統(tǒng)信息
使用
node_explorer
可以暴露Linux系統(tǒng)的指標信息,然后Prometheus就可以通過定時掃描的方式獲取并存儲指標信息了。
下載
node_explorer
的安裝包,下載地址:https://prometheus.io/download/#node_exporter
這次我們直接把
node_explorer
安裝到Linux服務器上(如果使用Docker容器安裝,監(jiān)控的會是Docker容器的指標信息),將下載的安裝包解壓到指定目錄,并修改文件夾名稱:
cd?/mydata tar?-zxvf?node_exporter-1.1.2.linux-amd64.tar.gz mv?node_exporter-1.1.2.linux-amd64?node_exporter
進入解壓目錄,使用如下命令運行
node_explorer
,服務將運行在9100
端口上;
cd?node_exporter ./node_exporter?>log.file?2>&1?&
使用
curl
命令訪問獲取指標信息接口,獲取到信息表示運行成功;
curl?http://localhost:9100/metrics
#?HELP?promhttp_metric_handler_requests_in_flight?Current?number?of?scrapes?being?served. #?TYPE?promhttp_metric_handler_requests_in_flight?gauge promhttp_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?counter promhttp_metric_handler_requests_total{code="200"}?2175 promhttp_metric_handler_requests_total{code="500"}?0 promhttp_metric_handler_requests_total{code="503"}?0
接下來修改Prometheus的配置文件
prometheus.yml
,創(chuàng)建一個任務定時掃描node_explorer
暴露的指標信息;
scrape_configs: ??-?job_name:?node ????static_configs: ????-?targets:?['192.168.5.78:9100']
重啟Prometheus容器,可以通過
加號->Dashboard
來創(chuàng)建儀表盤;
當然你還可以選擇去Grafana的儀表盤市場下載一個Dashboard,市場地址:https://grafana.com/grafana/dashboards
這里選擇了
Node Exporter Full
這個儀表盤,記住它的ID,訪問地址:https://grafana.com/grafana/dashboards/1860
選擇導入Dashboard并輸入ID,最后點擊
Load
即可;
選擇數(shù)據(jù)源為Prometheus,最后點擊
Import
;
導入成功后就可以在Grafana中看到實時監(jiān)控信息了,是不是夠炫酷!
監(jiān)控SpringBoot應用
監(jiān)控SpringBoot應用需要依靠
actuator
及micrometer
,通過暴露actuator
的端點,Prometheus可以定時獲取并存儲指標信息。
修改項目的
pom.xml
文件,添加actuator
及micrometer
依賴;
<dependencies> ????<dependency> ????????<groupId>org.springframework.boot</groupId> ????????<artifactId>spring-boot-starter-actuator</artifactId> ????</dependency> ????<!--?集成micrometer,將監(jiān)控數(shù)據(jù)存儲到prometheus?--> ????<dependency> ????????<groupId>io.micrometer</groupId> ????????<artifactId>micrometer-registry-prometheus</artifactId> ????</dependency> </dependencies>
修改應用配置文件
application.yml
,通過actuator
暴露監(jiān)控端口/actuator/prometheus
;
management: ??endpoints: ????web: ??????exposure: ????????#?暴露端點`/actuator/prometheus` ????????include:?'prometheus' ??metrics: ????tags: ??????application:?${spring.application.name}
在監(jiān)控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的配置文件
prometheus.yml
,創(chuàng)建一個任務定時掃描actuator
暴露的指標信息,這里需要注意下,由于SpringBoot應用運行在Docker容器中,需要使用docker inspect mall-tiny-grafana |grep IPAddress
來獲取容器IP地址;
scrape_configs: ??#?采集任務名稱 ??-?job_name:?'mall-tiny-grafana' ????#?采集時間間隔 ????scrape_interval:?5s ????#?采集超時時間 ????scrape_timeout:?10s ????#?采集數(shù)據(jù)路徑 ????metrics_path:?'/actuator/prometheus' ????#?采集服務的地址 ????static_configs: ??????-?targets:?['172.17.0.5:8088']
我們可以通過Prometheus的可視化界面,來確定Prometheus是否能獲取到指標信息;
同樣,我們可以從儀表盤市場導入儀表盤,訪問地址:https://grafana.com/grafana/dashboards/14370
導入成功后就可以在Grafana中看到SpringBoot實時監(jiān)控信息了,果然夠炫酷!
總結
通過對Grafana的一波實踐,我們可以發(fā)現(xiàn),使用Grafana來進行數(shù)據(jù)可視化的過程是這樣的:首先我們得讓被監(jiān)控方將指標信息暴露出來,然后用Prometheus定時獲取并存儲指標信息,最后將Prometheus配置為Grafana的可視化數(shù)據(jù)源。
參考資料
Grafana官方文檔:https://grafana.com/docs/grafana/latest/getting-started/getting-started-prometheus/
node-exporter的使用:https://prometheus.io/docs/guides/node-exporter/
項目源碼地址
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-grafana
到此這篇關于使用Grafana監(jiān)控Redis的文章就介紹到這了,更多相關Grafana監(jiān)控Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
如何高效地向Redis插入大量的數(shù)據(jù)(推薦)
本篇文章主要介紹了如何高效地向Redis插入大量的數(shù)據(jù),現(xiàn)在分享給大家,感興趣的小伙伴們可以參考一下。2016-11-11Redis可視化工具Redis?Desktop?Manager的具體使用
本文主要介紹了Redis可視化工具Redis?Desktop?Manager的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12