Part I: Integrating Spring Boot Actuator with Habitat Health Checks
According to JetBrains’ State of Developer Ecosystem 2019, Java is the most popular primary programming language among developers, and Spring Boot is the most popular framework for Java.
Chef Habitat’s Health Check Hook can be implemented to integrate with the Spring Boot Actuator health endpoint, allowing the developer to create meaningful Custom Health Indicators that will be reflected by the Chef Habitat Supervisor’s health endpoint for that particular service.
ShuttleOps is a No-Code CI/CD platform that will enable us to – within minutes – take this Habitat Package and have it running in AWS behind a Load Balancer which uses these health checks to manage its pool of upstream instances.
We can take an existing Spring Boot codebase and add a habitat directory which is structured as follows:
All of the components in the habitat directory will be explored in the video walkthrough and are available in the ShuttleOps Examples Repository. However, we should highlight their significance, especially to Spring Boot, at a high level:
The plan.sh file defines the Package metadata, the build-time and run-time dependencies, and also makes use of the Build Phase Callbacks to build a jar file using Maven.
The run hook executes the resulting jar file and passes a flag to have Spring Boot make use of the rendered config directory.
The config directory contains Configuration Templates – in particular a Spring Boot properties file which can be modified based on system data, default values in the default.toml file, and also Configuration Updates.
The default.toml file contains default values that exist without Configuration Updates.
The health-check hook defines the health of the service using its exit code. Our goal is to make it reflect the output of the Spring Boot Actuator health endpoint.
Our Custom Health Indicator
We can write a Custom Health Indicator which will define the health reported by the Spring Boot Actuator:
This Health Indicator makes use of an Autowired Spring Boot Bean which the Indicator shares with a Controller. In particular, the Bean in question – HealthToggle – features a boolean which is set to false temporarily via a function for a configurable amount of seconds:
We can then hit a specific endpoint on the Controller which will trigger this toggle for an amount of time configured by the rendered application.yml and thus make the application reported by the Actuator as unhealthy for that specified period of time:
As we can see above, this endpoint also tells us how long the application will be unhealthy for, and the IPv4 address of the instance running the application, which will be useful when we experiment with this application when it is behind a Load Balancer. All of this is shown in the video walkthrough.
Showcasing the Application and Deploying to ShuttleOps
The relationship between the Chef Habitat components Spring Boot code shown above, and how the resulting application is deployed to ShuttleOps, is explained in more detail in the following video walkthrough:
With that, we now know how Chef Habitat can report the health defined by our Custom Health Indicator in Spring Boot. Furthermore, we now know how we can easily build and deploy this application to AWS behind a Load Balancer respecting the result of the health check hook!