You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot应用中内置启动Prometheus实现指标聚合?

解决Spring Boot+Micrometer无需单独Prometheus服务的方案

Hey there! I totally get your frustration—having to fire up three separate services just to get a look at your metrics feels like a lot of extra work, especially when you're used to Dropwizard's more streamlined approach where you could just expose aggregated metrics directly from your app.

Let's break down the solutions based on whether you're running a single instance or multiple instances of your Spring Boot app:

单实例场景:直接对接Grafana无需Prometheus

If you only need to monitor a single instance, you can skip the standalone Prometheus server entirely and connect Grafana directly to your Spring Boot app's metrics endpoint. Here's how:

  1. Add required dependencies
    For Maven projects, add these to your 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>
    

    For Gradle projects, add these to your build.gradle:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'io.micrometer:micrometer-registry-prometheus'
    
  2. Enable the Prometheus endpoint
    Update your application.properties (or application.yml) to expose the Prometheus endpoint:

    management.endpoints.web.exposure.include=prometheus,health
    management.endpoint.prometheus.enabled=true
    
  3. Test the endpoint
    Start your Spring Boot app, then visit http://localhost:8080/actuator/prometheus—you'll see metrics formatted in Prometheus' native syntax.

  4. Connect Grafana
    In Grafana, add a new Prometheus data source and set the URL to your app's endpoint (e.g., http://your-app-url:8080/actuator/prometheus). You can now build dashboards using PromQL queries just like you would with a standalone Prometheus server.

This setup only requires your Spring Boot app and Grafana—no extra Prometheus service needed, matching the simplicity of your old Dropwizard workflow.


多实例场景:轻量级聚合替代方案

If you need to aggregate metrics across multiple instances, fully embedding Prometheus into your Spring Boot app isn't practical (Prometheus' storage and scraping logic is designed to run as a separate service). But there are lighter alternatives:

  • Option 1: Use Prometheus PushGateway
    Deploy a lightweight PushGateway service, then configure all your Spring Boot instances to push metrics to it. You can either run a minimal Prometheus instance to scrape the PushGateway (for persistence) or connect Grafana directly to the PushGateway (note: Grafana's support for PushGateway is limited, but basic PromQL queries work).

  • Option 2: Custom in-app aggregation (simple use cases)
    For basic aggregation needs, you can build a small custom service in your Spring Boot app that periodically scrapes metrics from other instances, aggregates them (e.g., averages, sums), and exposes the aggregated results via Micrometer. Keep in mind this approach lacks persistence—metrics will reset if the aggregating instance restarts.


Bonus: Bring back Dropwizard-style metrics

If you miss the structured metrics format from Dropwizard, you can add the micrometer-registry-dropwizard dependency and enable the /actuator/metrics endpoint. This will expose metrics in a format similar to Dropwizard's, though Prometheus format is still the most compatible with Grafana.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:17:58