SpringBoot监控终极方案:整合Prometheus+Grafana

🎓博主介绍:Java、Python、js全栈开发 “多面手”,精通多种编程语言和技术,痴迷于人工智能领域。秉持着对技术的热爱与执着,持续探索创新,愿在此分享交流和学习,与大家共进步。
📖DeepSeek-行业融合之万象视界(附实战案例详解100+)
📖全栈开发环境搭建运行攻略:多语言一站式指南(环境搭建+运行+调试+发布+保姆级详解)
👉感兴趣的可以先收藏起来,希望帮助更多的人
在这里插入图片描述

SpringBoot监控终极方案:整合Prometheus+Grafana

一、引言

在当今的软件开发领域,Spring Boot 以其快速开发、简化配置等优势,成为了构建微服务架构的热门选择。然而,随着应用的不断上线和规模的逐渐扩大,对应用进行有效的监控变得至关重要。监控不仅可以帮助我们及时发现系统中的问题,还能为系统的优化和性能调优提供有力的数据支持。

Prometheus 是一款开源的监控系统和时间序列数据库,它具有强大的查询语言和灵活的告警机制。Grafana 则是一个开源的可视化工具,能够将 Prometheus 收集到的数据以直观的图表和仪表盘的形式展示出来。本文将详细介绍如何将 Spring Boot 应用与 Prometheus 和 Grafana 进行整合,实现对 Spring Boot 应用的全面监控。

二、准备工作

2.1 环境要求

  • JDK 1.8 及以上
  • Maven 3.x 及以上
  • Spring Boot 2.x 及以上
  • Docker(可选,用于快速部署 Prometheus 和 Grafana)

2.2 创建 Spring Boot 项目

我们可以使用 Spring Initializr(https://start.spring.io/) 来快速创建一个 Spring Boot 项目,添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
</dependencies>

spring-boot-starter-web 用于创建一个简单的 Web 应用,micrometer-registry-prometheus 用于将 Spring Boot 应用的指标暴露给 Prometheus。

三、Spring Boot 应用配置

3.1 配置 Micrometer

application.propertiesapplication.yml 中添加以下配置:

management.endpoints.web.exposure.include=*
management.metrics.tags.application=my-spring-boot-app

或者使用 YAML 格式:

management:
  endpoints:
    web:
      exposure:
        include: '*'
  metrics:
    tags:
      application: my-spring-boot-app
  • management.endpoints.web.exposure.include=*:将所有的管理端点暴露出来,包括 Prometheus 指标端点。
  • management.metrics.tags.application=my-spring-boot-app:为所有的指标添加一个 application 标签,方便在 Prometheus 中进行筛选和查询。

3.2 创建简单的 Web 服务

创建一个简单的控制器类:

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.3 启动 Spring Boot 应用

启动 Spring Boot 应用后,访问 http://localhost:8080/actuator/prometheus,可以看到 Prometheus 格式的指标数据。

四、Prometheus 配置与部署

4.1 下载和启动 Prometheus

可以从 Prometheus 官方网站(https://prometheus.io/download/) 下载适合自己操作系统的版本。下载完成后,解压文件,进入解压后的目录,编辑 prometheus.yml 配置文件:

global:
  scrape_interval: 15s
  evaluation_interval: 15s

scrape_configs:
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']
  • scrape_intervalevaluation_interval:分别表示数据采集间隔和规则评估间隔,这里设置为 15 秒。
  • scrape_configs:定义了要采集的目标,job_name 为任务名称,metrics_path 为指标数据的访问路径,targets 为要采集的目标地址。

启动 Prometheus:

./prometheus --config.file=prometheus.yml

4.2 使用 Docker 部署 Prometheus

如果使用 Docker 部署 Prometheus,可以使用以下命令:

docker run -d -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

其中 /path/to/prometheus.yml 为本地 prometheus.yml 文件的路径。

4.3 验证 Prometheus 配置

访问 http://localhost:9090,可以看到 Prometheus 的 Web 界面。在搜索框中输入 up,点击 Execute 按钮,如果看到 spring-boot-app 对应的指标值为 1,表示 Prometheus 已经成功采集到 Spring Boot 应用的指标数据。

五、Grafana 配置与部署

5.1 下载和启动 Grafana

可以从 Grafana 官方网站(https://grafana.com/grafana/download) 下载适合自己操作系统的版本。下载完成后,启动 Grafana 服务:

  • 在 Linux 系统上:
sudo systemctl start grafana-server
  • 在 Windows 系统上,直接运行 grafana-server.exe

5.2 使用 Docker 部署 Grafana

使用 Docker 部署 Grafana 更加方便:

docker run -d -p 3000:3000 grafana/grafana

5.3 配置 Grafana 数据源

访问 http://localhost:3000,使用默认用户名 admin 和密码 admin 登录 Grafana。登录后,点击左侧菜单栏的 Configuration -> Data Sources,点击 Add data source,选择 Prometheus,在 URL 字段中输入 http://localhost:9090,然后点击 Save & Test,如果提示 Data source is working,表示数据源配置成功。

5.4 创建 Grafana 仪表盘

点击左侧菜单栏的 + -> Dashboard,点击 Add a new panel,在 Query 标签页中选择之前配置的 Prometheus 数据源,输入查询语句,例如 http_server_requests_seconds_count,然后点击 Apply,可以看到相应的指标数据以图表的形式展示出来。可以根据需要对图表进行进一步的配置和调整,如设置图表类型、时间范围等。

六、高级配置与优化

6.1 自定义指标

在 Spring Boot 应用中,可以使用 Micrometer 自定义指标。例如,创建一个自定义的计数器:

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class CustomMetrics {

    private final MeterRegistry meterRegistry;
    private Counter customCounter;

    public CustomMetrics(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }

    @PostConstruct
    public void init() {
        customCounter = meterRegistry.counter("custom_counter");
    }

    public void incrementCustomCounter() {
        customCounter.increment();
    }
}

在控制器中使用自定义指标:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomMetricsController {

    @Autowired
    private CustomMetrics customMetrics;

    @GetMapping("/increment")
    public String increment() {
        customMetrics.incrementCustomCounter();
        return "Custom counter incremented!";
    }
}

6.2 告警配置

在 Prometheus 中可以配置告警规则。编辑 prometheus.yml 文件,添加以下告警规则:

rule_files:
  - 'alert.rules.yml'

创建 alert.rules.yml 文件:

groups:
  - name: spring-boot-alerts
    rules:
      - alert: HighRequestRate
        expr: rate(http_server_requests_seconds_count[5m]) > 100
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "High request rate detected"
          description: "The request rate of the Spring Boot application has exceeded 100 requests per second for the last 5 minutes."

在 Grafana 中配置告警通知渠道,如邮件、Slack 等。点击左侧菜单栏的 Configuration -> Alerting,选择相应的通知渠道进行配置。

七、总结

通过将 Spring Boot 应用与 Prometheus 和 Grafana 进行整合,我们可以实现对 Spring Boot 应用的全面监控和可视化展示。Prometheus 负责收集和存储应用的指标数据,Grafana 则将这些数据以直观的图表和仪表盘的形式展示出来,方便我们及时发现系统中的问题和进行性能调优。同时,通过自定义指标和告警配置,我们可以根据实际需求对监控系统进行进一步的扩展和优化。

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fanxbl957

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

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

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

打赏作者

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

抵扣说明:

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

余额充值