How to Visualize SpringBoot Metrics with Grafana and Prometheus Using Docker
This guide walks through installing Grafana and Prometheus with Docker, configuring node_exporter to collect system metrics, adding SpringBoot Actuator and Micrometer for application metrics, setting up Prometheus scrape jobs, and importing ready‑made Grafana dashboards to achieve real‑time monitoring and alerting.
Grafana Overview
Grafana is an open‑source data‑visualization and analysis tool that can display metrics from any source and includes alerting when thresholds are exceeded.
Prometheus Overview
Prometheus is a time‑series database (a MySQL‑like database with timestamps) that stores metrics; Grafana visualizes data but does not store it, so they are used together.
Installation
Using Docker is the simplest way to install Grafana and Prometheus.
Pull the Grafana Docker image:
<code>docker pull grafana/grafana</code>Run Grafana:
<code>docker run -p 3000:3000 --name grafana -d grafana/grafana</code>Pull the Prometheus Docker image:
<code>docker pull prom/prometheus</code>Create
prometheus.ymlin
/mydata/prometheus/with basic configuration, e.g.:
<code>global:
scrape_interval: 5s
</code>Run Prometheus with the configuration file mounted:
<code>docker run -p 9090:9090 --name prometheus -v /mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus</code>Access Grafana at
http://<your-host>:3000(default login admin:admin) and Prometheus at
http://<your-host>:9090.
Monitoring System Metrics
Install
node_exporteron the Linux host to expose system metrics, then let Prometheus scrape them.
Download the
node_exporterpackage from Prometheus download page and extract it.
Run the exporter:
<code>cd node_exporter
./node_exporter >log.file 2>&1 &</code>Verify metrics with
curl http://localhost:9100/metrics.
Add a scrape job to
prometheus.yml:
<code>scrape_configs:
- job_name: node
static_configs:
- targets: ['192.168.5.78:9100']
</code>Restart the Prometheus container and import a dashboard (e.g., “Node Exporter Full”, ID 1860) from the Grafana marketplace.
Monitoring a SpringBoot Application
Add
spring-boot-starter-actuatorand
micrometer-registry-prometheusdependencies to the project’s
pom.xmland expose the
/actuator/prometheusendpoint.
<code><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- integrate micrometer for Prometheus -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
</code>Configure
application.ymlto expose the endpoint:
<code>management:
endpoints:
web:
exposure:
include: 'prometheus'
metrics:
tags:
application: ${spring.application.name}
</code>Run the SpringBoot container, then add a scrape job for its metrics (replace the IP with the container’s address):
<code>scrape_configs:
- job_name: 'mall-tiny-grafana'
scrape_interval: 5s
scrape_timeout: 10s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['172.17.0.5:8088']
</code>After reloading Prometheus and importing a suitable dashboard (e.g., ID 14370), Grafana will display real‑time SpringBoot metrics.
Summary
To visualize metrics with Grafana, first expose metrics from the target system (using Actuator, Micrometer, node_exporter, etc.), let Prometheus scrape and store them, and finally configure Grafana to use Prometheus as a data source.
References
Grafana documentation: https://grafana.com/docs/grafana/latest/getting-started/getting-started-prometheus/
Node exporter guide: https://prometheus.io/docs/guides/node-exporter/
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.