Operations 7 min read

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.

Lobster Programming
Lobster Programming
Lobster Programming
How to Build a Complete SpringBoot Monitoring System with Prometheus and Grafana

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>&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;!--prometheus--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
        &lt;artifactId&gt;micrometer-registry-prometheus&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</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&lt;byte[]&gt; 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.

SpringBoot startup in Prometheus
SpringBoot startup in Prometheus
Prometheus monitoring status
Prometheus monitoring status

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.

Add Prometheus in Grafana
Add Prometheus in Grafana
Configure Prometheus data source
Configure Prometheus data source

After saving, Grafana shows a successful connection message.

Prometheus data source test result
Prometheus data source test result

6.1 Choose a Dashboard

Visit Grafana Dashboards to select a suitable SpringBoot monitoring dashboard and note its ID.

Dashboard selection
Dashboard selection

6.2 Import Dashboard into Grafana

Enter the dashboard ID in Grafana’s import dialog, select the Prometheus data source, and import.

Import dashboard
Import dashboard
Select Prometheus source
Select Prometheus source
Grafana monitoring panel
Grafana monitoring panel

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.

Running test2
Running test2
Grafana panel during execution
Grafana panel during execution
Grafana after time
Grafana after time

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.

monitoringmicroservicesPrometheusSpringBootGrafanaActuator
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.