CodeGym /Courses /Module 5. Spring /Lecture 111: Introduction to Spring Boot Actuator

Lecture 111: Introduction to Spring Boot Actuator

Module 5. Spring
Level 19 , Lesson 0
Available

Programming isn't just about writing code and fixing bugs. It's also about monitoring apps in real time so you can figure out what's slowing the system, which resources are overloaded, and where those precious bytes of memory in the JVM are disappearing to. Imagine you're the pilot of an airplane and you need to see your craft's dashboard — otherwise you're flying blind. That's where Spring Boot Actuator comes in! It's a tool that gives you powerful capabilities for monitoring and managing Spring Boot applications. It lets you:

  • Track the health of your application.
  • Get key metrics (CPU, memory, request performance).
  • Inspect operational characteristics of the system.
  • Access diagnostics and app state information.

How to integrate Actuator into a Spring Boot project?

First, you need to add the Actuator dependency to your project. Spring Boot makes it easy to enable built-in tools via Maven or Gradle. It's as simple as always.

Adding the Maven dependency: Add the following dependency to your pom.xml:


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

Adding the dependency in Gradle: If you're using build.gradle, include:


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

Done! Now Actuator is integrated into your app.


Overview of built-in endpoints

Spring Boot Actuator provides a bunch of ready-made endpoints for monitoring and managing your app. Here are some of the most popular ones:

Endpoint Description
/health Shows the application's health (healthy or not)
/info Displays info about the app, like version, description
/metrics Shows metrics such as CPU, memory, and request performance
/env Contains environment properties (environment variables)
/loggers Provides information about current logging settings
/beans Prints a list of all beans registered in your context
/threaddump A thread dump of threads inside the JVM that helps find deadlocks

Now imagine: you run the app, open http://localhost:8080/actuator/health, and it tells you the magic word UP. That means your application is up and running. Hooray!


Enabling and configuring Actuator

Like everything in Spring Boot, Actuator is easy to configure. You use the usual configuration files: application.properties or application.yml.

Configuring the base path for endpoints: By default all Actuator endpoints live under /actuator. But what if you want to change it, for example to hide them from prying eyes? Just set a new path in application.properties:


management.endpoints.web.base-path=/management

Now the endpoints will be available under /management.

Enabling and disabling endpoints: Being able to enable or disable specific endpoints is part of Actuator's magic. For example:


management.endpoint.health.enabled=true
management.endpoint.beans.enabled=false

This way you enable /health and disable /beans.

Which endpoints are exposed? You can configure exposure (by default only some endpoints are exposed):


management.endpoints.web.exposure.include=*

Or allow access to just a couple of favorites:


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

This is useful when you want to minimize security risks.


Quick practical example

Let's enable Actuator and test it on a simple Spring Boot app. We'll create a demo REST API and add Actuator for monitoring.

Step 1: Add the dependency to pom.xml (or build.gradle):


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

Step 2: Configure application.properties:


management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always

Step 3: Create the REST API: Let's create a simple HelloController:


@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hi, Spring Boot Actuator!";
    }
}

Step 4: Run the app: After starting the app, open your browser or Postman and hit the endpoints:

  • http://localhost:8080/actuator/health — app health.
  • http://localhost:8080/actuator/info — app information.
  • http://localhost:8080/actuator/metrics — CPU, memory and request metrics.

Results:
At /health you'll see the following magic:


{
  "status": "UP",
  "details": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 500000000000,
        "free": 400000000000,
        "threshold": 10485760
      }
    }
  }
}

This means your app is running, and your disk isn't filled with cat pics yet.


Key Actuator features

  1. Easy integration: just add one dependency.
  2. Flexible configuration: enable only the endpoints you need.
  3. Extensibility: easy to add custom metrics.
  4. Effective diagnostics: helps you quickly figure out why the app is acting up.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION