我目前有一个 Spring Boot 应用程序,可以通过执行器访问运行状况检查。
这个应用程序依赖于另一个可用的 Spring Boot 应用程序,所以我的问题是:
通过覆盖第一个应用程序中的运行状况检查,是否有一种优雅的方式对第二个应用程序进行运行状况检查?
本质上,我只想使用一次调用并获取两个应用程序的运行状况检查信息。
最佳答案
您可以通过实现检查后端应用运行状况的 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();
}
}
关于rest - 从另一个 Spring Boot Web 应用程序对 Spring Boot Web 应用程序进行健康检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37654130/