Spring Health Indicator

sojw·2024년 9월 14일
0

종류

  • Health
  • Liveness
  • Readiness

Custom Indicator 구현

DynamoDB

Health

@Slf4j
@Component
public class DynamoDBHealthIndicator extends AbstractHealthIndicator {
    private final AmazonDynamoDB amazonDynamoDB;
    @Value("${dynamodb.table.name.prefixv2}")
    private String dynamoDBTablePrefix;
    @Value("${dynamodb.table.lists}")
    private List<String> dynamoDBTables;
    public DynamoDBHealthIndicator(AmazonDynamoDB amazonDynamoDB) {
        this.amazonDynamoDB = amazonDynamoDB;
    }
    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        try {
            if (CustomHealthIndicatorSupport.isReadyDynamoDB(amazonDynamoDB, dynamoDBTablePrefix, dynamoDBTables)) {
                builder.up().withDetail("dynamoDB tables", dynamoDBTables).build();
            }
        } catch (final Exception e) {
            log.error("", e);
            builder.down().withException(e).build();
        }
    }
}

Liveness

@Component
public class DynamoDBLivenessStateHealthIndicator extends LivenessStateHealthIndicator {
    private final AmazonDynamoDB amazonDynamoDB;
    @Value("${dynamodb.table.name.prefixv2}")
    private String dynamoDBTablePrefix;
    @Value("${dynamodb.table.lists}")
    private List<String> dynamoDBTables;
    public DynamoDBLivenessStateHealthIndicator(ApplicationAvailability availability, AmazonDynamoDB amazonDynamoDB) {
        super(availability);
        this.amazonDynamoDB = amazonDynamoDB;
    }
    @Override
    protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
        return CustomHealthIndicatorSupport.isReadyDynamoDB(amazonDynamoDB, dynamoDBTablePrefix, dynamoDBTables)
                ? LivenessState.CORRECT
                : LivenessState.BROKEN;
    }
}

Readiness

@Component
public class DynamoDBReadinessStateHealthIndicator extends ReadinessStateHealthIndicator {
    private final AmazonDynamoDB amazonDynamoDB;
    @Value("${dynamodb.table.name.prefixv2}")
    private String dynamoDBTablePrefix;
    @Value("${dynamodb.table.lists}")
    private List<String> dynamoDBTables;
    public DynamoDBReadinessStateHealthIndicator(ApplicationAvailability availability, AmazonDynamoDB amazonDynamoDB) {
        super(availability);
        this.amazonDynamoDB = amazonDynamoDB;
    }
    @Override
    protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
        return CustomHealthIndicatorSupport.isReadyDynamoDB(amazonDynamoDB, dynamoDBTablePrefix, dynamoDBTables)
                ? ReadinessState.ACCEPTING_TRAFFIC
                : ReadinessState.REFUSING_TRAFFIC;
    }
}

0개의 댓글