Spring Boot应用与Prometheus和Grafana整合方案

 更新时间:2025年11月04日 16:55:23   作者:fanxbl957  
通过将Spring Boot应用与Prometheus和Grafana进行整合,我们可以实现对Spring Boot应用的全面监控和可视化展示,本文将详细介绍如何将Spring Boot应用与Prometheus和Grafana进行整合,实现对Spring Boot应用的全面监控,感兴趣的朋友一起看看吧

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 则将这些数据以直观的图表和仪表盘的形式展示出来,方便我们及时发现系统中的问题和进行性能调优。同时,通过自定义指标和告警配置,我们可以根据实际需求对监控系统进行进一步的扩展和优化。

到此这篇关于Spring Boot应用与Prometheus和Grafana整合方案的文章就介绍到这了,更多相关springboot prometheus+grafana整合内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring中11个最常用的扩展点总结,你知道几个

    Spring中11个最常用的扩展点总结,你知道几个

    我们知道IOC(控制反转)和AOP(面向切面编程)是spring的基石,除此之外spring的扩展能力非常强,下面这篇文章主要给大家介绍了关于Spring中11个最常用的扩展点的相关资料,需要的朋友可以参考下
    2022-12-12
  • Java调用DeepSeek API的8个高频坑与解决方法

    Java调用DeepSeek API的8个高频坑与解决方法

    现在大模型开发特别火,DeepSeek 因为中文理解好、反应快、还便宜,不少 Java 开发者都用它,本文整理了最常踩的 8 个坑,希望对大家有所帮助
    2025-12-12
  • springboot:接收date类型的参数方式

    springboot:接收date类型的参数方式

    这篇文章主要介绍了springboot:接收date类型的参数方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • jar包运行一段时间后莫名其妙挂掉线上问题及处理方案

    jar包运行一段时间后莫名其妙挂掉线上问题及处理方案

    这篇文章主要介绍了jar包运行一段时间后莫名其妙挂掉线上问题及处理方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • SpringBoot热部署启动关闭流程详解

    SpringBoot热部署启动关闭流程详解

    Spring Boot启动热部署是一种技术,它能让开发者在不重启应用程序的情况下实时更新代码。这样可以提高开发效率,避免频繁重启应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-04-04
  • IDEA连接mysql数据库报错的解决方法

    IDEA连接mysql数据库报错的解决方法

    这篇文章主要介绍了IDEA连接mysql数据库报错的解决方法,文中有非常详细的图文示例,对出现Server returns invalid timezone. Go to ‘Advanced‘ tab and set ‘serverTimezone‘ prope报错的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • SpringBoot2零基础到精通之数据与页面响应

    SpringBoot2零基础到精通之数据与页面响应

    SpringBoot是一种整合Spring技术栈的方式(或者说是框架),同时也是简化Spring的一种快速开发的脚手架
    2022-03-03
  • 完美解决java.lang.OutOfMemoryError处理错误的问题

    完美解决java.lang.OutOfMemoryError处理错误的问题

    下面小编就为大家带来一篇完美解决java.lang.OutOfMemoryError处理错误的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Java键值操作MapUtils的使用小结

    Java键值操作MapUtils的使用小结

    在Java开发中,键值操作是日常工作中不可或缺的一部分,本文主要介绍MapUtils 以org.apache.commons.collections4.MapUtils 为例的工具类实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-04-04
  • 基于Java实现动态切换ubuntu壁纸功能

    基于Java实现动态切换ubuntu壁纸功能

    这篇文章主要为大家详细介绍了如何使用 Java 在 Ubuntu Linux 系统中实现自动切换壁纸的示例程序,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11

最新评论