如何使用 Spring Boot Prometheus Grafana 来打造可视化监控?

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 我是小假 期待与你的下一次相遇 ~

一、背景

SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一。它们三者之间的关系大概如下图:

二、开发SpringBoot应用

首先,创建一个SpringBoot项目,pom文件如下:

  1. <dependency>
  2.  <groupId>org.springframework.boot</groupId>
  3.  <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5. <dependency>
  6.  <groupId>org.springframework.boot</groupId>
  7.  <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10.  <groupId>org.projectlombok</groupId>
  11.  <artifactId>lombok</artifactId>
  12.  <optional>true</optional>
  13. </dependency>
  14. <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
  15. <dependency>
  16.  <groupId>io.prometheus</groupId>
  17.  <artifactId>simpleclient_spring_boot</artifactId>
  18.  <version>0.8.1</version>
  19. </dependency>
  20. <dependency>
  21.  <groupId>org.springframework.boot</groupId>
  22.  <artifactId>spring-boot-starter-security</artifactId>
  23. </dependency>

注意: 这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。

MonitorDemoApplication启动类增加注解

  1. import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
  2. import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @EnablePrometheusEndpoint
  6. @EnableSpringBootMetricsCollector
  7. @SpringBootApplication
  8. public class MonitorDemoApplication {
  9.    public static void main(String[] args) {
  10.        SpringApplication.run(MonitorDemoApplication.class, args);
  11.    }
  12. }

配置文件application.yml

  1. server:
  2.  port: 8848
  3. spring:
  4.  application:
  5.    name: monitor-demo
  6. security:
  7.  user:
  8.    name: admin
  9.    password: 1234
  10.  basic:
  11.    enabled: true
  12.    # 安全路径列表,逗号分隔,此处只针对/admin路径进行认证
  13.    path: /admin
  14. # actuator暴露接口的前缀
  15. management:
  16.  context-path: /admin
  17.  # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离
  18.  port: 8888
  19.  security:
  20.    enabled: true
  21.    roles: SUPERUSER

测试代码TestController

  1. @RequestMapping("/heap/test")
  2. @RestController
  3. public class TestController {
  4.    public static final Map<String, Object> map = new ConcurrentHashMap<>();
  5.    @RequestMapping("")
  6.    public String testHeapUsed() {
  7.        for (int i = 0; i < 10000000; i++) {
  8.            map.put(i + "", new Object());
  9.        }
  10.        return "ok";
  11.    }
  12. }

这里的逻辑就是在请求这个接口后,创建大量对象保存到map中增加堆内存使用量,方便后面测试邮件报警。

启动项目后,可以在IDEA中看到有很多Endpoints,如图:

启动完毕,访问http://localhost:8888/admin/prometheus就可以看到服务暴露的那些监控指标了。

注意:由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus

三、安装Prometheus

下载地址https://prometheus.io/download/,本文下载的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。

解压后修改prometheus.yml文件,配置数据采集的目标信息。

  1. scrape_configs:
  2.  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  3.  # - job_name: 'prometheus'
  4.    # metrics_path defaults to '/metrics'
  5.    # scheme defaults to 'http'.
  6.    # static_configs:
  7.    # - targets: ['localhost:9090']
  8.  - job_name: 'monitor-demo'
  9.    scrape_interval: 5s # 刮取的时间间隔
  10.    scrape_timeout: 5s  
  11.    metrics_path: /admin/prometheus
  12.    scheme: http  
  13.    basic_auth: #认证信息
  14.      username: admin
  15.      password: 1234
  16.    static_configs:
  17.      - targets:
  18.        - 127.0.0.1:8888  #此处填写 Spring Boot 应用的 IP + 端口号

更多配置信息请查看官方文档。

现在可以启动Prometheus了,命令行输入:prometheus.exe --config.file=prometheus.yml 访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。

采集目标信息

四、安装Grafana

下载地址https://grafana.com/grafana/download,这里用到的是Windows版本grafana-6.3.3.windows-amd64.zip。

解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin。

现在开始创建自己的可视化监控面板。

1.设置数据源

2. 创建一个Dashboard

3. 填写采集的指标点

注意: 这里的指标点不能随便填,必须是已有的可以在 Prometheus看到。

4.选择图表样式

5.填写标题描述

最后点击右上角的保存,输入Dashboad的名称即可。

Tips: 这里的图表布局是可以用鼠标拖动的

五、添加邮件报警

在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。

第一步: 点击[Alerting]——>[Notification channels]添加通知通道

这里的Type有很多选项,包括webhook、钉钉等,这里以邮件为例。

第二步: 邮箱配置

Grafana默认使用conf目录下defaults.ini作为配置文件运行,根据官方的建议不要更改defaults.ini而是在同级目录下新建一个配置文件custom.ini。以腾讯企业邮箱为例,配置如下:

  1. #################################### SMTP / Emailing #####################
  2. [smtp]
  3. enabled = true
  4. host = smtp.exmail.qq.com:465
  5. user = xxxx@ininin.com
  6. # If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
  7. password = XXX
  8. cert_file =
  9. key_file =
  10. skip_verify = true
  11. from_address = xxxx@ininin.com
  12. from_name = Grafana
  13. ehlo_identity = ininin.com

然后需要重启Grafana,命令grafana-server.exe -config=E:\file\grafana-6.3.3\conf\custom.ini

第三步: 为指标添加alert

配置预警规则

配置通知方式和信息

Evaluate every 表示检测评率,这里为了测试效果,改为1秒 For 如果警报规则配置了For,并且查询违反了配置的阈值,那么它将首先从OK变为Pending。从OK到Pending Grafana不会发送任何通知。一旦警报规则的触发时间超过持续时间,它将更改为Alerting并发送警报通知。 Conditions when 表示什么时间,of 表示条件,is above 表示触发值 同时,设置了is above后会有一条红线。 If no data or all values are null 如果没有数据或所有值都为空,这里选择触发报警 If execution error or timeout 如果执行错误或超时,这里选择触发报警

注意: 下一次触发,比如10秒后,它不会再次触发,防止报警风暴产生!

第四步: 测试

请求http://localhost:8848/heap/test接口后,内存升高大于设置的阈值,然后就收到报警邮件。


相关文章
|
3月前
|
Prometheus 监控 Cloud Native
云原生监控实战:Prometheus+Grafana快速搭建指南
云原生监控实战:Prometheus+Grafana快速搭建指南
|
3月前
|
存储 Prometheus 监控
OSS监控体系搭建:Prometheus+Grafana实时监控流量、错误码、存储量(开源方案替代云监控自定义视图)
本方案基于Prometheus构建OSS监控系统,涵盖架构设计、指标采集、可视化、告警及性能优化,助力企业实现高可用、低成本的自建监控体系。
340 1
|
4月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
336 79
|
3月前
|
存储 监控 Cloud Native
云原生监控实战:Prometheus+Grafana打造RDS多维度预警体系
本方案构建了基于Prometheus与Thanos的云原生RDS监控体系,涵盖数据采集、存储、可视化与告警全流程。支持10万+QPS采集、90%存储压缩,具备&lt;30秒告警延迟能力。通过自定义指标与智能预警策略,显著提升故障发现效率,实现分钟级响应。
241 5
|
2月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
|
10月前
|
Prometheus 运维 监控
智能运维实战:Prometheus与Grafana的监控与告警体系
【10月更文挑战第26天】Prometheus与Grafana是智能运维中的强大组合,前者是开源的系统监控和警报工具,后者是数据可视化平台。Prometheus具备时间序列数据库、多维数据模型、PromQL查询语言等特性,而Grafana支持多数据源、丰富的可视化选项和告警功能。两者结合可实现实时监控、灵活告警和高度定制化的仪表板,广泛应用于服务器、应用和数据库的监控。
880 3
|
6月前
|
Prometheus Kubernetes 监控
Kubernetes监控:Prometheus与AlertManager结合,配置邮件告警。
完成这些步骤之后,您就拥有了一个可以用邮件通知你的Kubernetes监控解决方案了。当然,所有的这些配置都需要相互照应,还要对你的Kubernetes集群状况有深入的了解。希望这份指南能帮助你创建出适合自己场景的监控系统,让你在首次发现问题时就能做出响应。
251 22
|
9月前
|
存储 数据采集 Prometheus
Grafana Prometheus Altermanager 监控系统
Grafana、Prometheus 和 Alertmanager 是一套强大的开源监控系统组合。Prometheus 负责数据采集与存储,Alertmanager 处理告警通知,Grafana 提供可视化界面。本文简要介绍了这套系统的安装配置流程,包括各组件的下载、安装、服务配置及开机自启设置,并提供了访问地址和重启命令。适用于希望快速搭建高效监控平台的用户。
433 20
|
9月前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
715 7
|
9月前
|
Prometheus 运维 监控
Prometheus+Grafana+NodeExporter:构建出色的Linux监控解决方案,让你的运维更轻松
本文介绍如何使用 Prometheus + Grafana + Node Exporter 搭建 Linux 主机监控系统。Prometheus 负责收集和存储指标数据,Grafana 用于可视化展示,Node Exporter 则采集主机的性能数据。通过 Docker 容器化部署,简化安装配置过程。完成安装后,配置 Prometheus 抓取节点数据,并在 Grafana 中添加数据源及导入仪表盘模板,实现对 Linux 主机的全面监控。整个过程简单易行,帮助运维人员轻松掌握系统状态。
1143 3