一、背景

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


关系图

二、开发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> <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
<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启动类增加注解

  1. package cn.sp; 


  2. import io.prometheus.client.spring.boot.EnablePrometheusEndpoint; 

  3. import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector; 

  4. import org.springframework.boot.SpringApplication; 

  5. import org.springframework.boot.autoconfigure.SpringBootApplication; 

  6. @EnablePrometheusEndpoint 

  7. @EnableSpringBootMetricsCollector 

  8. @SpringBootApplication 

  9. public class MonitorDemoApplication { 


  10. public static void main(String[] args) { 

  11. SpringApplication.run(MonitorDemoApplication.class, args); 







配置文件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

@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,如图:


enter description here

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

而这个需要 idea2017.2版本以上才能看到。

后来只好重新下载安装,弄了好久。。。。

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


监控指标

注意:

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

三、安装Prometheus

下载地址点击这里,本文下载的是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

下载地址点击这里,本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。

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

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

1.设置数据源

2. 创建一个Dashboard


enter description here

3. 填写采集的指标点

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


采集指标

4.选择图表样式

5.填写标题描述

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


结果展示

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

五、添加邮件报警

在实际项目中当监控的某的个指标超过阈值(比如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 = xxxx@ininin.com
# 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 = xxxx@ininin.com
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接口后,内存升高大于设置的阈值,然后就收到报警邮件。


报警邮件

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

六、总结

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

附上几个链接:

Prometheus官方文档

Grafana官方文档

代码地址

SpringBoot+Prometheus+Grafana实现应用监控和报警的更多相关文章

  1. Prometheus+Grafana通过kafka_exporter监控kafka

    Prometheus+Grafana通过kafka_exporter监控kafka 一.暴露 kafka-metric 方式 二.jmx_exporter方式 2.1 下载jmx_prometheus ...

  2. prometheus + grafana部署RabbitMQ监控

    prometheus + grafana部署RabbitMQ监控 1.grafana导入dashboards https://grafana.com/dashboards/2121   2.expor ...

  3. 【Prometheus+Grafana系列】监控MySQL服务

    前言 前面的一篇文章已经介绍了 docker-compose 搭建 Prometheus + Grafana 服务.当时实现了监控服务器指标数据,是通过 node_exporter.Prometheu ...

  4. Prometheus+Grafana+Altermanager搭建监控系统

    基本概念 Prometheus 时间序列化数据库,我的理解就是将数据打上标签,以时间维度存储.后面有机会在深入研究. Prometheus架构如下: Grafana Prometheus中存储的数据, ...

  5. 基于Prometheus+Grafana+AlertManager的监控系统

    一.Prometheus 1.1 简介 Prometheus是一套开源的监控&报警&时间序列数据库的组合,基于应用的metrics来进行监控的开源工具 . 1.2 下载&安装 ...

  6. 使用prometheus+ grafana+nginx-module-vts 模块监控openresty

      nginx-module-vts 是一个很不错的nginx 模块,我们可以用来,方便的分析系统的请求状态 同时支持基于prometheus 的监控, 我参考openresty 的docker镜像已 ...

  7. Prometheus+Grafana打造Mysql监控平台

    prometheus/node_exporter/mysqld_exporter都是由go语言编写,需要先安装GoLang环境 下载node_exporter(监控服务器的CPU.内存.存储使用情况) ...

  8. prometheus+grafana实现服务监控

    一.安装prometheus: 下载相应的版本 :https://prometheus.io/download/ 解压: Linux:tar -zxvf XXX.tar.gz windows:直接下载 ...

  9. 【Linux】【Services】【SaaS】Docker+kubernetes(12. 部署prometheus/grafana/Influxdb实现监控)

    1.简介 1.1. 官方网站: promethos:https://prometheus.io/ grafana:https://grafana.com/ 1.2. 架构图 2. 环境 2.1. 机器 ...

随机推荐

  1. 推荐4个Flutter重磅开源项目

    早上好,骚年,我是小 G,我的公众号「菜鸟翻身」会推荐 GitHub 上有用的项目,一分钟 get 一个优秀的开源项目,挖掘开源的价值,欢迎关注我. 近年来,随着移动智能设备的快速普及,移动多端统一开 ...

  2. bugkuctf 这 是 一 个 神 奇 的 登 录 界 面

    首先结合源码可以看出这是一道sql注入题. 然后开始萌新的日常操作,尝试单引号闭合,可是并没有用,而且因为单引号注入的题太多,导致并没有立刻开始尝试双引号,之后想起双引号(对,双引号木得牌面)得到如下 ...

  3. Android夜神模拟器

    夜神安卓模拟器 NOX,是一个可以让手机应用程序运行在电脑上的软件,也是电脑玩手游的新一代神器, 与传统安卓模拟器相比,基于基于Android5.1.1,兼容X86/AMD,在性能.稳定性.兼容性等方 ...

  4. Scrum 冲刺 第三篇

    Scrum 冲刺 第三篇 每日会议照片 昨天已完成工作 队员 昨日完成任务 黄梓浩 继续完成app项目架构搭建 黄清山 完成部分个人界面模块数据库的接口 邓富荣 完成部分后台首页模块数据库的接口 钟俊 ...

  5. 半夜删你代码队 Day7冲刺

    一.每日站立式会议 1.站立式会议 成员 昨日完成工作 今日计划工作 遇到的困难 陈惠霖 好友界面初步 完善好友界面 无 侯晓龙 帮助他人建立数据库 用户信息界面 无 周楚池 完善管理员界面 用户界面 ...

  6. 【题解】Fuzzy Google Suggest(UVA1462)

    题目链接 题意 给定一个字符串集合,有n次搜索,每次有一个整数x和一个字符串,表示可以对字符串进行x次修改, 包括增加.修改和删除一个字符,问修改后的字符串可能是字符集中多少个字符串的前缀. 思路 简 ...

  7. Java设计模式(一)——单例模式

    简介 定义: 确保一个类只有一个实例,并提供一个全局访问点来访问这个唯一实例. 单例类拥有一个私有构造函数,确保用户无法通过 new 来直接实例化它.类中包含一个静态私有成员变量与静态公有的工厂方法, ...

  8. hive的调优策略

    hive有时执行速度很慢,若hive on spark 的话,在sparkUI上可以清楚看到是否数据倾斜 优化方法: 1.增加reduce数目 hive.exec.reducers.bytes.per ...

  9. Java8的StreamAPI常用方法总结

    目录 什么是Stream? Stream的创建 测试API 新建测试数据 findFirst.findAny anyMatch.noneMatch filter max.count peek.map ...

  10. gnuplot设置字体及大小

    set term png font 'times.ttf,14'set fontpath '/home/peter/.fonts'set output 'vel-cost.eps'set gridse ...