Introduction
Welcome back! In our last lesson, you did an excellent job implementing secure inter-service communication using OpenFeign and a RequestInterceptor. This allows our blog-service to validate a user's identity by calling the auth-service, a crucial pattern for any microservices architecture.
Now that our services can talk to each other, we need to address another fundamental question in distributed systems: "Are our services healthy?" In a complex system with many moving parts, we need an automated way to monitor the status of each service. This is a key aspect of observability.
In this lesson, you will add health check endpoints to each of our microservices (api-gateway, auth-service, and blog-service) using Spring Boot Actuator. These endpoints will be vital later when we deploy our system to Kubernetes, as it uses them to decide whether to restart a service or stop sending it traffic.
MERN Stack Comparison
In an Express.js application, you might manually create a /health or /status endpoint that returns a simple 200 OK. You might also add logic to check database connectivity within that endpoint. Spring Boot Actuator provides a much more powerful and standardized framework for this. It automatically provides not just a basic health status, but also detailed diagnostics about dependencies like databases, disk space, and more, all with minimal configuration.
1. What is Spring Boot Actuator?
Spring Boot Actuator is a sub-project of Spring Boot that provides production-ready features for monitoring and managing your application. Once you add it as a dependency, it exposes a series of built-in endpoints over HTTP that provide valuable information about the application's state.
Let's watch a quick introduction to see what Actuator offers.
Mastering Spring Boot Actuator: Monitoring & Managing Your Application
The video 'Mastering Spring Boot Actuator' from CodeSnippet gives a concise overview of what Actuator is and the problems it solves in a production environment.
Please watch from the beginning until 00:57 to get a high-level understanding of Actuator's purpose.
The most fundamental endpoint provided by Actuator is /health, which we will focus on in this lesson. When everything is configured, you'll see a response like this, which lists the available Actuator endpoints:

2. Adding Actuator to Your Microservices
Let's start by adding the Actuator dependency to all three of our microservices: api-gateway, auth-service, and blog-service.
This is a straightforward change to each service's pom.xml file.
<dependencies>
<!-- ... other dependencies ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Your Task (5 minutes):
- Open the
pom.xmlfile for theapi-gateway,auth-service, andblog-serviceprojects. - Add the
spring-boot-starter-actuatordependency inside the<dependencies>block in all three files. - Let your IDE sync the Maven projects to download the new dependency.
3. Exposing and Detailing Health Information
For security reasons, Spring Boot 2 and later versions expose very little information by default. If you were to restart a service now and navigate to /actuator/health, you would only see a minimal response:
{
"status": "UP"
}
This is useful, but not very informative. We want to see the status of individual components, like our database connection. We can configure this in the application.yml file.
The article "How to Configure Spring Boot Actuator for Health Checks" by OneUptime provides excellent YAML examples for this.
How to Configure Spring Boot Actuator for Health Checks - OneUptime
This article provides clear, production-focused examples for configuring Actuator health checks. We'll use it to understand how to expose detailed health information.
Read the 'Basic Configuration' section. Pay close attention to the YAML structure and the management.endpoint.health.show-details property. We will use always for our local development.
Your Task (10 minutes):
Let's apply this to our blog-service, which has a database connection we want to monitor.
-
Open the
src/main/resources/application.ymlfile in theblog-serviceproject. -
Add the following configuration:
management: endpoints: web: exposure: include: health,info,prometheus # Expose health, info and prometheus endpoints endpoint: health: show-details: always # 'always' is fine for dev, use 'when_authorized' in prodexposure.include: This explicitly lists which Actuator endpoints are exposed over HTTP. It's a security best practice to only expose what you need. We've includedhealth,info(for general app info), andprometheus(which we'll use for metrics later).show-details: always: This tells the/healthendpoint to always show full component details.
-
Restart the
blog-service. -
Ensure your PostgreSQL Docker container is running.
-
Open your browser or Postman and access
http://localhost:8082/actuator/health.
You should now see a much more detailed JSON response, including the status of the database (db) and diskSpace. Spring Boot automatically detects the spring-data-jpa and postgresql driver on your classpath and adds a corresponding health indicator.
4. Health Checks for Kubernetes: Liveness vs. Readiness
This is where Actuator becomes truly powerful for our goal of deploying to Kubernetes. Kubernetes uses two types of probes (health checks) to manage application containers:
- Liveness Probe: "Is the application running correctly?" If this probe fails, Kubernetes assumes the application is in an unrecoverable state (e.g., a deadlock) and restarts the container.
- Readiness Probe: "Is the application ready to serve traffic?" If this probe fails, Kubernetes stops sending new traffic to the container but leaves it running, giving it time to recover. This is used for temporary issues, like a lost database connection.
Spring Boot Actuator has built-in support for these specific probes. We can configure Health Groups to create separate endpoints for /actuator/health/liveness and /actuator/health/readiness.
How to Configure Spring Boot Actuator for Health Checks - OneUptime
Let's return to the OneUptime article to see how to configure these specific probes. This directly aligns with our future Kubernetes deployment.
Please read the following sections: Health Groups: Understand how to define liveness and readiness groups in YAML. Kubernetes Integration: See how these endpoints map directly to a Kubernetes deployment.yaml file. You don't need to write the YAML now, just understand the connection. Spring Boot Kubernetes Properties: Note the management.health.probes.enabled: true property. Health Check Best Practices: Look at the flowchart distinguishing what to check for liveness vs. readiness.
Your Task (15 minutes):
Let's implement this for all three services.
-
In
blog-serviceandauth-service(application.yml):
These services depend on the database, so their readiness check should include it.management: endpoints: web: exposure: include: health,info,prometheus endpoint: health: show-details: always probes: # Add this block to enable Kubernetes probes enabled: true health: group: readiness: # Define the readiness group include: db,diskSpace # Include checks for DB and disk spaceNote: We only define the
readinessgroup. Thelivenessgroup is provided by default and only checks the application's internal state, which is exactly what we want. -
In
api-gateway(application.yml):
The gateway does not have a database, so its readiness check is simpler.management: endpoints: web: exposure: include: health,info,prometheus endpoint: health: show-details: always probes: enabled: true health: group: readiness: include: diskSpace # Just check disk space -
Restart all three services.
Runtime Observation
Now, test the new endpoints for the blog-service (running on port 8082):
- Liveness:
http://localhost:8082/actuator/health/liveness- You should see
{"status":"UP"}. This probe passes as long as the application itself has not crashed.
- You should see
- Readiness:
http://localhost:8082/actuator/health/readiness- You should see a detailed response including the
dbanddiskSpacecomponents. This probe passes only if the app is running and its critical dependencies are available.
- You should see a detailed response including the
Debugging Task: Simulating a Failure
Now for the most important part: observing failure.
- Keep your microservices running.
- Open a terminal and stop your PostgreSQL database container:
docker stop <your-postgres-container-name>. - Wait about 10-15 seconds.
Your Task (5 minutes):
With the database down, answer the following questions by testing the endpoints for the blog-service (http://localhost:8082):
- What HTTP status code and JSON body do you get from
/actuator/health/liveness? - What HTTP status code and JSON body do you get from
/actuator/health/readiness? - Why are the responses different, and what would Kubernetes do in this scenario based on these probe results?
Click for the answer
- /actuator/health/liveness: You will still get a
200 OKstatus with the body{"status":"UP"}. The application process itself is still running fine; it hasn't crashed or deadlocked. - /actuator/health/readiness: You will get a
503 Service Unavailablestatus code. The JSON body will show an overall status ofDOWN, with thedbcomponent also showingDOWNand providing error details. - Why and what would Kubernetes do?
- The responses are different because the liveness probe is only checking if the application is "alive," while the readiness probe is checking if it's "ready to serve traffic" (which it isn't, because it can't reach the database).
- Based on these results, Kubernetes would:
- Not restart the container, because the liveness probe is still passing. Restarting wouldn't help if the database is the problem.
- Remove the container from the service's load balancer, because the readiness probe is failing. It would stop sending user traffic to this faulty instance until the database connection is restored and the readiness probe passes again.
This is the intelligent, self-healing behavior that makes platforms like Kubernetes so powerful, and Spring Boot Actuator provides the perfect hooks to enable it.
Remember to restart your PostgreSQL container with docker start <your-postgres-container-name> when you're done.
Conclusion
Fantastic work! You have now equipped all your microservices with production-grade health checks using Spring Boot Actuator. This is a non-negotiable feature for any real-world distributed system.
Key Takeaways:
- Spring Boot Actuator is the standard way to add monitoring and management capabilities to Spring applications.
- By default, Actuator endpoints are secure and must be explicitly exposed via configuration (
management.endpoints.web.exposure.include). - Actuator automatically provides health indicators for common dependencies like databases (
db) anddiskSpace. - Health Groups allow you to create distinct
livenessandreadinessprobes, which are essential for effective orchestration in Kubernetes. - Liveness (
/health/liveness) tells Kubernetes if the app is running and should be restarted if it fails. - Readiness (
/health/readiness) tells Kubernetes if the app can handle traffic and should be removed from the load balancer if it fails.
Preview of the Next Lesson:
Our services are now observable, but they are still just Java processes running on our local machine. To prepare them for deployment anywhere—from another developer's machine to a cloud environment—we need to package them into a standard, portable format. In the next lesson, you will write a multi-stage Dockerfile to create an optimized, production-ready container image for a Spring Boot application.