javascript
Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控
文章目錄
- 環(huán)境信息
- Prometheus 組件
- 下載 & 安裝
- 通過指定配置文件prometheus.yml啟動Prometheus
- 查看采集到的性能指標
- 查看prometheus規(guī)則
- 查看監(jiān)控對象
- SpringBoot集成Prometheus
- pom.xml
- 配置文件
- 實例化MeterRegistryCustomizer
- 打包,上傳到服務器上
- Prometheus 修改配置文件prometheus.yml 接入該工程
- Grafana 組件
- 下載 & 安裝 & 啟動Grafana
- Grafana 接入Prometheus 的數據
- Step1 添加Prometheus 數據源
- Step2 設置相關信息
- Step3 導入想要的dashboards
環(huán)境信息
OS: Centos7 (防火墻已經關閉)
IP: 192.168.31.34
APP: Spring Boot 2.1.3搭建的演示環(huán)境
Prometheus 組件
官網: https://prometheus.io/
下載地址: https://prometheus.io/download/
官方文檔: https://prometheus.io/docs/prometheus/latest/getting_started/
Prometheus是一套開源的監(jiān)控&報警&時間序列數據庫的組合,基于應用的metrics來進行監(jiān)控的開源工具 。
更多信息請參考官網介紹: https://prometheus.io/docs/introduction/overview/
下載 & 安裝
官網下載速度太慢,我這里就沒有使用最新版本,而是從 http://cactifans.hi-www.com 下載了 prometheus-2.1.0的版本
# 下載 prometheus-2.1.0.linux-amd64.tar.gz [root@artisan ~]# wget http://cactifans.hi-www.com/prometheus/prometheus-2.1.0.linux-amd64.tar.gz --2019-03-10 21:57:56-- http://cactifans.hi-www.com/prometheus/prometheus-2.1.0.linux-amd64.tar.gz Resolving cactifans.hi-www.com (cactifans.hi-www.com)... 222.186.135.67 Connecting to cactifans.hi-www.com (cactifans.hi-www.com)|222.186.135.67|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 25271154 (24M) [application/octet-stream] Saving to: ‘prometheus-2.1.0.linux-amd64.tar.gz’100%[===========================================================================================================================================>] 25,271,154 6.34MB/s in 3.7s 2019-03-10 21:58:00 (6.55 MB/s) - ‘prometheus-2.1.0.linux-amd64.tar.gz’ saved [25271154/25271154] # 解壓prometheus-2.1.0.linux-amd64.tar.gz [root@artisan ~]# tar -xvzf prometheus-2.1.0.linux-amd64.tar.gz prometheus-2.1.0.linux-amd64/ prometheus-2.1.0.linux-amd64/consoles/ prometheus-2.1.0.linux-amd64/consoles/index.html.example prometheus-2.1.0.linux-amd64/consoles/node-cpu.html prometheus-2.1.0.linux-amd64/consoles/node-disk.html prometheus-2.1.0.linux-amd64/consoles/node-overview.html prometheus-2.1.0.linux-amd64/consoles/node.html prometheus-2.1.0.linux-amd64/consoles/prometheus-overview.html prometheus-2.1.0.linux-amd64/consoles/prometheus.html prometheus-2.1.0.linux-amd64/console_libraries/ prometheus-2.1.0.linux-amd64/console_libraries/menu.lib prometheus-2.1.0.linux-amd64/console_libraries/prom.lib prometheus-2.1.0.linux-amd64/prometheus.yml prometheus-2.1.0.linux-amd64/LICENSE prometheus-2.1.0.linux-amd64/NOTICE prometheus-2.1.0.linux-amd64/prometheus prometheus-2.1.0.linux-amd64/promtool # 為了方便使用,給解壓后的目錄 建個軟連接 [root@artisan ~]# ln -s prometheus-2.1.0.linux-amd64 prometheus通過指定配置文件prometheus.yml啟動Prometheus
配置文件官方說明: https://prometheus.io/docs/prometheus/latest/configuration/configuration/
默認情況下,Prometheus會監(jiān)控自己本身。
# 后臺運行 & [root@artisan prometheus]# ./prometheus --config.file=prometheus.yml & [1] 9015 [root@artisan prometheus]# level=info ts=2019-03-10T15:06:43.257570596Z caller=main.go:225 msg="Starting Prometheus" version="(version=2.1.0, branch=HEAD, revision=85f23d82a045d103ea7f3c89a91fba4a93e6367a)" level=info ts=2019-03-10T15:06:43.257669987Z caller=main.go:226 build_context="(go=go1.9.2, user=root@6e784304d3ff, date=20180119-12:01:23)" level=info ts=2019-03-10T15:06:43.257704431Z caller=main.go:227 host_details="(Linux 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 artisan (none))" level=info ts=2019-03-10T15:06:43.257735315Z caller=main.go:228 fd_limits="(soft=1024, hard=4096)" level=info ts=2019-03-10T15:06:43.263798112Z caller=main.go:499 msg="Starting TSDB ..." level=info ts=2019-03-10T15:06:43.291174989Z caller=web.go:383 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2019-03-10T15:06:43.402481602Z caller=main.go:509 msg="TSDB started" level=info ts=2019-03-10T15:06:43.402579292Z caller=main.go:585 msg="Loading configuration file" filename=prometheus.yml level=info ts=2019-03-10T15:06:43.404207598Z caller=main.go:486 msg="Server is ready to receive web requests." level=info ts=2019-03-10T15:06:43.404378748Z caller=manager.go:59 component="scrape manager" msg="Starting scrape manager..."# 查看進程, 9015端口,剛才啟動的時候也輸出了這個端口 [root@artisan prometheus]# ps -ef|grep prometheus |grep -v grep root 9015 7675 0 23:06 pts/0 00:00:00 ./prometheus --config.file=prometheus.yml查看采集到的性能指標
訪問 http://192.168.31.34:9090/metrics
太多了,真是沒法看,還好有個弱弱的圖形頁面 (待會整合到Grafana 中就方便看了)
訪問: http://192.168.31.34:9090/graph
選中某個指標,點擊Execute . 超多指標可以查看 …
查看prometheus規(guī)則
查看監(jiān)控對象
SpringBoot集成Prometheus
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.artisan</groupId><artifactId>springbootPrometheusGrafana</artifactId><version>0.0.1-SNAPSHOT</version><name>springbootPrometheusGrafana</name><description>Artisan </description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>配置文件
spring: application:name: springbootPrometheusGrafanamanagement:endpoints:web:exposure:include: '*'metrics:tags:application: ${spring.application.name}如果使用properties
spring.application.name=springbootPrometheusGrafana management.endpoints.web.exposure.include=* management.metrics.tags.application=${spring.application.name}實例化MeterRegistryCustomizer
方便起見,我們就在啟動類中實例化吧
package com.artisan;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean;import io.micrometer.core.instrument.MeterRegistry;@SpringBootApplication public class SpringbootPrometheusGrafanaApplication {public static void main(String[] args) {SpringApplication.run(SpringbootPrometheusGrafanaApplication.class, args);}@BeanMeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {return (registry) -> registry.config().commonTags("application", applicationName);}}打包,上傳到服務器上
右鍵–Run As – Maven build ,輸入 clean package
服務器上啟動該jar即可
java -jar springbootPrometheusGrafana-0.0.1-SNAPSHOT.jar &Prometheus 修改配置文件prometheus.yml 接入該工程
增加如下配置:
#SpringBoot應用配置- job_name: 'springbootPrometheusGrafana'scrape_interval: 5smetrics_path: '/actuator/prometheus'static_configs:- targets: ['192.168.31.34:8080']重啟Prometheus
[root@artisan prometheus]# ./prometheus --config.file=prometheus.yml &訪問 http://192.168.31.34:9090/targets
查看配置文件
服務自動發(fā)現
Grafana 組件
官網:https://grafana.com/
下載地址: https://grafana.com/grafana/download
入門:http://docs.grafana.org/guides/getting_started/
剛才也說了,Prometheus 的可視化功能比較弱,這里我們來接入Grafana 。
Grafana是一個跨平臺的開源的度量分析和可視化工具,可以通過將采集的數據查詢然后可視化的展示,并及時通知。它主要有以下六大特點:
-
展示方式:快速靈活的客戶端圖表,面板插件有許多不同方式的可視化指標和日志,官方庫中具有豐富的儀表盤插件,比如熱圖、折線圖、圖表等多種展示方式;
-
數據源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
-
通知提醒:以可視方式定義最重要指標的警報規(guī)則,Grafana將不斷計算并發(fā)送通知,在數據達到閾值時通過Slack、PagerDuty等獲得通知;
-
混合展示:在同一圖表中混合使用不同的數據源,可以基于每個查詢指定數據源,甚至自定義數據源;
-
注釋:使用來自不同數據源的豐富事件注釋圖表,將鼠標懸停在事件上會顯示完整的事件元數據和標記;
-
過濾器:Ad-hoc過濾器允許動態(tài)創(chuàng)建新的鍵/值過濾器,這些過濾器會自動應用于使用該數據源的所有查詢。
下載 & 安裝 & 啟動Grafana
官網下載速度太慢,我這里就沒有使用最新版本,而是從 http://cactifans.hi-www.com 下載了 grafana-5.4.2-1.x86_64.rpm的版本
# 下載 [root@artisan ~]# wget http://cactifans.hi-www.com/grafana/grafana-5.4.2-1.x86_64.rpm --2019-03-10 23:44:28-- http://cactifans.hi-www.com/grafana/grafana-5.4.2-1.x86_64.rpm Resolving cactifans.hi-www.com (cactifans.hi-www.com)... 222.186.135.67 Connecting to cactifans.hi-www.com (cactifans.hi-www.com)|222.186.135.67|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 54991977 (52M) [application/x-redhat-package-manager] Saving to: ‘grafana-5.4.2-1.x86_64.rpm’100%[===========================================================================================================================================>] 54,991,977 5.20MB/s in 9.5s 2019-03-10 23:44:37 (5.51 MB/s) - ‘grafana-5.4.2-1.x86_64.rpm’ saved [54991977/54991977]# yum本地安裝 [root@artisan ~]# yum localinstall grafana-5.4.2-1.x86_64.rpm Loaded plugins: fastestmirror, langpacks Examining grafana-5.4.2-1.x86_64.rpm: grafana-5.4.2-1.x86_64 Marking grafana-5.4.2-1.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package grafana.x86_64 0:5.4.2-1 will be installed --> Finished Dependency Resolution base/7/x86_64 | 3.6 kB 00:00:00 extras/7/x86_64 | 3.4 kB 00:00:00 extras/7/x86_64/primary_db | 180 kB 00:00:00 updates/7/x86_64 | 3.4 kB 00:00:00 updates/7/x86_64/primary_db | 2.5 MB 00:00:00 Dependencies Resolved=====================================================================================================================================================================================Package Arch Version Repository Size ===================================================================================================================================================================================== Installing:grafana x86_64 5.4.2-1 /grafana-5.4.2-1.x86_64 151 MTransaction Summary ===================================================================================================================================================================================== Install 1 PackageTotal size: 151 M Installed size: 151 M Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transactionInstalling : grafana-5.4.2-1.x86_64 1/1 ### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemdsudo /bin/systemctl daemon-reloadsudo /bin/systemctl enable grafana-server.service ### You can start grafana-server by executingsudo /bin/systemctl start grafana-server.service POSTTRANS: Running scriptVerifying : grafana-5.4.2-1.x86_64 1/1 Installed:grafana.x86_64 0:5.4.2-1 Complete!# 啟動 [root@artisan ~]# systemctl start grafana-server # 設置為開機啟動 [root@artisan ~]# systemctl enable grafana-server Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service. # 查看進程 [root@artisan ~]# ps -ef|grep grafana-server |grep -v grep grafana 9542 1 2 23:49 ? 00:00:00 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=rpm cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins cfg:default.paths.provisioning=/etc/grafana/provisioning [root@artisan ~]#配置文件 /etc/grafana/grafana.ini , 默認3000端口,按需修改
訪問 http://192.168.31.34:3000/login
默認的用戶名和密碼為 admin/admin
Grafana 接入Prometheus 的數據
如何將 Prometheus 數據 添加到 Grafana 中,如下
Prometheus 官方指導 : https://prometheus.io/docs/visualization/grafana/
Step1 添加Prometheus 數據源
選擇 Prometheus
Step2 設置相關信息
Step3 導入想要的dashboards
看板地址: https://grafana.com/dashboards
可篩選
https://grafana.com/dashboards/4701
導入
總結
以上是生活随笔為你收集整理的Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 并发编程-26 高并发处理手段之服务降级
- 下一篇: Nginx-从零开始使用nginx实现反