从另一个Spring Boot Web应用程序对Spring Boot Web应用程序进行健康检查

6

我目前拥有一个Spring Boot应用程序,可以通过执行器访问健康检查。

这个应用程序依赖于另一个Spring Boot应用程序的可用性/上线,所以我的问题是:

通过覆盖第一个应用程序中的健康检查,是否有一种优雅的方式来对第二个应用程序进行健康检查?

本质上,我只想使用一个调用来获取两个应用程序的健康检查信息。

2个回答

16

通过实现 HealthIndicator 检查后端应用的健康状况,您可以开发自己的健康指标。因此,本质上这不会太困难,因为您可以直接使用开箱即用的 RestTemplate。

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

太完美了!这正是我在寻找的 - 谢谢。 - Lars Rosenqvist
优秀的响应 - TuGordoBello
能否提供您的解决方案,@LarsRosenqvist。建议的答案无效。谢谢。 - pixel

0
如果您在应用程序A中有一个控制器,那么您可以在控制器中引入一个GET方法请求,并将其指向应用程序B的健康检查API端点。通过这种方式,您将在应用程序A中拥有一个可用于检查应用程序B健康状况的API端点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接