Search code examples
spring-boot-actuatorindicator

How can I invoke the health check of another Spring Boot application from within my Spring Boot health endpoint?


I wonder how I can invoke a custom health indicator:

  1. in the same application
  2. of another Spring Boot application

My application is split into a base application (rather a configuration) A which implements nearly all the functionality (having no main method) and another application B (having a main method ;-) ) having the base configuration as a dependency in the POM.

In A I have implemented a custom HealthIndicator:

@Component
@RequiredArgsConstructor
public class AdapterDownstreamHealthIndicator implements HealthIndicator {

    private RestTemplate restTemplate;
    private String downStreamUrl = "http://localhost:8081/actuator";

    public AdapterDownstreamHealthIndicator(RestTemplate restTemplate, String downStreamUrl) {
        this.restTemplate = restTemplate;
        this.downStreamUrl = downStreamUrl;
    }

    @Override
    public Health health() {
//        try {
//            JsonNode resp = restTemplate.getForObject(downStreamUrl + "/health", JsonNode.class);
//            if (resp.get("status").asText().equalsIgnoreCase("UP")) {
//                System.out.println("JUHUUUUUUUUUUU!!!!");
//                return Health.up().build();
//            }
//        } catch (Exception ex) {
//            return Health.down(ex).build();
//        }
        return Health.down().build();
    }
}

In my application.properties I have some actuator properties:

management.endpoints.web.exposure.include=health,info,prometheus,adapterDownstream
spring.jackson.serialization.INDENT_OUTPUT=true
management.endpoint.health.show-details=always

When I enter http://localhost:9091/actuator/health/adapterDownstream in a browser the debugger does not stop in the health() method and I simply get an empty page displayed.

I already tried to extend AbstractHealthIndicator instead of implementing HealthIndicator interface.

What am I doing wrong that the custom health indicator is not recognized?

In the end I want to make some kind of deep health check to test all components being used in my application. Maybe using CompositeHealthContributor should be used???


Solution

  • As I described I have a dependency A which has NO main method which is loaded into my application B as a dependency in the POM. So far I tried to implement the custom healthcheck class/the health indicator in this dependency/module A.

    The simple solution is to add a @ComponentScan(basePackages = "path.to.actuator") to the main method of the application.