如何在Spring Boot应用中内置启动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:
Add required dependencies
For Maven projects, add these to yourpom.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'Enable the Prometheus endpoint
Update yourapplication.properties(orapplication.yml) to expose the Prometheus endpoint:management.endpoints.web.exposure.include=prometheus,health management.endpoint.prometheus.enabled=trueTest the endpoint
Start your Spring Boot app, then visithttp://localhost:8080/actuator/prometheus—you'll see metrics formatted in Prometheus' native syntax.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

