Search code examples
restspring-bootspring-boot-actuator

Health check on Spring Boot webapp from another Spring Boot webapp


I currently have a Spring Boot app where I can access the health check via actuator.

This app is dependent on another Spring Boot App being available/up so my question is:

By overriding the health check in the first app, is there an elegant way to do a health check on the second app?

In essence I just want to use one call and get health-check-info for both applications.


Solution

  • You can develop an own health indicator by implementing HealthIndicator that checks the health of the backend app. So in essence that will not be too difficult, cause you can just use the RestTemplate you get out of the box, e.g.

    public class DownstreamHealthIndicator implements HealthIndicator {
    
        private RestTemplate restTemplate;
        private String downStreamUrl;
    
        @Autowired
        public DownstreamHealthIndicator(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")) {
                    return Health.up().build();
                } 
            } catch (Exception ex) {
                return Health.down(ex).build();
            }
            return Health.down().build();
        }
    }