Redian新闻
>
SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!

SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!

公众号新闻


1.背景

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

关系图

2.开发SpringBoot应用

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

<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.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
注意:这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。

MonitorDemoApplication启动类增加注解

@EnablePrometheusEndpoint 
@EnableSpringBootMetricsCollector 
@SpringBootApplication 
public class MonitorDemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MonitorDemoApplication.class, args);
    }
 
}

配置文件application.yml

server: 
  port: 8848 
spring:
  application:
    name: monitor-demo
 
security:
  user:
    name: admin
    password: 1234 
  basic:
    enabled: true
    # 安全路径列表,逗号分隔,此处只针对/admin路径进行认证
    path: /admin
 
# actuator暴露接口的前缀
management:
  context-path: /admin
  # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离
  port: 8888 
  security:
    enabled: true
    roles: SUPERUSER

测试代码TestController

@RequestMapping("/heap/test")
@RestController
public class TestController {

    public static final Map<String, Object> map = new ConcurrentHashMap<>();

    @RequestMapping("")
    public String testHeapUsed() {
        for (int i = 0; i < 10000000; i++) {
            map.put(i + "", new Object());
        }
        return "ok";
    }
}
这里的逻辑就是在请求这个接口后,创建大量对象保存到map中增加堆内存使用量,方便后面测试邮件报警。
启动项目后,可以在IDEA中看到有很多Endpoints,如图:

开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的,

而这个需要 idea2017.2版本以上才能看到。
后来只好重新下载安装,弄了好久。

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

监控指标

注意:

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

3.安装Prometheus

本文下载的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。

下载地址:https://prometheus.io/download/

解压后修改prometheus.yml文件,配置数据采集的目标信息。
scrape_configs: 
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  # - job_name: 'prometheus' 
 
    # metrics_path defaults to '/metrics' 
    # scheme defaults to 'http'.
 
    # static_configs:
    # - targets: ['localhost:9090']
  - job_name: 'monitor-demo' 
    scrape_interval: 5s # 刮取的时间间隔
    scrape_timeout: 5s  
    metrics_path: /admin/prometheus
    scheme: http
    basic_auth: #认证信息
      username: admin
      password: 1234 
    static_configs:
      - targets:
        - 127.0.0.1:8888  #此处填写 Spring Boot 应用的 IP + 端口号
更多配置信息请查看官方文档。
现在可以启动Prometheus了,命令行输入:
prometheus.exe --config.file=prometheus.yml
访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。

采集目标信息

4.安装Grafana

本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。

下载地址:https://grafana.com/grafana/download
解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin
现在开始创建自己的可视化监控面板。

1.设置数据源

2. 创建一个Dashboard

3. 填写采集的指标点

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

4.选择图表样式

5.填写标题描述

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

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

5.添加邮件报警

在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。
第一步:点击[Alerting]——>[Notification channels]添加通知通道

这里的Type有很多选项,包括webhook、钉钉等,这里以邮件为例。
第二步:邮箱配置

Grafana默认使用conf目录下defaults.ini作为配置文件运行,根据官方的建议我们不要更改defaults.ini而是在同级目录下新建一个配置文件custom.ini。

以腾讯企业邮箱为例,配置如下:
################### SMTP / Emailing ###################
[smtp]
enabled = true
host = smtp.exmail.qq.com:465
user = [email protected]
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = XXX
cert_file =
key_file =
skip_verify = true
from_address = [email protected]
from_name = Grafana
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接口后,内存升高大于设置的阈值,然后就收到报警邮件。

报警邮件

这里图片没有显示出来,搞不懂为什么。

6.总结

这套监控功能还是挺强大的,就是Prometheus的表达式有点多。

文档:

https://prometheus.io/docs/introduction/first_steps/

https://grafana.com/docs/grafana/latest/

https://github.com/2YSP/monitor-demo

来源:https://www.cnblogs.com/2YSP/p/12827487.html



联合一线大厂朋友花费8个月的时间,录制了一份Java入门+进阶视频教程

课程特色:

  1. 总共88G,时常高达365小时,覆盖所有主流技术栈

  2. 均为同一人录制,不是东拼西凑的

  3. 对标线下T0级别的培训课,讲师大厂架构师,多年授课经验,通俗易懂

  4. 内容丰富,每一个技术点除了视频,还有课堂源码、笔记、PPT、图解

  5. 五大实战项目(视频+源码+笔记+SQL+软件)

  6. 一次付费,持续更新,永无二次费用

点击下方超链接查看详情(或者点击文末阅读原文):

(点击查看)  88G,超全技术栈的Java入门+进阶+实战!


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
SpringBoot 接口加密解密,新姿势!Prompt总结 | 从MLM预训任务到Prompt Learning原理解析与Zero-shot分类、NER简单实践SpringBoot + Flyway,自动化实现数据库版本控制基于 Spring Boot + Vue 实现的可视化拖拽编辑的大屏项目SpringBoot 3.0正式发布,有这几个新变化!别再用 if 校验参数了,太Low!这才是专业的 SpringBoot 参数校验方式!SpringBoot 接口数据加解密技巧,so easy!毛泽东是第一个与官国决裂的人如何快速构建Prometheus监控体系,架构、指标、数据、告警… | 极客时间九大投行|Deutsche Bank 2023 China Graduate Programme 火热进行中!The Chinese Online Slang That Took Over the Internet in 2022Combo(i7-9700f/16gram 2666mhz/msi z390-a pro motherboard)Prometheus Metric 的实践总结,搞定监控需注意~Prometheus API 使用介绍IKEA x OBEGRÄNSAD联名!宜家22年最受瞩目系列开售!主流监控系统 Prometheus 学习指南SpringBoot+Prometheus+Grafana 实现自定义监控SpringBoot 官方推荐,连接池,太快了!用Prometheus监控K8s,从核心原理到告警实操都讲明白了 | 极客时间云原生监控报警可视化全职妈妈招惹谁了?【庭院种菜】自家菜地,别用这种有机肥!穷山恶水出刁民,绿水青山养文人(1)这16个有用的 SpringBoot 扩展接口,居然还有人不知道?Recommend somebody to do something是错误说法吗?SpringBoot 整合 Groovy 脚本,实现动态编程平价买到高级感!IKEA全新 OBEGRÄNSAD系列,全系列都好看!李克强内部讲话(有待核实)快速定位SpringBoot接口超时问题的神器!性能爆表:SpringBoot利用ThreadPoolTaskExecutor批量插入百万级数据实测!PyTorch下的可视化工具(网络结构/训练过程可视化)SpringBoot四大核心组件,你知道几个?这样做优化,实现 0.059s 启动一个SpringBoot项目!Nginx 可视化神器!复杂配置一键生成,监控管理一条龙!SpringBoot超大文件上传,实现秒传
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。