You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

升级Spring Boot 2.0后,如何通过PushGateway向Prometheus导出指标?

解决Spring Boot 2.0+通过PushGateway向Prometheus导出指标的问题

Spring Boot 2.0之后确实不再需要micrometer-spring-legacy依赖了,取而代之的是直接引入官方适配的starter和PushGateway相关组件,再配合正确的配置就能实现指标推送。下面是具体的操作步骤:

1. 添加正确的依赖

先把项目里的micrometer-spring-legacy依赖移除,然后根据你的构建工具添加以下依赖:

Maven 示例(pom.xml)

<!-- Micrometer核心依赖 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
</dependency>
<!-- Prometheus注册表依赖 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!-- PushGateway客户端依赖 -->
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
</dependency>

Gradle 示例(build.gradle)

implementation 'io.micrometer:micrometer-core'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'io.prometheus:simpleclient_pushgateway'

2. 配置PushGateway参数

application.ymlapplication.properties中添加推送相关配置,替换成你的实际环境信息:

application.yml 示例

management:
  metrics:
    export:
      prometheus:
        pushgateway:
          enabled: true
          base-url: http://your-pushgateway-ip:9091 # 你的PushGateway地址
          job: your-app-name # 标识应用的Job名称,Prometheus通过这个区分不同应用
          push-rate: 15s # 指标推送间隔,比如15秒一次

application.properties 示例

management.metrics.export.prometheus.pushgateway.enabled=true
management.metrics.export.prometheus.pushgateway.base-url=http://your-pushgateway-ip:9091
management.metrics.export.prometheus.pushgateway.job=your-app-name
management.metrics.export.prometheus.pushgateway.push-rate=15s

3. 自定义推送逻辑(可选)

如果需要更灵活的控制(比如手动触发推送、自定义异常处理),可以创建配置类来实现:

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.pushgateway.PushGateway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
public class PushGatewayConfig {

    @Value("${management.metrics.export.prometheus.pushgateway.base-url}")
    private String gatewayUrl;

    @Value("${management.metrics.export.prometheus.pushgateway.job}")
    private String appJobName;

    @Bean
    public PushGateway pushGateway() {
        return new PushGateway(gatewayUrl);
    }

    // 定时推送示例,这里用@Scheduled替代配置里的push-rate
    @Scheduled(fixedRateString = "${management.metrics.export.prometheus.pushgateway.push-rate}")
    public void pushMetrics(PrometheusMeterRegistry registry, PushGateway pushGateway) {
        try {
            // pushAdd会累加指标,push会覆盖,按需选择
            pushGateway.pushAdd(registry.getPrometheusRegistry(), appJobName);
        } catch (Exception e) {
            // 这里可以添加日志记录或告警逻辑
            System.err.println("推送指标到PushGateway失败:" + e.getMessage());
        }
    }
}

4. 验证推送结果

启动应用后,访问PushGateway的Web界面(比如http://your-pushgateway-ip:9091),就能看到你配置的job对应的指标数据了。同时记得检查Prometheus是否已经配置了从PushGateway拉取指标的规则。

需要注意的是,Spring Boot 2.0+的Micrometer配置逻辑和1.5.x差异较大,核心是抛弃了legacy包,直接使用官方的注册表和客户端组件,配置前缀也统一为management.metrics.export.prometheus.pushgateway.*,如果配置不生效,可以先检查依赖是否正确引入,配置项有没有写错。

内容的提问来源于stack exchange,提问作者Aura

火山引擎 最新活动