Dynamic health-checks with spring boot
Tags: java, health, springboot, actuator
Published: 2021-11-08 19:28:50

---
title: Dynamic health-checks with spring boot
published: true
description: 
tags: java, health, springboot, actuator
//cover_image: https://images.unsplash.com/photo-1572996489084-bd293caab387?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&w=6000
---

Health-check is an important part of any project that you create. You can deploy your service, and expose an endpoint that emits its health periodically. These endpoints could be called from your container, your dependent clients, your cloud deployment infrastructure, etc. As long as the service is healthy or "UP", the consumers are happy. When it goes down, the consumers can either pivot to something else, or show an error to the end user to wait till it's back up. 

### Dynamic Health-checks

Let's imagine you have a use-case, where your spring boot application is already up, and its talking to other systems, and you want to register a new health endpoint while your application is running. Spring boot will already give something like this already out of the box -  

```json
{
  "status": "UP",
  "components": {
    "ping": {
      "status": "UP"
    }
  }
}
```

So, the application is up and running now, and there is a new dependency that you have identified, that you want to register. Usually, the health-checks are static - either configured in the yaml / properties file or defined as part of your code by implementing [HealthIndicator](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/HealthIndicator.html). 

But, there is another way to register a health indicator, while the system is running, this is using [HealthContributorRegistry](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/HealthContributorRegistry.html). Spring boot gives you this from the application context, so you can Autowire it in your bean. 

```java
@SpringBootApplication
public class Application {

	@Autowired
	HealthContributorRegistry healthContributorRegistry;
	
	public static void main(final String[] args) {
		SpringApplication.run(Application.class, args);
	}
}
```

Once you get the object, there are really simple methods to invoke to `register` or `unregister` contributors to your application while its running. 

```java
healthContributorRegistry.registerContributor("myNewService", new MyNewServiceImpl());
```

Your [MyNewServiceImpl.java](http://MyNewServiceImpl.java) should implement `HealthIndicator`.

```json
{
  "status": "UP",
  "components": {
    "ping": {
      "status": "UP"
    },
		"myNewService": {
      "status": "UP"
    }
  }
}
```

You can also unregister it, if you want to remove something at runtime. 

```java
healthContributorRegistry.unregisterContributor("myNewService");
```

Hope this helps you, your application and your application's consumers stay healthy 🙂

[Spring Boot documentation](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/HealthContributorRegistry.html)
Originally published [here] (https://arpit.dev/Dynamic-health-checks-with-spring-boot-33faf8fad89c4697ad468cf8e610eef5)