Operations 4 min read

How to Integrate Prometheus and Grafana with Spring Boot for Real‑Time Monitoring

Learn step‑by‑step how to set up Prometheus and Grafana with a Spring Boot 2.4.12 application, configure dependencies, expose metrics via Actuator, customize meters, and monitor database connection pools, providing a complete observability solution for Java backend services.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Integrate Prometheus and Grafana with Spring Boot for Real‑Time Monitoring

Environment: springboot2.4.12 + prometheus1.6.7 + grafana7.5.7

What is Prometheus

Prometheus is an open‑source service monitoring system and time‑series database.

Prometheus stores time‑series data, i.e., collections of data points identified by the same metric name and set of key/value labels over time.

A time series is defined by a metric name and a set of key/value labels; series with identical name and labels belong to the same time series.

Dependency Configuration

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
  </dependency>
</dependencies>
spring:
  application:
    name: app-prometheus
---
management:
  server:
    port: 9999
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: '*'

Register MeterRegistry bean:

@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String name) {
    return registry -> registry.config().commonTags("application", name);
}

Access Prometheus actuator endpoint (e.g., http://localhost:9999/actuator/prometheus).

Prometheus Installation and Configuration

Download Prometheus from the official site.

Configure scrape_configs:

scrape_configs:
  - job_name: 'app-prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:9999']

Start Prometheus and open its UI to view monitored metrics.

Custom Meter Example

@Resource
private MeterRegistry registry;
private Counter counter;

@PostConstruct
public void init() {
    counter = this.registry.counter("visitor");
}

@GetMapping("/count")
public String count() {
    this.counter.increment();
    return "访问次数:" + this.counter.count();
}

Invoke the /count endpoint several times and observe the incremented metric in Prometheus.

Grafana Installation and Configuration

Download Grafana, start the service (default login admin/admin).

Add Prometheus as a data source and explore the collected metrics, including the custom visitor counter.

Monitoring Database Connection Pool

When using HikariCP in the Spring Boot project, Grafana automatically displays database connection pool statistics after adding the Prometheus data source.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ObservabilityMetricsPrometheusspring-bootGrafana
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

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.