Spring Boot 实现应用监控和报警 绝地灬酷狼 2021-09-07 06:14 432阅读 0赞 ![fd37a5b8d6d1227c3df4fd5107c2e501.gif][] Spring Boot 的应用监控方案比较多,Spring Boot+Prometheus+Grafana是目前比较常用的方案之一。它们三者之间的关系大概如下图: ![关系图][3b01e6be31b4c3d94bcfdd0c9969fe13.png] **01 开发 Spring Boot 应用** 首先,创建一个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> **注意:** 这里的 Spring Boot 版本是 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,如图: ![enter description here][] 开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的, 而这个需要 idea2017.2版本以上才能看到。 后来只好重新下载安装,弄了好久。 启动完毕,访问http://localhost:8888/admin/prometheus就可以看到服务暴露的那些监控指标了。 ![监控指标][b9ed141bd8a0cd36338a68c3de8452c6.png] **注意:** 由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下你的请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus **02 安装Prometheus** 下载地址点击这里https://prometheus.io/download/,本文下载的是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 + 端口号 更多配置信息请查看官方文档。 现在可以启动Prometheus了,命令行输入:prometheus.exe --config.file=prometheus.yml, 访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。 ![采集目标信息][95b802d0df6131496019c7e9429a2c45.png] **03 安装Grafana** 下载地址点击这里:https://grafana.com/grafana/download,本文用到的是 Windows 版本 grafana-6.3.3.windows-amd64.zip。 解压后运行bin目录下的 grafana-server.exe 启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin。现在开始创建自己的可视化监控面板。 **1.设置数据源** ![enter description here][enter description here 1] **2. 创建一个Dashboard** ![enter description here][enter description here 2] ![enter description here][enter description here 3] **3. 填写采集的指标点** ![enter description here][enter description here 4] **注意:** 这里的指标点不能随便填,必须是已有的可以在 Prometheus看到。 ![采集指标][b6fc8eec680cdc60642e7b05914126b5.png]**4.选择图表样式** ![enter description here][enter description here 5] **5. 填写标题描述** ![enter description here][enter description here 6] 最后点击右上角的保存,输入Dashboad的名称即可。 ![结果展示][088d0d4d2b93a67861ce3b2a7bd38dd7.png] **Tips:** 这里的图表布局是可以用鼠标拖动的 **04 添加邮件报警** 在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。 **第一步:** 点击\[Alerting\]——>\[Notification channels\]添加通知通道 ![创建通道][09813553ff634a1c3f66c81624699c79.png] ![编辑][4bb44e213efe8503a2fd6588bd603fb8.png] 这里的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 配置预警规则 ![配置预警规则][2a5389c2a9e61491f41cf08d4af1763e.png] 配置通知方式和信息 ![配置通知方式和信息][a740020e3f743590a15f3404f0b572ae.png] **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 接口后,内存升高大于设置的阈值,然后就收到报警邮件。 ![报警邮件][073002546eb1cb670636492e889a4fe3.png] 这里图片没有显示出来,搞不懂为什么。这套监控功能还是挺强大的,就是 Prometheus 的表达式有点多。 附上几个链接: Prometheus官方文档: https://prometheus.io/docs/introduction/first\_steps/Grafana 官方文档: https://grafana.com/docs/grafana/latest/ 代码地址: https://github.com/2YSP/monitor-demo 原文链接: https://cnblogs.com/2YSP/p/12827487.html -END- 如果看到这里,说明你喜欢这篇文章,请 转发、点赞。微信搜索「web_resource」,关注后回复「进群」或者扫描下方二维码即可进入无广告交流群。 ↓扫描二维码进群↓ 推荐阅读 1. Spring Boot 分层构建 Docker 镜像实战 2. 键式搭建分布式文件服务器 3. MyBatis 动态 SQL 详解 4. Java 开发中常用的 4 种加密方法 5. 团队开发中 Git 最佳实践 喜欢文章,点个在看 [fd37a5b8d6d1227c3df4fd5107c2e501.gif]: /images/20210730/cc6551398e7e4860be8ef7b5fea3e4b8.png [3b01e6be31b4c3d94bcfdd0c9969fe13.png]: /images/20210730/c34d22289bac43ff95b89389fbf87bb3.png [enter description here]: /images/20210730/c0ec003001de44c296ec1685b4d86dd7.png [b9ed141bd8a0cd36338a68c3de8452c6.png]: /images/20210730/ca8725aa3d644b34908f41c08ae9f0e8.png [95b802d0df6131496019c7e9429a2c45.png]: /images/20210730/87902d985b1f4e10a26c37a3012e36c9.png [enter description here 1]: /images/20210730/42c5e463e2e2456eaed9ebb66ffa04ba.png [enter description here 2]: /images/20210730/a65eeb01f935474c8f445ce16207d1f3.png [enter description here 3]: /images/20210730/c4916f88082d4431bff4e252fd07f28c.png [enter description here 4]: /images/20210730/80761d2fd56f4a39b9b7153152e8a7ea.png [b6fc8eec680cdc60642e7b05914126b5.png]: /images/20210730/3028ef90e51c4b36a4c4ab6be0989275.png [enter description here 5]: /images/20210730/cea1e55f24c34619b70266cfedd0431c.png [enter description here 6]: /images/20210730/d98f4039e9d343fba9c6a7742b444eb6.png [088d0d4d2b93a67861ce3b2a7bd38dd7.png]: /images/20210730/756eb9bc3337478fbb9009e818b00103.png [09813553ff634a1c3f66c81624699c79.png]: /images/20210730/04faa8b398274654a1f73e5c2f576ff0.png [4bb44e213efe8503a2fd6588bd603fb8.png]: /images/20210730/79276d9ce21d4098a8c02f97029a5add.png [2a5389c2a9e61491f41cf08d4af1763e.png]: /images/20210730/eff3cafab8a44328bb0fb33e60a4b7a7.png [a740020e3f743590a15f3404f0b572ae.png]: /images/20210730/46538863beca41dd954eb2cdad41c37a.png [073002546eb1cb670636492e889a4fe3.png]: /images/20210730/a48af3b139634ec487b781e14248fa24.png
还没有评论,来说两句吧...