How to Build a Complete SpringBoot Monitoring System with Prometheus and Grafana
This guide walks you through integrating SpringBoot with Prometheus and Grafana, covering dependency setup, YAML configuration, a test controller, Prometheus scrape jobs, and Grafana dashboard creation to achieve real‑time application monitoring and performance analysis.
Why Monitoring Is Essential
For any application system, monitoring is crucial because failures such as network interruptions, memory leaks, disk space exhaustion, or software bugs can disrupt the entire system and cause financial loss.
Integrating SpringBoot with Prometheus
SpringBoot enables rapid creation of standalone, production‑grade Spring applications, while Prometheus provides open‑source monitoring and alerting. Combining them allows real‑time metrics collection and performance analysis for microservice architectures.
1. Add Required Dependencies
<code><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--prometheus-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies></code>2. Configure application.yml
<code>server:
port: 8081
spring:
application:
name: prometheus-demo
#prometheus
management:
endpoints:
web:
exposure:
include: '*'</code>3. Add a Test Controller
<code>@RestController
@RequestMapping("/test")
public class OrderController {
@GetMapping("/test1")
public String test1(){
return "success";
}
@GetMapping("/test2")
public String test2(){
List<byte[]> byteList = new ArrayList<>();
while (true){
byteList.add(new byte[1024*1024*2]);
try {
Thread.sleep(2000);
} catch (InterruptedException e){
// handle exception
}
}
}
}</code>4. Verify SpringBoot Project in Prometheus
After starting the SpringBoot application, open Prometheus and query the /actuator/prometheus endpoint to ensure metrics are being scraped. The screenshots show a green status indicating successful monitoring.
5. Add SpringBoot Scrape Job to Prometheus Config
<code>scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["Prometheus_ip:9090"]
- job_name: 'springboot-prometheus'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["springboot项目的ip:8081"]</code>6. Configure Grafana Data Source
In Grafana, add a new Prometheus data source, test the connection, and save it. The interface screenshots illustrate the steps.
After saving, Grafana shows a successful connection message.
6.1 Choose a Dashboard
Visit Grafana Dashboards to select a suitable SpringBoot monitoring dashboard and note its ID.
6.2 Import Dashboard into Grafana
Enter the dashboard ID in Grafana’s import dialog, select the Prometheus data source, and import.
7. Test the Monitoring Setup
Trigger the /test2 endpoint, which runs an infinite loop allocating memory. Grafana visualizes the increasing CPU and memory usage, allowing rapid detection of performance issues.
Summary
SpringBoot integrates with Prometheus by exposing metrics at /actuator/prometheus . Metrics can be pushed via Pushgateway for short‑lived jobs or scraped directly for long‑running services. This setup enables real‑time visibility into application health and performance.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.