Topic 5: – 6 steps of Spring Boot Actuator

image 2

Play Store Application link – Spring Boot in 13 steps – App on Google Play


Github project link – https://github.com/kuldeep101990/Spring_Boot_Actuator

Step 1: Setting Up a Spring Boot Project

Using Spring Initializr

  1. Go to https://start.spring.io/.
  2. Fill in the details:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.x.x
    • Dependencies: Spring Web, Spring Boot Actuator
  3. Click Generate, and extract the downloaded ZIP file.

Import into IDE

  • Open your IDE (e.g., IntelliJ or Eclipse).
  • Import the extracted project as a Maven project.

Step 2: Introduction to Actuator

Spring Boot Actuator provides built-in endpoints for monitoring and managing your application.

Add Actuator Dependency

If you haven’t added Actuator via Spring Initializr, include it in pom.xml:

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

Enable Actuator Endpoints

Add the following in application.properties to enable all endpoints:

management.endpoints.web.exposure.include=*


Step 3: Built-in Actuator Endpoints

Actuator provides several endpoints out of the box:

  1. /actuator/health: Displays application health status.
  2. /actuator/metrics: Provides application metrics.

Example: Access Health Endpoint

Start the application and access:

http://localhost:8080/actuator/health

Response:

{
  "status": "UP"
}

Example: Access Metrics Endpoint

Access:

http://localhost:8080/actuator/metrics

Response (partial):

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


Step 4: Securing Actuator Endpoints

Add Security Dependency

Include Spring Security in pom.xml:

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

Configure Security in application.properties

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

When accessing an Actuator endpoint, you will now be prompted for credentials.


Step 5: Customizing Actuator Endpoints

You can customize the information displayed by endpoints.

Example: Customize Health Endpoint

Create a custom health indicator:

package com.example.demo;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // Custom logic to determine health
        boolean appHealthy = checkAppHealth();
        return appHealthy ? Health.up().withDetail("CustomHealth", "All good!").build()
                          : Health.down().withDetail("CustomHealth", "Something's wrong!").build();
    }
    private boolean checkAppHealth() {
        // Simulate health check logic
        return true;
    }
}

Also add below line in application.properties,
management.endpoint.health.show-details=always

Access http://localhost:8080/actuator/health to see the custom health details.


Step 6: Integrating Actuator with Monitoring Tools

Actuator supports integration with external monitoring tools like Prometheus and Grafana.

Example: Add Micrometer Dependency for Prometheus

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Configure Prometheus Endpoint

Add the following in application.properties:

management.metrics.export.prometheus.enabled=true

Start the application and access:

http://localhost:8080/actuator/prometheus

The output can be scraped by Prometheus for monitoring.


Conclusion

In this guide, we:

  1. Set up a Spring Boot Actuator project.
  2. Explored built-in endpoints like /health and /metrics.
  3. Secured endpoints using Spring Security.
  4. Customized endpoints with a custom health indicator.
  5. Integrated Actuator with external tools like Prometheus.

One comment

Leave a Reply

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