Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot Actuator

Spring Boot Actuator is a sub-project of Spring Boot, which adds production-ready features to your application without needing to write these components yourself. It's incredibly beneficial for monitoring and managing production applications, exposing various features via HTTP endpoints and JMX.

Features:

  1. Health Checks: Shows the health of the application. It can be easily extended to check the health of data sources, caches, and other external systems.

  2. Metrics Collection: Collects and exposes a large range of application metrics like JVM, garbage collection, web requests, and custom metrics.

  3. Application Environment: Displays properties and configurations of the application.

  4. Auditing: Tracks user activity and authentication events.

  5. HTTP Traces: Keeps a log of the latest HTTP requests.

  6. Beans Information: Displays all beans and their relationships in the application context.

  7. Shutdown: Allows the application to be gracefully shut down (should be used carefully).

  8. And more...

How to Include Actuator:

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

For Maven:

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

For Gradle:

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

Accessing Actuator Endpoints:

Once the dependency is added, various actuator endpoints become available at the default /actuator path. For example:

  • Health check: /actuator/health
  • List of all actuator endpoints: /actuator
  • Metrics: /actuator/metrics
  • App env: /actuator/env
  • And so on...

Security:

Out of the box, many sensitive actuator endpoints are secured and not exposed over HTTP. You might want to expose them depending on your needs, but you must be careful and ensure they're protected, especially in a production environment.

For example, to expose all endpoints:

management.endpoints.web.exposure.include=*

To expose specific endpoints:

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

Customization:

Actuator provides numerous properties and extension points for customization:

  • You can change the base path using management.endpoints.web.base-path.

  • Add custom health indicators.

  • Extend existing or create new endpoints.

Conclusion:

Spring Boot Actuator simplifies the process of adding production-ready features to your application. Whether you need detailed metrics, health checks, or application insights, Actuator has you covered. Just remember to always secure sensitive endpoints, especially in production environments.

  1. Customizing Spring Boot Actuator endpoints:

    • Extend Endpoint class and customize endpoint behavior.
    • Example:
      @Endpoint(id = "custom")
      public class CustomEndpoint {
          // Custom implementation
      }
      
  2. Configuring Spring Boot Actuator in application.properties:

    • Customize Actuator properties in application.properties.
    • Example:
      management.endpoints.web.exposure.include=health,info
      
  3. Enabling and disabling specific Actuator endpoints:

    • Use management.endpoints.web.exposure.include and management.endpoints.web.exposure.exclude properties.
    • Example:
      management.endpoints.web.exposure.include=health,info