Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 614
Lars Rosenqvist Avatar asked Dec 10 '22 16:12

Lars Rosenqvist


1 Answers

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();
    }
}
like image 140
daniel.eichten Avatar answered Dec 28 '22 07:12

daniel.eichten