A health indicator is a Spring Boot Actuator component that reports the status of an external system used by an application. Actuator combines these individual results into the application's overall health, which the /health endpoint can expose. section 15.2.1
Health indicators communicate one of four statuses:
- UP means the external system is reachable. section 15.2.1
- DOWN means the system is unavailable or unreachable. section 15.2.1
- OUT_OF_SERVICE means the system is reachable but temporarily unavailable. section 15.2.1
- UNKNOWN means the system's status cannot be determined; unknown results do not affect the aggregate health. section 15.2.1
Creating a custom indicator
When Spring Boot has no built-in indicator for an integrated legacy system, create a Spring bean that implements HealthIndicator and override health(). Return Health.up(), Health.down(), or Health.outOfService(), adding details with withDetail() when useful. A realistic implementation would make a remote call and translate its response into the appropriate status. section 15.3.2
Listing 15.5 An unusual implementation of HealthIndicator
package tacos.actuator;
import java.util.Calendar;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class WackoHealthIndicator
implements HealthIndicator {
@Override
public Health health() {
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if (hour > 12) {
return Health
.outOfService()
.withDetail("reason",
"I'm out of service after lunchtime")
.withDetail("hour", hour)
.build();
}
if (Math.random() <= 0.1) {
return Health
.down()
.withDetail("reason", "I break 10% of the time")
.build();
}
return Health
.up()
.withDetail("reason", "All is good!")
.build();
}
}
The complete example shows a component implementation, the required health() method, status builders, and diagnostic details. section 15.3.2
