Sub topic 1
What Actuator Provides and How to Add It
Prerequisites
Assume you know basic Spring Boot structure, Maven or Gradle dependencies, YAML configuration, HTTP, and JSON. Actuator is Spring Boot’s production-monitoring module. It adds endpoints that expose application health, metrics, configuration, and runtime information.
Adding Actuator
For a Maven project, add the Actuator starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Spring Boot enables several endpoints automatically. HTTP endpoints use the /actuator base path, so health is usually available at /actuator/health.
Enabling and Exposing Endpoints
By default, only the health endpoint is exposed over HTTP. Choose the endpoints that your operators need:
management:
endpoints:
web:
exposure:
include: health,info,metrics
jmx:
exposure:
include: health,info,metricsHTTP exposure creates URLs such as /actuator/info. JMX exposure makes the same endpoint operations available through Java Management Extensions, which are commonly used by JVM administration tools. Exposure controls visibility; it does not replace authentication and authorization.
Reading and Configuring Health
The health endpoint aggregates checks from the application and its dependencies. It returns UP, DOWN, or another status. Configure its details and Kubernetes probe groups like this:
management:
endpoint:
health:
show-details: when-authorized
probes:
enabled: trueshow-details: when-authorized prevents anonymous users from seeing dependency details. Probe groups provide liveness and readiness views for container platforms.
Writing a Custom Health Indicator
A HealthIndicator is a component that reports whether one dependency is usable:
@Component
public class PaymentGatewayHealthIndicator implements HealthIndicator {
private final PaymentClient client;
public PaymentGatewayHealthIndicator(PaymentClient client) {
this.client = client;
}
@Override
public Health health() {
try {
client.ping();
return Health.up().withDetail("gateway", "reachable").build();
} catch (Exception ex) {
return Health.down(ex).build();
}
}
}Spring Boot adds this result to the aggregate under a name based on the class. Keep checks fast and bounded. A slow external call can make the health endpoint slow.
Exercise
Why should health details usually be restricted?
Show answer
They can reveal dependency names and internal runtime information that unauthenticated users should not see.