Master Spring Boot Actuator: Enable Monitoring, Secure Endpoints, and Build Custom Metrics

This guide explains what Spring Boot Actuator is, how to add the dependency, configure exposed endpoints, use common health and metrics endpoints, secure access with basic authentication, limit exposure, and create custom endpoints with full code examples.

Dunmao Tech Hub
Dunmao Tech Hub
Dunmao Tech Hub
Master Spring Boot Actuator: Enable Monitoring, Secure Endpoints, and Build Custom Metrics

What is Spring Boot Actuator?

Spring Boot Actuator is an independent module that adds production‑ready monitoring and management features to a Spring Boot application, exposing built‑in endpoints for health, configuration, logs, metrics, and more.

How to Enable Actuator

1. Add Dependency

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

For Gradle:

implementation 'org.springframework.boot:spring-boot-starter-actuator'

2. Configure Endpoints

In application.properties or application.yml enable the desired endpoints:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

– expose all endpoints. management.endpoint.health.show-details=always – show detailed health information.

Common Endpoints

/actuator/health

: Checks application health status. Example URL http://localhost:8080/actuator/health. /actuator/info: Shows basic application information. Example URL http://localhost:8080/actuator/info. /actuator/metrics: Exposes performance metrics. Example URL http://localhost:8080/actuator/metrics. /actuator/loggers: Allows dynamic log level changes. Example URL http://localhost:8080/actuator/loggers. /actuator/env: Displays environment properties. Example URL http://localhost:8080/actuator/env.

Example: Health Check

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP",
      "details": {
        "database": "MySQL",
        "validationQuery": "isValid()"
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 5000000000,
        "free": 3000000000
      }
    }
  }
}

Example: Metrics

Requesting http://localhost:8080/actuator/metrics returns the list of metric names:

{
  "names": [
    "jvm.memory.used",
    "jvm.memory.max",
    "http.server.requests",
    "system.cpu.usage"
  ]
}

Specific metric values can be fetched, e.g., http://localhost:8080/actuator/metrics/jvm.memory.used:

{
  "name": "jvm.memory.used",
  "measurements": [
    { "statistic": "VALUE", "value": 12345678 }
  ],
  "availableTags": [
    { "tag": "area", "values": ["heap", "nonheap"] }
  ]
}

Security Configuration

1. Basic Authentication

Add Spring Security credentials in application.properties:

spring.security.user.name=admin
spring.security.user.password=secret

Accessing any actuator endpoint now requires the configured username and password.

2. Restrict Endpoint Exposure

Limit which endpoints are publicly accessible:

management.endpoints.web.exposure.include=health,info

Only /health and /info will be exposed.

Custom Endpoint

Developers can create their own actuator endpoints to expose application‑specific data.

Code Example

package com.example.demo.actuator;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "custom")
public class CustomEndpoint {

    @ReadOperation
    public String customEndpoint() {
        return "This is a custom endpoint";
    }
}

After adding the class, the endpoint is reachable at http://localhost:8080/actuator/custom and returns the string defined above.

Conclusion

Spring Boot Actuator equips developers with ready‑made monitoring endpoints, rich metric data, security controls, and extensibility for custom endpoints, making it a powerful tool for observing, diagnosing, and securing applications in production environments.

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.

Spring BootactuatorCustom Endpoint
Dunmao Tech Hub
Written by

Dunmao Tech Hub

Sharing selected technical articles synced from CSDN. Follow us on CSDN: Dunmao.

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.