springboot+prometheus+grafana实现应用监控和报警 - windows版

本文介绍了如何使用SpringBoot、Prometheus和Grafana搭建应用监控系统。首先,详细讲解了Prometheus的安装及配置,然后展示了SpringBoot项目集成Prometheus的步骤,包括添加依赖、配置YML文件以及暴露监控指标。接着,利用PromQL进行QPS和耗时统计。最后,介绍了Grafana的安装,并演示了如何配置数据源、导入SpringBoot应用的面板,实现可视化监控。

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

Prometheus官网:https://prometheus.io/docs/introduction/first_steps/
Grafana官网:https://grafana.com/docs/grafana/latest/

SpringBoot+Prometheus+Grafana是目前比较常用的应用监控方案,由Springboot项目暴露指标,Prometheus进行信息采集,Grafana实现可视化监控与报警。

一、Prometheus安装

安装包下载地址:https://prometheus.io/download/
下载windows64位,解压可用,解压后进入目录运行premetheus.exe,访问http://localhost:9090/即可,需要查看监控的对象列表可以进入status > targets查看
在这里插入图片描述

二、Springboot使用Prometheus

1.Springboot项目添加prometheus的Maven依赖

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.6.4</version>
        </dependency>

2.Springboot项目 yml 配置文件设置

spring:
  application:
    name: cloud-reservoirs
    
management:
  endpoints:
    web:
      exposure:
        include: "*" # 指定所有的web接口都会上报
  metrics:
    tags:
      application: ${spring.application.name} # 这个应用所有上报的metrics 都会带上 application这个标签

3.编辑 Prometheus 的 prometheus.yml 文件
进入Prometheus的解压目录编辑prometheus.yml文件,添加如下配置

  - job_name: 'cloud-reservoirs'
    scrape_interval: 5s # 抓取频率
    metrics_path: '/actuator/prometheus' # 抓取的端点
    static_configs:
      - targets: ['localhost:10024'] # 目标机器

在这里插入图片描述
4.重新启动premetheus.exe
重新启动premetheus.exe,查看http://localhost:9090 status > targets
在这里插入图片描述
访问 http://localhost:10024/actuator/prometheus 可以查看各项指标
在这里插入图片描述
5.访问Graph
访问Graph,在搜索框输入:
http_server_requests_seconds_count,然后点击Execute,可以看到抓起metric的记录
在这里插入图片描述
接着访问我们项目的任一接口,再次查看Graph,可以看到新增了一条记录
在这里插入图片描述
6.qps统计

sum(rate(http_server_requests_seconds_count{application="cloud-reservoirs"}[10s]))

在这里插入图片描述
rate: 用于统计增长趋势,要求上报的Metric为Counter类型(只增不减)
irate: 与rate相似,区别在于rate统计的是一段时间内的平均增长速率,无法反应这个时间窗口内的突发情况(即瞬时高峰),irate通过区间向量中最后两个样本数据来计算增长速率,但是当选用的区间范围较大时,可能造成不小的偏差
sum: 求和,适用于统计场景
更多内置函数,可以参考: PromQL内置函数 或参考官网
https://prometheus.io/docs/prometheus/latest/querying/operators/

7.耗时统计
除了qps,另外一个经常关注的指标就是rt了,如上面接口的平均rt,通过两个Metric的组合来实现

sum(rate(http_server_requests_seconds_sum{application="cloud-reservoirs"}[10s])) / sum(rate(http_server_requests_seconds_count{application="cloud-reservoirs"}[10s]))

在这里插入图片描述
将sum聚合去掉则可以看到各接口的访问情况

三、Grafana安装与使用

1.Grafana安装
安装包下载地址:https://grafana.com/grafana/download?platform=windows
windows 64位下载grafana-enterprise-8.4.7.windows-amd64.msi,直接运行安装,安装完成后进入grafana/bin目录下,双击运行garafana-server.exe,访问地址http://localhost:3000/,默认用户名密码admin/admin在这里插入图片描述
2.Grafana使用
(1)grafana启动之后,配置数据源Promethues
点击首界面的 DATA SOURCES
在这里插入图片描述
接着选择 Prometheus
在这里插入图片描述
然后输入 Prometheus 服务器地址,点击Save & test
在这里插入图片描述
(2)给SpringBoot应用配置面板
可以直接使用现成的模板,比如 12856,这需要到官网去查找:
https://grafana.com/grafana/dashboards?dataSource=prometheus&search=spring
选择一个,点进去之后,右边的 Copy ID toClipboard 对应的数字就是我们需要的
在这里插入图片描述
在这里插入图片描述
拿到ID后,回到Grafana界面,找到如下Import项
在这里插入图片描述
输入ID,点击输入框右边的 Load
在这里插入图片描述
选择我们前边配置的数据源,点击 Import
在这里插入图片描述
配置后面板如下
在这里插入图片描述
over ~

要在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应用程序的监控指标,并配置仪表板的数据展示方式报警规则,以便可以及时发现应用程序的性能问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值