Skip to content
Spring Boot

Spring

Spring Boot

Spring

Spring Boot Actuator

admin, January 31, 2025January 31, 2025

Spring Boot Actuator is a powerful feature that helps monitor and manage Spring Boot applications in production. It provides built-in endpoints to check the application’s health, metrics, environment properties, and more.


1. Adding Actuator to a Spring Boot Application

To enable Actuator, add the following dependency to your pom.xml (for Maven):

xml

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

For Gradle:

groovy

implementation ‘org.springframework.boot:spring-boot-starter-actuator’


2. Configuring Actuator Endpoints

By default, only the /actuator/health and /actuator/info endpoints are enabled. You can expose more endpoints in the application.properties or application.yml file.

Enable All Endpoints

properties

management.endpoints.web.exposure.include=*

Enable Specific Endpoints

properties

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

Customize Health Endpoint Details

properties

management.endpoint.health.show-details=always


3. Key Actuator Endpoints

EndpointDescription
/actuatorLists all available endpoints
/actuator/healthShows the application’s health status
/actuator/infoDisplays custom application information
/actuator/metricsProvides various performance metrics
/actuator/envShows environment properties
/actuator/loggersAllows dynamic logger level changes
/actuator/mappingsLists all request mappings
/actuator/threaddumpShows thread dump for debugging
/actuator/prometheusExposes metrics in Prometheus format

4. Customizing Actuator

Custom Health Indicator

You can define a custom health indicator by implementing the HealthIndicator interface.

java

import org.springframework.boot.actuate.health.Health;

import org.springframework.boot.actuate.health.HealthIndicator;

import org.springframework.stereotype.Component;

@Component

public class MyHealthIndicator implements HealthIndicator {

    @Override

    public Health health() {

        boolean isServiceUp = checkServiceHealth();

        if (isServiceUp) {

            return Health.up().withDetail(“service”, “Running Smoothly”).build();

        }

        return Health.down().withDetail(“service”, “Service Down!”).build();

    }

    private boolean checkServiceHealth() {

        return true; // Replace with real health check logic

    }

}


5. Securing Actuator Endpoints

By default, Actuator endpoints are accessible without authentication. You can secure them using Spring Security.

Basic Authentication for Actuator Endpoints

Add the following Spring Security dependency:

xml

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-security</artifactId>

</dependency>

Then, configure security in application.properties:

properties

management.endpoints.web.exposure.include=*

management.endpoint.health.show-details=always

spring.security.user.name=admin

spring.security.user.password=admin123

This will require authentication when accessing endpoints.


6. Monitoring with Prometheus and Grafana

To integrate Actuator with Prometheus, add:

properties

management.metrics.export.prometheus.enabled=true

management.endpoints.web.exposure.include=prometheus

Prometheus will scrape data from /actuator/prometheus, and you can visualize it in Grafana.

Spring Boot

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2025 Spring Boot | WordPress Theme by SuperbThemes