Monitoring Spring Boot Applications with Spring Actuator, Micrometer, Prometheus, and Grafana
This article demonstrates how to set up comprehensive monitoring for a Spring Boot application by integrating Spring Actuator, Micrometer, Prometheus, and Grafana, covering component introductions, sample code creation, Docker-based Prometheus and Grafana deployment, and dashboard configuration for real‑time metrics visualization.
In this tutorial we learn how to monitor a Spring Boot application using Spring Actuator, Micrometer, Prometheus, and Grafana. Although it may seem involved, the setup is straightforward.
1. Introduction
Monitoring a production application is essential for early detection of issues. By exposing internal metrics through Spring Actuator and Micrometer, and visualizing them with Prometheus and Grafana, developers can gain real‑time insight into application health.
2. Creating a Sample Application
Generate a Spring Boot project via Spring Initializr with dependencies Spring Boot Actuator , Prometheus , and Spring Web . Add a simple controller:
@RestController
public class MetricsController {
@GetMapping("/endPoint1")
public String endPoint1() {
return "Metrics for endPoint1";
}
@GetMapping("/endPoint2")
public String endPoint2() {
return "Metrics for endPoint2";
}
}Run the application:
$ mvn spring-boot:runVerify the endpoints with curl and view the Actuator endpoints (e.g., http://localhost:8080/actuator ).
3. Enabling Prometheus Export
Add the following line to application.properties to expose the Prometheus endpoint:
management.endpoints.web.exposure.include=health,info,metrics,prometheusAfter restarting, access http://localhost:8080/actuator/prometheus to see the exported metrics.
4. Adding Prometheus
Create a prometheus.yml configuration file:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'myspringmetricsplanet'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['HOST:8080']Run Prometheus in a Docker container:
$ docker run \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheusIf the container cannot reach the Spring Boot app, start it with host networking:
$ docker run \
--name prometheus \
--network host \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
-d prom/prometheus5. Adding Grafana
Run Grafana in Docker:
$ docker run --name grafana -d -p 3000:3000 grafana/grafanaAccess http://localhost:3000/ (default credentials admin/admin ), add a Prometheus data source, and import the JVM dashboard (ID 4701) or create a custom panel. Example panel query: http_server_requests_seconds_max .
6. Conclusion
We have set up basic monitoring for a Spring Boot application by combining Spring Actuator, Micrometer, Prometheus, and Grafana. This foundation can be extended with additional metrics and custom dashboards to suit more complex monitoring needs.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.