首页 > 解决方案 > Spring boot 2 和 AWS DynamoDB 健康端点

问题描述

我正在使用带有 DynamoDB 的 Spring Boot 2。是否可以在 Spring Boot 执行器 /health 端点上公开 dynamoDB 的健康检查?

最近我遇到了我的应用程序无法连接到 DynamoDB 的情况(底层连接 HTTP 池异常)。

标签: spring-bootamazon-dynamodb

解决方案


您应该能够通过定义自定义执行器运行状况指示器来执行此操作,您可以在其中执行 dynamoDb 操作,例如 ListTables。此处记录了自定义指标,此处记录了ListTables 。你应该最终得到类似的东西:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

@Override
public Health health() {
    int errorCode = check(); // perform some specific health check
    if (errorCode != 0) {
        return Health.down().withDetail("Error Code", errorCode).build();
    }
    return Health.up().build();
}

}

private int check(){
  try{
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    ListTablesRequest request = new ListTablesRequest();
    ListTablesResult response = client.listTables(request);
    return 0;
  }catch (Exception e){
     //log exception?
     return -1;
  }
}

推荐阅读