🎓博主介绍:Java、Python、js全栈开发 “多面手”,精通多种编程语言和技术,痴迷于人工智能领域。秉持着对技术的热爱与执着,持续探索创新,愿在此分享交流和学习,与大家共进步。
📖DeepSeek-行业融合之万象视界(附实战案例详解100+)
📖全栈开发环境搭建运行攻略:多语言一站式指南(环境搭建+运行+调试+发布+保姆级详解)
👉感兴趣的可以先收藏起来,希望帮助更多的人
SpringBoot监控终极方案:Actuator+Prometheus+Grafana可视化
一、引言
在当今的软件开发领域,Spring Boot 凭借其快速开发、易于部署等特性,成为了构建微服务应用的首选框架。然而,随着应用规模的不断扩大和复杂度的提升,对应用的监控和管理变得至关重要。有效的监控可以帮助我们及时发现应用中的性能瓶颈、故障隐患等问题,从而保障应用的稳定运行。本文将介绍一种 Spring Boot 监控的终极方案,即使用 Actuator、Prometheus 和 Grafana 实现应用的监控和可视化展示。
二、Spring Boot Actuator 简介
2.1 Actuator 概述
Spring Boot Actuator 是 Spring Boot 提供的一个强大的监控和管理工具,它可以帮助我们在生产环境中监控应用的健康状态、收集指标数据、审计操作等。Actuator 通过 HTTP 端点或 JMX 暴露应用的运行时信息,我们可以通过这些端点获取应用的各种指标,如内存使用情况、线程池状态、请求处理时间等。
2.2 Actuator 的使用
2.2.1 添加依赖
在 Spring Boot 项目中,我们可以通过在 pom.xml 中添加以下依赖来引入 Actuator:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.2.2 配置 Actuator
在 application.properties 或 application.yml 中进行配置,例如:
# 开启所有端点
management.endpoints.web.exposure.include=*
2.2.3 访问端点
启动应用后,我们可以通过访问 http://localhost:8080/actuator 查看所有可用的端点。例如,访问 http://localhost:8080/actuator/health 可以查看应用的健康状态。
三、Prometheus 简介
3.1 Prometheus 概述
Prometheus 是一个开源的系统监控和警报工具,它具有强大的指标收集和存储能力,以及灵活的查询语言。Prometheus 采用拉取(Pull)的方式从目标应用中收集指标数据,并将数据存储在本地的时间序列数据库中。我们可以使用 Prometheus 的查询语言 PromQL 对存储的数据进行查询和分析。
3.2 Prometheus 的安装和配置
3.2.1 下载和安装
可以从 Prometheus 官方网站(https://prometheus.io/download/)下载适合自己操作系统的版本,解压后即可使用。
3.2.2 配置文件
Prometheus 的配置文件是 prometheus.yml,我们需要在其中配置要监控的目标。以下是一个简单的配置示例:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
3.2.3 启动 Prometheus
在终端中执行以下命令启动 Prometheus:
./prometheus --config.file=prometheus.yml
3.3 Prometheus 与 Actuator 集成
Spring Boot Actuator 提供了对 Prometheus 的支持,我们只需要添加以下依赖:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
添加依赖后,Actuator 会自动将应用的指标数据以 Prometheus 格式暴露在 /actuator/prometheus 端点。
四、Grafana 简介
4.1 Grafana 概述
Grafana 是一个开源的可视化工具,它可以与多种数据源集成,如 Prometheus、InfluxDB 等,用于创建漂亮的仪表盘和可视化报表。Grafana 提供了丰富的可视化组件和模板,我们可以根据自己的需求定制各种监控面板。
4.2 Grafana 的安装和配置
4.2.1 下载和安装
可以从 Grafana 官方网站(https://grafana.com/grafana/download)下载适合自己操作系统的版本,按照官方文档进行安装。
4.2.2 配置数据源
启动 Grafana 后,访问 http://localhost:3000,使用默认的用户名和密码(admin/admin)登录。在 Grafana 中,我们需要配置 Prometheus 作为数据源。具体步骤如下:
- 点击左侧菜单中的“Configuration” -> “Data Sources”。
- 点击“Add data source”,选择“Prometheus”。
- 在“URL”字段中填写 Prometheus 的访问地址,如
http://localhost:9090。 - 点击“Save & Test”,如果配置正确,会显示测试成功的提示。
4.2.3 创建仪表盘
在 Grafana 中,我们可以创建仪表盘来展示监控数据。具体步骤如下:
- 点击左侧菜单中的“Create” -> “Dashboard”。
- 点击“Add a new panel”,选择要展示的指标和可视化类型,如折线图、柱状图等。
- 在“Metrics”选项卡中,使用 PromQL 编写查询语句,从 Prometheus 中获取数据。
- 配置面板的样式和布局,点击“Apply”保存设置。
五、综合实践
5.1 搭建 Spring Boot 项目
创建一个简单的 Spring Boot 项目,添加 Actuator 和 Prometheus 依赖,代码如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
5.2 配置 Prometheus 和 Grafana
按照前面介绍的步骤,配置 Prometheus 监控 Spring Boot 应用,并在 Grafana 中配置 Prometheus 作为数据源。
5.3 创建监控仪表盘
在 Grafana 中创建一个仪表盘,展示 Spring Boot 应用的关键指标,如 CPU 使用率、内存使用率、请求处理时间等。以下是一个简单的 PromQL 查询示例,用于查询应用的请求处理时间:
http_server_requests_seconds_sum / http_server_requests_seconds_count
六、总结
通过使用 Spring Boot Actuator、Prometheus 和 Grafana,我们可以实现对 Spring Boot 应用的全面监控和可视化展示。Actuator 提供了应用的运行时指标数据,Prometheus 负责收集和存储这些数据,Grafana 则将数据以直观的方式展示出来。这种方案不仅可以帮助我们及时发现应用中的问题,还可以为应用的性能优化提供有力的支持。

302




被折叠的 条评论
为什么被折叠?



