Spring Boot监控指标推送Pushgateway指南

将Spring Boot监控指标推送到Pushgateway

Pushgateway是Prometheus生态系统中的一个组件,允许短期任务和批处理作业将其指标推送到其中,然后由Prometheus服务器拉取。以下是如何将Spring Boot应用的监控指标推送到Pushgateway的步骤:

1. 添加必要依赖

首先,在pom.xml中添加Micrometer和Prometheus Pushgateway的依赖:

xml
 
复制
 
下载
 
 
运行
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_pushgateway</artifactId>
    <version>0.16.0</version> <!-- 使用最新版本 -->
</dependency>

2. 配置Pushgateway推送

application.propertiesapplication.yml中配置Pushgateway:

properties
 
复制
 
下载
# application.properties 配置示例
management.metrics.export.prometheus.pushgateway.base-url=http://localhost:9091
management.metrics.export.prometheus.pushgateway.enabled=true
management.metrics.export.prometheus.pushgateway.rate=1m
management.metrics.export.prometheus.pushgateway.shutdown-operation=PUSH
management.metrics.export.prometheus.pushgateway.job=spring-boot-app

或YAML格式:

yaml
 
复制
 
下载
# application.yml 配置示例
management:
  metrics:
    export:
      prometheus:
        pushgateway:
          base-url: http://localhost:9091
          enabled: true
          rate: 1m
          shutdown-operation: PUSH
          job: spring-boot-app
          grouping-key:
            instance: ${spring.application.name}

3. 自定义推送配置(可选)

如果需要更复杂的配置,可以创建配置类:

java
 
复制
 
下载
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.exporter.PushGateway;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class PushgatewayConfig {

    @Bean
    public PushGateway pushGateway() {
        return new PushGateway("localhost:9091");
    }

    @Bean
    public PrometheusPushGatewayManager prometheusPushGatewayManager(
            PrometheusMeterRegistry prometheusMeterRegistry,
            PushGateway pushGateway) {
        
        Map<String, String> groupingKey = new HashMap<>();
        groupingKey.put("job", "spring-boot-app");
        groupingKey.put("instance", "instance-1");
        
        return new PrometheusPushGatewayManager(
                pushGateway,
                prometheusMeterRegistry,
                groupingKey,
                60,  // 推送间隔(秒)
                PrometheusPushGatewayManager.ShutdownOperation.PUSH);
    }
}

4. 手动推送指标(可选)

如果需要手动控制推送时机:

java
 
复制
 
下载
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.exporter.PushGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class MetricsPushController {

    @Autowired
    private PrometheusMeterRegistry prometheusRegistry;
    
    @Autowired
    private PushGateway pushGateway;
    
    @PostMapping("/push-metrics")
    public String pushMetrics() throws IOException {
        pushGateway.pushAdd(prometheusRegistry.getPrometheusRegistry(), "spring-boot-app");
        return "Metrics pushed to Pushgateway";
    }
}

5. 注意事项

  1. Pushgateway不是长期存储:它只是临时存储指标,直到Prometheus拉取它们。

  2. 指标生命周期:推送到Pushgateway的指标会一直存在,直到被覆盖或手动删除。

  3. 分组键:使用分组键区分不同实例的指标。

  4. 安全性:如果Pushgateway暴露在公共网络,考虑添加基本认证或其他安全措施。

6. 测试和验证

  1. 启动Pushgateway(默认端口9091)

  2. 启动Spring Boot应用

  3. 访问Pushgateway的UI(http://localhost:9091)查看推送的指标

  4. 确保Prometheus配置了从Pushgateway拉取指标

通过以上配置,你的Spring Boot应用将定期将监控指标推送到Pushgateway,然后由Prometheus服务器拉取这些指标进行存储和可视化。

posted @ 2025-06-24 11:02  GaoYanbing  阅读(127)  评论(0)    收藏  举报