Lars Rosenqvist
Lars Rosenqvist

Reputation: 431

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.

Upvotes: 6

Views: 8803

Answers (2)

daniel.eichten
daniel.eichten

Reputation: 2555

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();
    }
}

Upvotes: 16

Srikanta
Srikanta

Reputation: 1147

If you have a controller in the App A, then you can introduce a GET method request in the controller and point it to the App B health check API endpoint. In this way, you will have an API endpoint available in App A to check App B's health as well.

Upvotes: 0

Related Questions