[JAVA] [Spring Boot Actuator] How to manually register your own health check process

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.

How to automatically register your own health check process

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;
  }
}

How to manually register your own health check process

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;
  }
}

Summary

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.

Reference site

Recommended Posts

[Spring Boot Actuator] How to manually register your own health check process
How to create your own Controller corresponding to / error with Spring Boot
[Forge] How to register your own Entity and Entity Render in 1.13.2
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
[Introduction to Spring Boot] Form validation check
How to split Spring Boot message file
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to add a classpath in Spring Boot
Create your own Utility with Thymeleaf with Spring Boot
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
Spring Boot --How to set session timeout time
How to implement authentication process by specifying user name and password in Spring Boot
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to use CommandLineRunner in Spring Batch of Spring Boot
How to boot by environment with Spring Boot of Maven
How to read your own YAML file (*****. Yml) in Java
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
Try Spring Boot from 0 to 100.
[AWS] How to check logs
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
How to check JSF version
Introduction to Spring Boot Part 1
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
Spring Boot 2.0 Actuator, 3 changes you need to know to get it working