Spring Boot 2和AWS DynamoDB运行状况终端节点

时间:2019-01-03 09:17:42

标签: spring-boot amazon-dynamodb

我正在将Spring Boot 2与DynamoDB一起使用。是否可以在Spring Boot执行器/ health端点上公开dynamoDB的运行状况检查?

最近我遇到了我的应用程序无法连接到DynamoDB(基础连接HTTP池例外)的情况。

1 个答案:

答案 0 :(得分:1)

您应该能够通过定义自定义执行器运行状况指示器来执行此操作,在该指示器中执行dynamoDb操作(如ListTables)。 自定义指标记录在here和ListTables here中。 您应该最终得到以下内容:

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