Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然
作者:烟味i
链接:https://www.cnblogs.com/2YSP/p/12827487.html
一、背景
Spring Boot 的应用监控方案比较多,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版本的能否支持。
推荐一个 Spring Boot 基础教程及实战示例:
https://github.com/javastacks/spring-boot-best-practice
MonitorDemoApplication启动类增加注解
package cn.sp; import io.prometheus.client.spring.boot.EnablePrometheusEndpoint; import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @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中增加堆内存使用量,方便后面测试邮件报警。
Spring Boot 基础就不介绍了,推荐下这个实战教程:
https://github.com/javastacks/spring-boot-best-practice
启动项目后,可以在IDEA中看到有很多Endpoints,如图:
开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的, 而这个需要 idea2017.2版本以上才能看到。Intellij IDEA 顺利激活推荐你看下。。
后来只好重新下载安装,弄了好久。。。。
启动完毕,访问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文件,配置数据采集的目标信息。
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 + 端口号 更多配置信息请查看官方文档。
Java技术栈专注分享Java技术干货,包括多线程、JVM、Spring Boot、Spring Cloud、Intellij IDEA、Dubbo、Zookeeper、Redis、架构设计、微服务、消息队列、Git、面试题、程序员攻略、最新动态等。496篇原创内容
公众号
现在可以启动Prometheus了,命令行输入:prometheus.exe --config.file=prometheus.yml 访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。
四、安装Grafana
下载地址点击这里,本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。最新面试题整理好了,点击Java面试库小程序在线刷题。
解压后运行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、钉钉等,这里以邮件为例。最新面试题整理好了,点击Java面试库小程序在线刷题。
第二步: 邮箱配置
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的表达式有点多。
版权声明:
作者: FreeClashNode
链接: https://www.freeclashnode.com/news/article-947.htm
来源: www.freeclashnode.com
文章版权归作者所有,未经允许请勿转载。
热门文章
- 10月1日免费节点分享|18M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 10月2日免费节点分享|22.8M/S,Singbox节点/Shadowrocket节点/Clash节点/V2ray节点|免费上网梯子每天更新
- 10月3日免费节点分享|20.1M/S,SSR节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 9月30日免费节点分享|19.1M/S,Clash节点/V2ray节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月6日免费节点分享|19.2M/S,V2ray节点/Singbox节点/Shadowrocket节点/Clash节点|免费上网梯子每天更新
- 10月8日免费节点分享|22.5M/S,V2ray节点/Singbox节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 10月11日免费节点分享|18.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月5日免费节点分享|19.9M/S,Singbox节点/Clash节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 9月29日免费节点分享|22M/S,SSR节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
最新文章
- 跨越知识边界:浙大学子的全球学术网络探索指南
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月24日免费节点分享|20.5M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 刘大猫科学上网全攻略:解锁全球网络的终极指南
- 10月23日免费节点分享|22.2M/S,Shadowrocket节点/Clash节点/Singbox节点/V2ray节点|免费上网梯子每天更新
- 全面解析Quantumult安卓版:功能、安装与配置教程
- 双重防护,畅享无界:AdGuard联手V2RayNG,打造极致安全的上网体验
- 10月22日免费节点分享|18.8M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 掌握Clash白名单设置:精准控制代理流量的艺术
- 10月21日免费节点分享|22M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 突破网络边界:技术科学上网的终极安全指南
- 10月20日免费节点分享|22.9M/S,SSR节点/V2ray节点/Clash节点/Singbox节点|免费上网梯子每天更新
- Shadowrocket 选购指南:从入门到精通,找到你的专属代理神器
- 10月19日免费节点分享|19.3M/S,Singbox节点/V2ray节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 深入解读:V2Ray为何比SSR更安全?一场网络隐私的全方位较量
- 10月18日免费节点分享|20.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度指南:Shadowrocket从入门到精通——iOS用户的网络自由密钥
- 10月17日免费节点分享|18M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 深入解析科学上网套:全方位使用指南与常见问题解答
- 10月16日免费节点分享|22M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月15日免费节点分享|21.2M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 全局掌控:安卓设备上 Clash 全局模式完整设置指南与深度解析
- 解锁全球游戏体验:育碧平台科学上网全攻略与深度解析
- 10月14日免费节点分享|22.5M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 华为Mate 10科学上网全攻略:突破限制,畅游全球网络
- 10月13日免费节点分享|22.1M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 10月12日免费节点分享|22.7M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 量子ULT全方位指南:从下载配置到高阶玩法一网打尽
- 10月11日免费节点分享|18.6M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 极路由V2ray插件完全指南:打造家庭网络的安全屏障
- 10月10日免费节点分享|18.3M/S,V2ray节点/Singbox节点/Clash节点/SSR节点|免费上网梯子每天更新
- 极速狂飙与正义追击:《警车 Clash》深度解析与高阶攻略
- 10月9日免费节点分享|22.3M/S,Singbox节点/Clash节点/Shadowrocket节点/V2ray节点|免费上网梯子每天更新
- 用hosts文件科学上网:一份从入门到实战的详细指南
- 10月8日免费节点分享|22.5M/S,V2ray节点/Singbox节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- iOS设备上SSR科学上网全教程:从入门到精通
- 10月7日免费节点分享|23M/S,Singbox节点/Clash节点/Shadowrocket节点/V2ray节点|免费上网梯子每天更新
- 深度剖析V2Ray订阅机制:从零搭建高效、安全的科学上网环境
- 10月6日免费节点分享|19.2M/S,V2ray节点/Singbox节点/Shadowrocket节点/Clash节点|免费上网梯子每天更新
- 全面解析蚕蛹生态冲突及其多元价值
- 10月5日免费节点分享|19.9M/S,Singbox节点/Clash节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 从零开始:手把手教你刷梅林固件实现安全科学上网
- 10月4日免费节点分享|20M/S,SSR节点/V2ray节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 跨越边界,自由探索:一加科学上网全面指南与深度解析
- 10月3日免费节点分享|20.1M/S,SSR节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月2日免费节点分享|22.8M/S,Singbox节点/Shadowrocket节点/Clash节点/V2ray节点|免费上网梯子每天更新
- 斐讯K2路由器刷机全攻略:解锁科学上网新境界
- 解锁网络自由:B站d手机科学上网全攻略
- 10月1日免费节点分享|18M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 洞悉网络脉络:一份关于Vmess节点测试的全景式实践手记
归档
- 2026-10 49
- 2026-09 58
- 2026-08 63
- 2026-07 61
- 2026-06 55
- 2026-05 56
- 2026-04 51
- 2026-03 60
- 2026-02 52
- 2026-01 56
- 2025-12 59
- 2025-11 55
- 2025-10 56
- 2025-09 55
- 2025-08 49
- 2025-07 31
- 2025-06 30
- 2025-05 31
- 2025-04 30
- 2025-03 388
- 2025-02 360
- 2025-01 403
- 2024-12 403
- 2024-11 390
- 2024-10 403
- 2024-09 388
- 2024-08 402
- 2024-07 427
- 2024-06 442
- 2024-05 181
- 2024-04 33
- 2024-03 31
- 2024-02 29
- 2024-01 51
- 2023-12 52
- 2023-11 32
- 2023-10 32
- 2023-09 3