当面对一个简单的零碎时,咱们往往须要监控工具来帮忙咱们解决一些性能问题。比方之前咱们应用SpringBoot Admin来监控利用,从而获取到SpringBoot Actuator裸露的指标信息。明天给大家介绍一个功能强大的监控工具Grafana,只有须要用到监控的中央,用它做可视化就对了!

SpringBoot实战电商我的项目mall(50k+star)地址:https://github.com/macrozheng/mall

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,把宿主机中的配置文件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曾经装置完后,是时候来波实际了,接下来咱们来介绍下应用Grafana来监控Linux零碎和SpringBoot利用。

监控零碎信息

应用node_explorer能够裸露Linux零碎的指标信息,而后Prometheus就能够通过定时扫描的形式获取并存储指标信息了。
  • 下载node_explorer的安装包,下载地址:https://prometheus.io/downloa...

  • 这次咱们间接把node_explorer装置到Linux服务器上(如果应用Docker容器装置,监控的会是Docker容器的指标信息),将下载的安装包解压到指定目录,并批改文件夹名称:
cd /mydatatar -zxvf node_exporter-1.1.2.linux-amd64.tar.gzmv 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 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的配置文件prometheus.yml,创立一个工作定时扫描node_explorer裸露的指标信息;
scrape_configs:  - job_name: node    static_configs:    - targets: ['192.168.5.78:9100']
  • 重启Prometheus容器,能够通过加号->Dashboard来创立仪表盘;

  • 当然你还能够抉择去Grafana的仪表盘市场下载一个Dashboard,市场地址:https://grafana.com/grafana/d...

  • 这里抉择了Node Exporter Full这个仪表盘,记住它的ID,拜访地址:https://grafana.com/grafana/d...

  • 抉择导入Dashboard并输出ID,最初点击Load即可;

  • 抉择数据源为Prometheus,最初点击Import

  • 导入胜利后就能够在Grafana中看到实时监控信息了,是不是够炫酷!

监控SpringBoot利用

监控SpringBoot利用须要依附actuatormicrometer,通过裸露actuator的端点,Prometheus能够定时获取并存储指标信息。
  • 批改我的项目的pom.xml文件,增加actuatormicrometer依赖;
<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>    <!-- 集成micrometer,将监控数据存储到prometheus -->    <dependency>        <groupId>io.micrometer</groupId>        <artifactId>micrometer-registry-prometheus</artifactId>    </dependency></dependencies>
  • 批改利用配置文件application.yml,通过actuator裸露监控端口/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的配置文件prometheus.yml,创立一个工作定时扫描actuator裸露的指标信息,这里须要留神下,因为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是否能获取到指标信息;

  • 同样,咱们能够从仪表盘市场导入仪表盘,拜访地址:https://grafana.com/grafana/d...

  • 导入胜利后就能够在Grafana中看到SpringBoot实时监控信息了,果然够炫酷!

总结

通过对Grafana的一波实际,咱们能够发现,应用Grafana来进行数据可视化的过程是这样的:首先咱们得让被监控方将指标信息裸露进去,而后用Prometheus定时获取并存储指标信息,最初将Prometheus配置为Grafana的可视化数据源。

参考资料

  • Grafana官网文档:https://grafana.com/docs/graf...
  • node-exporter的应用:https://prometheus.io/docs/gu...

我的项目源码地址

https://github.com/macrozheng...

本文 GitHub https://github.com/macrozheng/mall-learning 曾经收录,欢送大家Star!