Add Health Checks For External Services
Our starter service includes Spring Boot Actuator, which exposes an endpoint, /health, to check service availability.
Actuator uses health checks as a way of ensuring that a service’s components are up and available to be used. Your generated service came with an example health check class, ExampleCompontentHealthCheckConfig.
Since we have added two dependent services, we will create dedicated health indicators for each.
-
Rename the provided example class to
MobileMviServiceHealthCheckConfig.java. -
Update the example HealthIndicator to account for
mobile-mvi-serviceavailability. For our purposes, we will considermobile-mvi-serviceto be healthy if its swagger contract can be retrieved by hitting the service URL.
Your implementation should look similar to below:
package gov.va.mobile.starter.v1.service.health;
import gov.va.mobile.service.client.http.rest.RestHealthIndicatorBuilder;
import gov.va.mobile.starter.v1.service.AppProperties;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link RestHealthIndicatorBuilder} implementation for verifying the health of an external component.
*
* @see RestHealthIndicatorBuilder
* @since 1.0
*/
@Configuration
public class MobileMviServiceHealthCheckConfig {
@Bean("mobile-mvi-service")
public HealthIndicator mobileMviServiceHealthIndicator(final RestHealthIndicatorBuilder builder,
final AppProperties properties) {
return builder.url(properties.getMobileMviSvcUrl()).build();
}
}
-
Following the example above, add a health indicator class for
user-session-servicecalledUserSessionServiceHealthCheckConfig.
Add Service Health Checks to ActuatorITCase
Your generated service also includes a test class called ActuatorITCase, an implementation of AbstractHealthCheckITCase from the mobile-service-test library dependency added earlier. This test will verify a service’s availability through its /health Actuator endpoint. Now that you have added health checks for Mobile MVI and User Session services, you can add them to this test class so that their availability can be considered as part of your service’s health.
Update the healthCheckJsonPaths() method of ActuatorITCase to include the following dependent services as part of the test of the service health check:
-
"$.components.mobile-mvi-service.status"
-
"$.components.user-session-service.status"
Build your service with mvn clean install -Pwith-skaffold and verify that the ActuatorITCase test class executes successfully during the integration test phase.