Spring Boot Actuator: 5 Performance Boost Tips

Are you ready to take your application to the next level? In the world of software development, it’s not enough to create an application; you also need to ensure it runs smoothly in a production environment. This is where “Spring Boot Actuator” comes into play. In this comprehensive guide, we’ll walk you through the process of enhancing your application’s monitoring and management capabilities using Spring Boot Actuator.

Step 1: Understanding the Need

Why Additional Features Are Essential

After thoroughly testing your application, you’ll likely find that deploying it in a production environment requires more than just functional code. You need features that enable monitoring and management. Traditionally, this might involve maintaining a dedicated support team to ensure your application is always up and running.

Step 2: What is Spring Boot Actuator?

Spring Boot Actuator is a powerful feature bundled with Spring Boot. It provides a set of predefined endpoints that simplify the process of preparing your application for production deployment. These endpoints allow you to monitor and manage various aspects of your application seamlessly.

Step 3: Spring Boot Actuator Endpoints

Spring Boot Actuator offers a variety of endpoints to cater to different monitoring and management needs:

  1. info: Provides arbitrary information about your application, such as author, version, and licensing.
  2. health: Checks the liveness probe of your application to ensure it’s running and accessible.
  3. env: Displays all environment variables used by your application.
  4. configprops: Lists all configuration properties utilized by your application.
  5. beans: Shows all the bean definitions within the IoC container.
  6. thread dump: Provides access to the current JVM thread dump.
  7. metrics: Offers runtime information about your application, including memory usage, CPU utilization, and heap status.
  8. loggers: Displays loggers and their logging levels.
  9. logfile: Shows the application’s log file.
  10. shutdown: Allows for remote application shutdown.
  11. sessions: Presents active HTTP sessions of the web application.
  12. conditions: Shows the conditions that influence auto-configurations.

Step 4: Enabling Actuator Endpoints

Before we can start configuring and using Actuators, we need to add the Actuator dependency to our project pom.xml.

Spring Boot Starter Actuator Dependency

Maven:

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

Gradle:

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

To make use of these valuable endpoints, you’ll need to enable them by adding the “spring-boot-starter-actuator” dependency to your Spring Boot project. This will grant you access to the endpoints via URLs like “http://localhost:8080/actuator/{endpointId}“.

These endpoints are exposed by Spring Boot in two ways:

  1. JMX: JMX, or Java Management Extensions, is a specification provided by Java as part of J2SE5. It standardizes the API for managing devices, servers, and more. With JMX, you can programmatically manage devices or servers at runtime through JMX extensions. For example, instead of manually configuring a datasource in a WebLogic server through its console, you can automate the datasource configuration on an application server using JMX endpoints exposed by the application server.
  2. HTTP/Web: These are REST endpoints exposed over the HTTP protocol, making them accessible from a web browser or any HTTP client.

However, it’s worth noting that it’s recommended to expose Actuator endpoints through JMX rather than HTTP/Web endpoints due to security reasons. All Actuator endpoints are available for access via both JMX and HTTP/Web by default, and you don’t need to write any special code to enable or support them. You simply need to configure which endpoints you want to expose over which protocol, and Spring Boot Actuator will take care of exposing them accordingly.

To make an endpoint accessible in Spring Boot Actuator, you need to do two things:

  1. Enable the Endpoint
  2. Expose the Endpoint through JMX, HTTP, or Both

By default, all the endpoints of Spring Boot Actuator are enabled, except for the “shutdown” endpoint. If you want to disable these endpoints by default, you can add a property in your application.properties file:

Java
management.endpoints.enabled-by-default=false

Now, you can enable each individual endpoint in a controlled way using the endpoint’s ID, as shown below:

Java
# Enable specific Actuator endpoints
management.endpoint.info.enabled=true
management.endpoint.shutdown.enabled=true
management.endpoint.endpointId.enabled=true

In your application.properties file, you can include the following configuration to expose all the endpoints:

Java
# Expose all endpoints
management.endpoints.web.exposure.include=*

This configuration tells Spring Boot Actuator to include all endpoints for web/HTTP access.

Excluding Specific Actuator Endpoints

Java
# Exclude specific endpoints by their ID
management.endpoints.web.exposure.exclude=shutdown, sessions, conditions

Using application.yaml:

In your application.yaml file, you can include the following configuration to expose all the endpoints:

Java
management:
  endpoints:
    web:
      exposure:
        include: "*"

Next, you’ll need to specify how you want to expose these endpoints, either through JMX or HTTP. By default, only two endpoints, “info” and “health,” are exposed for security reasons. If you want more Actuator endpoints to be accessible, you can configure this using the following properties in either application.properties or application.yaml

In application.properties

Java
# Expose Actuator endpoints for both JMX and HTTP/Web access
management.endpoints.jmx.exposure.include=info, health, env, configProps
management.endpoints.web.exposure.include=info, health, env, configProps

In application.yaml:

Java
management:
  endpoints:
    web: #Web endpoints configuration
      exposure:
        include: info, health, env, configProps
    jmx: #JMX endpoints configuration
      exposure:
        include: info, health, env, configProps

Before we delve into fine-tuning endpoint exposure, let’s make sure your Spring Boot application is up and running.

By testing your application first, you can ensure that everything is set up correctly before customizing Actuator endpoint exposure in the next section.

Step 5: Fine-Tuning Endpoint Exposure

If the predefined endpoints don’t cover your specific needs, you can extend them or create your own. Here’s an example of how to customize the “health” and “info” endpoints:

Java
@Component
class AppHealthEndpoint implements HealthIndicator {
  public Health health() {
    // Perform checks on external or application-dependent resources and return UP or DOWN.
    return Health.Up().build();
  }
}

@Component
class AppInfoEndpoint implements InfoContributor {
  public void contribute(Builder builder) {
    builder.withDetails("key", "value").build();
  }
}

Step 6: Building Custom Endpoints

Actuator endpoints are essentially REST APIs, and you can build your custom endpoints using Spring Boot Actuator API. This is preferable over standard Spring REST controllers because it allows for JMX access and management.

Here’s a simplified example of how to create a custom endpoint:

Java
@Component
@Endpoint(id = "cachereload")
class CacheReloadEndpoint {
  @UpdateOperation
  public int reloadCache(String resource) {
    // Implement your custom logic here.
  }
}

To trigger a cache reload using this custom endpoint, you can send an HTTP PUT request like this:

Bash
http://localhost:8081/actuator/cachereload?resource=cities.properties

Testing Specific Actuator Endpoints

  • Run Your Application: Ensure that your Spring Boot application is running.
  • Access the Info Endpoint: Open a web browser or use a tool like curl to make an HTTP GET request to the following URL:
  1. Info Endpoint (/actuator/info)
Java
http://localhost:8080/actuator/info
JSON
{
    "app": {
        "name": "boot-actuator",
        "version": "1.0.0"
    },
    "author": "Pavan"
}

2. Health Endpoint (/actuator/health)

http://localhost:8080/actuator/health

Spring Boot Actuator health

3. Environment (Env) Endpoint (/actuator/env)

http://localhost:8080/actuator/env

Spring Boot Actuator env

4. Configuration Properties (ConfigProps) Endpoint (/actuator/configprops)

http://localhost:8080/actuator/configprops

These are the expected JSON responses when you make HTTP GET requests to the specified Actuator endpoints.

Conclusion:

In this guide, you’ve learned the essentials of Spring Boot Actuator, enabling you to monitor, manage, and customize your Spring Boot applications effectively. You’ve discovered how to test Actuator endpoints, fine-tune their exposure, and explore the associated GitHub repository for practical insights. Armed with this knowledge, you’re better equipped to maintain robust applications in production environments.

Leave a Comment