0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Health Checks and Metrics

Implement Spring Boot Actuator for exposing health endpoints and collecting application metrics.

Health Checks and Metrics is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Spring Boot Actuator?

When running microservices, monitoring their health and performance is super important. Spring Boot Actuator provides a set of production-ready features to help you do just that!

It exposes operational information about your running application, making it easier to observe and manage.

Add Actuator Dependency

To start using Actuator, you just need to add its dependency to your Spring Boot project. This is typically done in your pom.xml for Maven or build.gradle for Gradle.

Here's how to add it with Maven:

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

Explore Default Endpoints

Once the Actuator dependency is added, Spring Boot automatically configures several useful endpoints. These endpoints provide valuable insights into your application's state.

  • /actuator/health: Shows the application's health status.
  • /actuator/info: Displays general application information.
  • /actuator/metrics: Provides detailed metrics data.

Checking Application Health

The /actuator/health endpoint is a fundamental part of monitoring. It tells you if your application and its integrated components (like databases or disk space) are functioning correctly.

A simple "UP" status means everything is good! Run this basic app and check its health endpoint.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ActuatorDemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(ActuatorDemoApplication.class, args);
  }
}

Accessing Health Endpoint

After running the previous application, open your browser and navigate to http://localhost:8080/actuator/health.

You should see a JSON response indicating the application's overall status, typically {"status":"UP"}.

This is a quick way to verify if your service is alive!

Create Custom Health Checks

You can extend Actuator's health checks to include your own custom logic. For example, checking the status of an external API, a message queue, or a specific business process.

To do this, you implement the HealthIndicator interface:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomServiceHealthIndicator implements HealthIndicator {
  private final String SERVICE_NAME = "MyExternalService";

  @Override
  public Health health() {
    if (isServiceUp()) {
      return Health.up().withDetail(SERVICE_NAME, "Available").build();
    }
    return Health.down().withDetail(SERVICE_NAME, "Not Available").build();
  }

  private boolean isServiceUp() {
    // Simulate checking an external service
    return Math.random() > 0.3; // 70% chance of being up
  }
}

Understanding Application Metrics

Metrics are numerical data points that describe your application's behavior over time. They are crucial for understanding performance, resource usage, and identifying potential bottlenecks.

Spring Boot Actuator, powered by Micrometer, automatically collects various metrics:

  • CPU and memory usage
  • HTTP request counts and latencies
  • Database connection pool statistics

Querying Metrics Data

The /actuator/metrics endpoint provides a list of all available metrics. You can then query specific metrics for detailed information.

  • Visit http://localhost:8080/actuator/metrics to see available metrics.
  • Then, try http://localhost:8080/actuator/metrics/jvm.memory.used to see the specific memory usage metric.

This allows you to gather precise data for monitoring dashboards.

Implement Custom Metrics

Beyond the default metrics, you can track application-specific events using custom metrics. Micrometer provides simple APIs for different metric types like Counters, Gauges, and Timers.

Here's an example of a custom Counter:

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;

@Service
public class MyService {
  private final Counter customCounter;

  public MyService(MeterRegistry meterRegistry) {
    this.customCounter = Counter.builder("my_app.processed.items")
                                .description("Number of items processed by MyService")
                                .register(meterRegistry);
  }

  public void processItem() {
    System.out.println("Processing an item...");
    customCounter.increment(); // Increment the counter
  }
}

Customize Endpoint Exposure

By default, only /health and /info are typically exposed over HTTP. You can configure which other endpoints Actuator makes available using application.properties or application.yml.

To expose more endpoints like metrics, beans, and env, add this line:

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

Actuator Knowledge Check

Let's quickly check your understanding of Spring Boot Actuator's benefits.

Lesson Recap

Great job! In this lesson, you learned about:

  • Spring Boot Actuator for monitoring and managing microservices.
  • Implementing health checks using the /actuator/health endpoint.
  • Creating custom health indicators for specific services.
  • Understanding and collecting application metrics via /actuator/metrics.
  • Implementing custom metrics with Micrometer.
  • The importance of securing Actuator endpoints in production.

Next, we'll dive into centralized logging and dashboarding!

Frequently asked questions

Is the “Health Checks and Metrics” lesson free?

Yes — the full text of “Health Checks and Metrics” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Health Checks and Metrics”?

Implement Spring Boot Actuator for exposing health endpoints and collecting application metrics. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Health Checks and Metrics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Centralized Logging with ELK Stack
  2. Health Checks and Metrics
  3. Alerting and Dashboarding
← Back to Spring Boot 4 Microservices & REST APIs