Spring Boot Actuator with example

Spring Boot Actuator with example

Spring Boot Actuator is a module in Spring Boot that provides production-ready features to help you monitor and manage your application. It includes a set of built-in endpoints that can be used to inspect the state of your application, manage its health, and gather metrics. Actuator is useful for application management and monitoring, especially in production environments.

Key features of Spring Boot Actuator include:

1. Health Checks: Provides endpoints to check the health status of your application, including database connectivity, disk space, and more.

2. Metrics: Collects and exposes metrics about your application, such as the number of requests, memory usage, and garbage collection.

3. Environment Information: Exposes information about the application’s environment, including configuration properties and system properties.

4. Application Insights: Provides insights into application behavior and performance, which is useful for debugging and monitoring.

Spring Boot Actuator with example

Example

1. Adding Spring Boot Actuator Dependency

To use Spring Boot Actuator, you need to add the spring-boot-starter-actuator dependency to your project.

Adding Spring Boot Actuator Dependency
For Maven:
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```

For Gradle:
```groovy
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
```

2.Configuring Actuator Endpoints

You can configure which actuator endpoints are enabled and how they are exposed in your application.properties or application.yml file

Configuring Actuator Endpoints

.

Example `application.properties`:
```properties
management.endpoints.web.exposure.include=health,info
management.endpoint.health.show-details=always
```

- `management.endpoints.web.exposure.include`: Specifies which actuator endpoints are exposed over HTTP. In this case, only the `health` and `info` endpoints are exposed.
- `management.endpoint.health.show-details`: Configures whether detailed health information should be displayed.

3. Using Actuator Endpoints

    Once you have the dependency and configuration in place, you can access various actuator endpoints. Here are a few common ones:

    • Health Endpoint:
    • URL: http://localhost:8080/actuator/health
    • Description: Provides information about the application’s health status.

    Using Actuator Endpoints
     Example Response:
      ```json
      {
          "status": "UP",
          "components": {
              "diskSpace": {
                  "status": "UP",
                  "details": {
                      "total": 50000000000,
                      "free": 20000000000,
                      "threshold": 10485760
                  }
              }
          }
      }
      ```
    
    - Info Endpoint:
      - URL: `http://localhost:8080/actuator/info`
      - Description: Provides general information about the application, such as build version, description, and more.
    
      Example Response:
      ```json
      {
          "app": {
              "name": "Demo Application",
              "version": "1.0.0"
          }
      }
      ```
    
    

    4. Customizing Actuator Endpoints

      You can create custom actuator endpoints by implementing the Endpoint interface and registering it as a Spring Bean.

      Customizing Actuator Endpoints
      
      
      Example:
      ```java
      package com.example.demo;
      
      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 custom() {
              return "This is a custom actuator endpoint";
          }
      }
      ```
      
      Usage:
      - URL: `http://localhost:8080/actuator/custom`
      - Response:
        ```text
        This is a custom actuator endpoint
        ```
      

      Conclusion

      • Spring Boot Actuator: A module that provides production-ready features for monitoring and managing your Spring Boot application.
      • Features: Includes health checks, metrics, environment information, and custom endpoints.
      • Usage:
      • Add Dependency: Include spring-boot-starter-actuator.
      • Configure Endpoints: Use application.properties or application.yml for configuration.
      • Access Endpoints: Use HTTP requests to access built-in and custom actuator endpoints.

      Homepage

      Readmore