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

标签 rest spring-boot spring-boot-actuator

我目前有一个 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/

相关文章:

java - Spring @RequestBody 映射 POJO 时导致 HTTP 405 Method Not allowed

java - 如何使用 Spring Rest 服务/Spring Boot 下载 Excel

json - Springboot 与 Sybase 服务器 - 无法加载驱动程序类 : com. sybase.jdbc4.jdbc.SybDriver

spring - spring boot 微服务如何生成集中式日志?

php - yii2 REST API 调整

java - 使用 Log4j2 2.11 的微秒级打印分辨率

java - Spring Boot 使用调度程序关闭

spring - 当management.port=0时,在运行时获取Spring Boot管理端口

java - Spring Boot 生产监控

python - Django REST Framework 3.0 - NOT NULL 约束失败 :