The Spring Boot Actuator is an endpoint (/ actuator / health
) that provides the operating status (health check results) of the instance itself and external resources (disk, DB, messaging service, cache service, etc.) that depend on the instance. , / Actuator / health / {name}
), which can also provide its own health check state.
Although it is introduced with sample code in the Spring Boot reference, if you register a class that implements the HealthIndicator
interface in the DI container, the Spring Boot Actuator will detect it automatically.
@Component //Register with DI container by component scan
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return checkWeatherAvailable() ? Health.up().build(): Health.down().build();
}
private boolean checkWeatherAvailable() {
boolean result = false;
//Life and death monitoring process
// ...
return result;
}
}
The Spring Boot Actuator manages the objects that perform health check processing in HealthIndicatorRegistry
, and you can also manually register your own health check processing via HealthIndicatorRegistry
obtained from the DI container.
For example, if you want to change (increase or decrease) the target of health check processing according to the set value, you can use the manual registration mechanism.
@Configuration
public class MyConfiguration {
// ...
@Autowired
void addExternalServiceHealthIndicators(HealthIndicatorRegistry registry, MyProperties properties) {
properties.getExternalServices().stream().filter(s -> StringUtils.hasText(s.getHealthCheckUrl()))
.forEach(s -> registry.register(s.getName(), new UrlBasedHealthIndicator(s.getHealthCheckUrl())));
}
}
public class UrlBasedHealthIndicator implements HealthIndicator {
private final String url;
public UrlBasedHealthIndicator(String url) {
this.url = url;
}
@Override
public Health health() {
return checkWeatherAvailable() ? Health.up().build(): Health.down().build();
}
private boolean checkWeatherAvailable() {
boolean result = false;
//Life and death monitoring process
// ...
return result;
}
}
I don't use Spring Boot as a commercial application in the project I'm currently involved in, but I use an external system simulator (mainly used for testing ... a simulator used for internal management even in a commercial environment). I wanted to create a mechanism that can increase or decrease the operating status of the resources handled in the simulator depending on the set value.
Recommended Posts