Master Spring Boot 3 Monitoring: Actuator, Prometheus & Grafana in Practice
This article demonstrates how to use Spring Boot 3 Actuator together with Prometheus and Grafana to monitor JVM, Tomcat, database, Redis, and remote HTTP calls, providing real‑time metrics that help detect bottlenecks, optimize resources, and ensure stable performance under high load.
1. Introduction
Stable and efficient online applications depend on real‑time monitoring of core performance metrics such as database, Redis, MQ, scheduled tasks, controller endpoints and remote calls (e.g., RestTemplate). Spring Boot Actuator can expose these metrics, helping detect bottlenecks, failures and providing data for optimization under high concurrency.
2. Practical Cases
2.1 Dependency Management
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency></code>Adding Actuator automatically configures a composite MeterRegistry . If no specific Micrometer registry is present, a SimpleMeterRegistry (in‑memory) is used.
2.2 Supported Systems
Actuator supports many monitoring systems, e.g., AppOptics, Atlas, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx, JMX, KairosDB, OpenTelemetry, Prometheus, Simple, Wavefront. This guide uses Prometheus because it is open‑source and free.
2.3 Prometheus Configuration
<code><dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency></code>After adding the dependency, Actuator exposes the endpoint /actuator/prometheus . In prometheus.yml add:
<code>scrape_configs:
- job_name: "sbapp"
metrics_path: "/ac/prometheus"
static_configs:
- targets: ["localhost:7000"]</code>Start Prometheus (default port 9090) and the Spring Boot application.
2.4 Viewing JVM Metrics
A sample controller continuously creates byte arrays to generate load. Access the endpoint and observe JVM memory usage in Prometheus.
<code>private List<byte[]> data = new ArrayList<>();
@GetMapping("")
public Object index() {
new Thread(() -> {
while (true) {
if (data.size() % 200 == 0) { data.clear(); }
data.add(new byte[1024 * new Random().nextInt(1000)]);
try { TimeUnit.MILLISECONDS.sleep(new Random().nextInt(100)); } catch (InterruptedException e) {}
}
}).start();
return "success";
}</code>2.5 Viewing Tomcat Metrics
Enable mbeanregistry in application.yml to expose Tomcat metrics, then create endpoints with artificial delays to monitor request latency.
<code>server:
tomcat:
mbeanregistry:
enabled: true</code> <code>@GetMapping("/index1")
public Object index1() throws Exception {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(2000));
return "index1";
}
@GetMapping("/index2")
public Object index2() throws Exception {
TimeUnit.MILLISECONDS.sleep(new Random().nextInt(5000));
return "index2";
}</code>2.6 Database Connection Metrics
Spring Boot instruments any DataSource; metrics are prefixed with jdbc. . Example Hikari configuration:
<code>spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/testjpa
username: root
password: xxxooo
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimumIdle: 10
maximumPoolSize: 30</code>After invoking a database‑heavy endpoint, Prometheus shows active, idle and total connections.
2.7 Remote Call Metrics (RestTemplate, WebClient, RestClient)
Inject the appropriate builder and create beans to monitor outbound HTTP calls.
<code>@Bean
RestClient restClient(RestClient.Builder builder) {
builder.baseUrl("http://localhost:8084/api");
return builder.build();
}</code>After calling the wrapper endpoint, Prometheus displays request counts, latency, and error rates.
2.8 Redis Metrics
When using Lettuce, Micrometer creates timers lettuce.command.firstresponse and lettuce.command.completion with tags command , local , and remote . A sample endpoint continuously writes keys to Redis.
<code>@GetMapping("/index")
public Object index() {
AtomicInteger ac = new AtomicInteger(0);
new Thread(() -> {
while (true) {
int v = ac.getAndIncrement();
stringRedisTemplate.opsForValue().set("s-" + v, "v-" + v);
try { TimeUnit.MILLISECONDS.sleep(300); } catch (InterruptedException e) {}
}
}).start();
return "success";
}</code>3. Grafana Visualization
Grafana is an open‑source visualization platform. After installing, run grafana server (default admin/admin). Create a Prometheus data source, then import or build dashboards to display JVM, Tomcat, database, and Redis metrics.
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.
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.