当前位置: 代码网 > it编程>编程语言>Java > Spring Boot应用与Prometheus和Grafana整合方案

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

2025年11月04日 Java 我要评论
springboot监控终极方案:整合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整合内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com