🎓博主介绍:Java、Python、js全栈开发 “多面手”,精通多种编程语言和技术,痴迷于人工智能领域。秉持着对技术的热爱与执着,持续探索创新,愿在此分享交流和学习,与大家共进步。
📖DeepSeek-行业融合之万象视界(附实战案例详解100+)
📖全栈开发环境搭建运行攻略:多语言一站式指南(环境搭建+运行+调试+发布+保姆级详解)
👉感兴趣的可以先收藏起来,希望帮助更多的人
SpringBoot监控体系构建:Prometheus+Grafana可视化实战
一、背景介绍
在当今的软件开发领域,Spring Boot 凭借其便捷性和高效性,成为了构建微服务应用的首选框架。随着应用规模的不断扩大和复杂度的增加,对应用进行有效的监控变得至关重要。Prometheus 和 Grafana 作为开源的监控和可视化工具,为 Spring Boot 应用提供了强大的监控解决方案。Prometheus 用于收集和存储指标数据,而 Grafana 则用于将这些数据以直观的图表和仪表盘的形式展示出来。
二、环境准备
2.1 安装 Java 和 Spring Boot
确保你的系统已经安装了 Java 8 或更高版本。可以通过以下命令检查 Java 版本:
java -version
使用 Spring Initializr 或 Spring Boot CLI 创建一个新的 Spring Boot 项目。
2.2 安装 Prometheus
从 Prometheus 官方网站(https://prometheus.io/download/)下载适合你操作系统的版本。解压下载的文件,并进入解压后的目录。启动 Prometheus 服务器:
./prometheus --config.file=prometheus.yml
2.3 安装 Grafana
从 Grafana 官方网站(https://grafana.com/grafana/download)下载适合你操作系统的版本。安装完成后,启动 Grafana 服务:
sudo systemctl start grafana-server
三、Spring Boot 集成 Prometheus
3.1 添加依赖
在 pom.xml 中添加以下依赖:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.10.6</version>
</dependency>
3.2 配置 Spring Boot
在 application.properties 中添加以下配置:
management.endpoints.web.exposure.include=*
management.metrics.export.prometheus.enabled=true
3.3 编写示例代码
创建一个简单的 Spring Boot 控制器,用于模拟业务逻辑:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
3.4 验证指标暴露
启动 Spring Boot 应用,访问 http://localhost:8080/actuator/prometheus,你应该能够看到 Prometheus 格式的指标数据。
四、Prometheus 配置
4.1 编辑 prometheus.yml
打开 prometheus.yml 文件,添加以下配置:
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
4.2 重启 Prometheus
修改配置文件后,重启 Prometheus 服务器,使配置生效。
4.3 验证数据收集
访问 Prometheus 控制台(http://localhost:9090),在查询框中输入 http_server_requests_seconds_count,如果能看到相关的指标数据,说明 Prometheus 已经成功收集到 Spring Boot 应用的指标。
五、Grafana 配置
5.1 登录 Grafana
打开浏览器,访问 http://localhost:3000,使用默认用户名和密码(admin/admin)登录 Grafana。
5.2 添加数据源
在 Grafana 界面中,点击左侧菜单栏的“Configuration” -> “Data Sources”,然后点击“Add data source”。选择 Prometheus 作为数据源,在“URL”字段中输入 Prometheus 的访问地址(http://localhost:9090),点击“Save & Test” 验证连接。
5.3 创建仪表盘
点击左侧菜单栏的“Create” -> “Dashboard”,然后点击“Add a new panel”。在“Metrics” 选项卡中,选择要展示的指标,如 http_server_requests_seconds_count。调整图表的样式和布局,最后点击“Apply” 保存仪表盘。
六、监控指标分析
6.1 常用指标
http_server_requests_seconds_count:记录 HTTP 请求的总数。http_server_requests_seconds_sum:记录 HTTP 请求的总耗时。jvm_memory_used_bytes:记录 JVM 内存的使用情况。
6.2 指标查询和可视化
在 Grafana 中,可以使用 PromQL(Prometheus Query Language)进行指标查询和可视化。例如,查询每个请求路径的请求总数:
sum(rate(http_server_requests_seconds_count[5m])) by (uri)
七、告警设置
7.1 配置 Alertmanager
从 Alertmanager 官方网站(https://prometheus.io/download/#alertmanager)下载适合你操作系统的版本。解压下载的文件,并编辑 alertmanager.yml 配置文件,设置告警接收方式(如邮件、Slack 等)。
7.2 编写告警规则
在 Prometheus 中创建告警规则文件,例如 rules.yml:
groups:
- name: spring-boot-alerts
rules:
- alert: HighRequestRate
expr: sum(rate(http_server_requests_seconds_count[5m])) > 100
for: 5m
labels:
severity: critical
annotations:
summary: "High request rate detected"
description: "The application is receiving more than 100 requests per second."
7.3 加载告警规则
在 prometheus.yml 中添加告警规则文件的路径:
rule_files:
- 'rules.yml'
重启 Prometheus 服务器,使告警规则生效。
八、总结
通过以上步骤,我们成功构建了一个基于 Prometheus 和 Grafana 的 Spring Boot 监控体系。Prometheus 负责收集和存储指标数据,Grafana 用于将这些数据可视化,同时我们还设置了告警机制,以便及时发现和处理应用中的问题。这种监控体系可以帮助我们更好地了解应用的运行状态,提高应用的稳定性和可靠性。

3458

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



