配置Prometheus采集SpringBoot数据并在Grafana实时监控

本文详细介绍了如何在SpringBoot应用中集成Prometheus进行数据采集,以及如何利用Grafana展示和配置监控指标,包括QPS、接口耗时等关键性能指标的监控。同时,提供了Prometheus配置文件示例和Grafana仪表板的设置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 简介

Prometheus作为可靠的数据采集工具,常用于后台系统的流量、CPU利用率、时延等指标数据采集。
Grafana作为指标展示工具,常用于后台系统的流量、CPU利用率、时延等指标数据展示,并可针对不同的指标配置告警。
本文以SpringBoot后台系统为例,讲解Prometheus采集后台接口相关数据、Grafana配置并展示相关数据。

2 配置

2.1 SpringBoot配置

在SpringBoot中引入actuator组件监控系统信息并对外暴露信息URI,
供调用方查看SpringBoot的相关运行信息,
引入Prometheus组件,生成统计信息,
通过Actuator暴露出Prometheus统计信息的URI,
供Prometheus读取SpringBoot中premetheus组件采集的数据。
配置如下:

  • pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>io.micrometer</groupId>
	<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
  • application.yml
    作为测试环境,将exposure设置为*,即暴露所有URI,
    当然,开发者可以配为其他。
    base-path设置为(选配):/api/v1,
    因为SpringBoot系统中使用了接口拦截,开放的前缀为/api/v1,
    所以,需要这样配置,如果没有配置接口拦截的开发者,可以不设置。
management:
  endpoints:
    web:
      exposure:
        include: '*'
      base-path: /api/v1
  • 添加Bean
    在启动主函数中添加Bean,
    让prometheus组件采集SpringBoot运行时数据,
    配置如下:
package com.monkey.tutorial;

import io.micrometer.core.instrument.MeterRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

@SpringBootApplication
@EnableCaching
@EnableFeignClients
@ServletComponentScan
public class TutorialApplication {

	private static final Logger logger = LoggerFactory.getLogger(TutorialApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(TutorialApplication.class, args);
		logger.info("Tutorial 成功启动");
	}

	@Bean
	MeterRegistryCustomizer<MeterRegistry> prometheusConfiguration() {
		return (registry) -> registry.config().commonTags("application", "tutorial");
	}
}

2.2 Prometheus配置

Prometheus数据采集,配置数据采集间隔、SpringBoot服务IP:PORT、采集信息的URI,
配置如下:

global:
  scrape_interval: 10s # 每10秒采集一次数据
  evaluation_interval: 10s # 每10秒检测一次告警

scrape_configs:
  - job_name: current_system
    static_configs:
      - targets: ['192.168.211.129:9100']
        labels:
          instance: system
  - job_name: app_tutorial # 任务名称
    metrics_path: '/api/v1/prometheus' # 后台通过actuator暴露的URI
    static_configs:
      - targets: ['192.168.211.1:9121'] # 后台IP和PORT
        labels:
          instance: springboot # 实例名称
  • 启动
    这里不配置采集数据持久化,只挂载配置文件。
sudo docker run  -d -p 9090:9090  -v /home/xindaqi/software/install/prometheus/config.yml:/etc/prometheus/prometheus.yml  prom/prometheus

3 监控

3.1 SpringBoot监控数据

启动后台该服务,生成的数据接口:
http://localhost:9121/api/v1/prometheus

数据统计标识:

序号标识描述
1http_server_requests_seconds_count请求量
2http_server_requests_seconds_sum请求总耗时
3http_server_requests_seconds_max请求最大耗时

在这里插入图片描述

3.2 Grafana监控仪表板

Grafana连接Prometheus参考:
合集:Ubuntu20部署Prometheus和Grafana
配置Grafana最终的效果如下图所示。
在这里插入图片描述

4 Grafana监控配置

在Grafana中配置监控指标。

4.1 QPS配置

表达式:

rate(http_server_requests_seconds_count{uri="/api/v1/prometheus/test"}[5m])
  • 配置监控属性
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 数据显示
    在这里插入图片描述
  • 配置图例

在这里插入图片描述

4.2 接口耗时配置

计算公式:
接 口 耗 时 = 单 位 时 间 内 总 耗 时 单 位 时 间 内 请 求 数 量 接口耗时=\frac{{单位时间内总耗时}}{{单位时间内请求数量}} =
Grafana表达式:

rate(http_server_requests_seconds_sum{uri="/api/v1/prometheus/test"}[1m])/rate(http_server_requests_seconds_count{uri="/api/v1/prometheus/test"}[1m])

其余和QPS配置方式一致,单位选择秒,参考配置如下图:
在这里插入图片描述

4.3 JVM垃圾回收配置

Metrics browser中选择jvm_gc_pause_seconds_count,其余和QPS配置方式一致。
Grafana表达式:

jvm_gc_pause_seconds_count{action=~"end of major GC|end of minor GC",job="app_tutorial"}

5 小结

要点:

  • SpringBoot添加actuator和prometheus依赖,actuator暴露监听数据URI,prometheus采集并统计数据;
  • SpringBoot中配置prometheus数据采集Bean;
  • Prometheus启动文件中添加监控的后台服务信息,如数据采集间隔、服务IP:PROT等;
  • Grafana配置:Metrics browser配置监控的指标:包括uri、outcome等等;
  • Transform配置数据转换:Labels to fields配置图例名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天然玩家

坚持才能做到极致

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

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

打赏作者

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

抵扣说明:

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

余额充值