SpringBoot+Prometheus+Grafana实现应用监控和报警

本文详细介绍了如何在SpringBoot应用中集成Prometheus和Grafana进行监控,包括添加Endpoint、配置数据采集、创建dashboard以及设置邮件报警功能。通过示例展示了如何实现内存监控和警报系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

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.EnableSpri
要在Spring BootVue项目中配置应用监控,可以采取以下步骤: 1. 在Spring Boot项目中添加Spring Boot Actuator依赖,以便可以访问应用监控端点。可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 2. 配置Spring Boot Actuator端点的访问路径安全性。可以在application.properties或application.yml文件中添加以下配置: ``` # 配置Actuator端点的访问路径 management.endpoints.web.base-path=/actuator # 开启Actuator端点的安全认证 management.endpoint.health.show-details=always management.endpoints.web.exposure.include=health,info,metrics management.endpoint.info.enabled=true management.endpoint.metrics.enabled=true management.security.enabled=true management.security.roles=ACTUATOR_ADMIN ``` 3. 在Vue项目中添加Vue.js Devtools插件,以便可以实时查看Vue组件的状态性能。可以在Chrome浏览器中安装Vue.js Devtools插件。 4. 配置Vue.js Devtools插件的选项,以便可以连接到本地的Vue.js应用程序。可以在Chrome浏览器中打开Vue.js Devtools插件,然后在选项中添加以下配置: ``` { "host": "localhost", "port": 8081, "https": false } ``` 其中,host是Vue.js应用程序的主机名,port是应用程序的端口号,https表示是否使用HTTPS协议。 5. 在Vue组件中添加性能监控代码,以便可以实时查看组件的性能数据。可以在Vue组件的mounted()方法中添加以下代码: ``` mounted() { this.$nextTick(() => { if (window.performance && window.performance.mark) { window.performance.mark('vue-app-mounted') } }) } ``` 其中,window.performance.mark()方法可以在浏览器的性能分析工具中创建一个时间戳,以便可以测量组件的渲染时间。 6. 在Spring Boot项目中添加Micrometer依赖,以便可以将Vue.js应用程序的性能数据发送到监控系统。可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.1.4</version> </dependency> ``` 7. 配置Micrometer的Prometheus注册表,以便可以将Vue.js应用程序的性能数据发送到Prometheus监控系统。可以在application.properties或application.yml文件中添加以下配置: ``` # 配置Prometheus注册表 management.metrics.export.prometheus.enabled=true management.metrics.export.prometheus.endpoint=/prometheus ``` 其中,management.metrics.export.prometheus.enabled表示是否启用Prometheus注册表,management.metrics.export.prometheus.endpoint表示Prometheus注册表的访问路径。 8. 在Prometheus监控系统中添加Vue.js应用程序的监控指标,以便可以实时查看应用程序的性能数据。可以在Prometheus的配置文件中添加以下配置: ``` - job_name: 'vue-app' metrics_path: '/prometheus' static_configs: - targets: ['localhost:8081'] ``` 其中,job_name是监控任务的名称,metrics_path是Prometheus注册表的访问路径,targets是Vue.js应用程序的地址端口号。 9. 在Grafana监控系统中创建Vue.js应用程序的监控仪表板,以便可以实时查看应用程序的性能数据。可以在Grafana中添加以下数据源: ``` { "name": "Prometheus", "type": "prometheus", "url": "http://localhost:9090", "access": "proxy", "isDefault": true } ``` 其中,url是Prometheus监控系统的地址端口号。 然后,可以在Grafana中创建一个新的仪表板,然后添加Vue.js应用程序的监控指标,并配置仪表板的数据展示方式报警规则,以便可以及时发现应用程序的性能问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxj19880502

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值