Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom health check in spring boot health?

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 

This will add several useful endpoints to your application. One of them is /health. When you start your application and navigate to the /health endpoint you will see it returns already some data.

{     "status":"UP",     "diskSpace": {         "status":"UP",         "free":56443746,         "threshold":1345660     } } 

How to add a custom health check in spring boot health?

like image 709
shabinjo Avatar asked Jun 30 '17 15:06

shabinjo


People also ask

Does spring boot provide health check?

Spring Boot Actuator module helps you monitor and manage your Spring Boot application by providing production-ready features like health check-up, auditing, metrics gathering, HTTP tracing etc. All of these features can be accessed over JMX or HTTP endpoints.

How do I create a health check API?

To show health check status on the dashboard, you have to configure through the HealthCheck-UI settings. Name: Name of the service which implements the Health Check API. Uri: The endpoint which provides health check data. HealthChecks: The collection of health checks URIs to evaluate.

How do I create a custom actuator endpoint in spring boot?

To create a custom actuator endpoints, Use @Endpoint annotation on a class. Then leverage @ReadOperation / @WriteOperation / @DeleteOperation annotations on the methods to expose them as actuator endpoint bean as needed.


2 Answers

Adding a custom health check is easy. Just create a new Java class, extend it from the AbstractHealthIndicator and implement the doHealthCheck method. The method gets a builder passed with some useful methods. Call builder.up() if your health is OK or builder.down() if it is not. What you do to check the health is completely up to you. Maybe you want to ping some server or check some files.

@Component public class CustomHealthCheck extends AbstractHealthIndicator {     @Override     protected void doHealthCheck(Health.Builder bldr) throws Exception {         // TODO implement some check         boolean running = true;         if (running) {           bldr.up();         } else {           bldr.down();         }     } } 

This is enough to activate the new health check (make sure @ComponentScan is on your application). Restart your application and locate your browser to the /health endpoint and you will see the newly added health check.

{     "status":"UP",     "CustomHealthCheck": {         "status":"UP"     },     "diskSpace": {         "status":"UP",         "free":56443746,         "threshold":1345660     } } 
like image 104
shabinjo Avatar answered Oct 13 '22 11:10

shabinjo


Since Spring Boot 2.X

As stated by @yuranos87 the actuator concept has changed in Spring Boot 2.X but you can still add custom health checks easily by implementing HealthIndicator or for reactive applications ReactiveHealthIndicator:

@Component public class CacheHealthIndicator implements HealthIndicator {  @Override public Health health() {     long result = checkSomething();     if (result <= 0) {         return Health.down().withDetail("Something Result", result).build();     }     return Health.up().build();         } } 

or

@Component public class CacheHealthIndicator implements ReactiveHealthIndicator {  @Override public Mono<Health> health() {     return Mono.fromCallable(() -> checkSomething())         .map(result -> {             if (result <= 0) {                 return Health.down().withDetail("Something Result", result).build();             }             return Health.up().build();         });    } } 

Additionally you can add or extend any endpoint with @Endpointor @EndpointWebExtension. Endpoints here are info, health and many more. So you can add custom health check by using @Endpoint but it is much easier to do with HealthIndicator.

You can find more information about custom health checks and custom endpoints in the spring boot documentation.

like image 24
Tobske Avatar answered Oct 13 '22 10:10

Tobske