gpt4 book ai didi

java - Spring Boot 健康检查 - SQS Consumer

转载 作者:行者123 更新时间:2023-11-30 02:10:23 28 4
gpt4 key购买 nike

是否有适用于 SQS 的 Spring Boot Actuator 运行状况检查端点?我已经构建了一个 SQS 使用者,我想检查 SQS 是否已启动并运行。我没有使用 JMSlistener 连接到 SQS,而是使用 Spring Cloud Libraries。

我实现了以下运行状况检查端点。当我删除队列并尝试访问运行状况检查端点时,这会返回以下错误。如果存在连接问题或者 SQS 服务出现故障,我是否会收到类似的错误,最终导致运行状况检查端点失败?

com.amazonaws.services.sqs.model.QueueDoesNotExistException: The specified queue does not exist for this wsdl version. (Service: AmazonSQS; Status Code: 400; Error Code: AWS.SimpleQueueService.NonExistentQueue; Request ID: cd8e205d-dc43-535e-931f-7332733bd16c)

public class SqsQueueHealthIndicator extends AbstractHealthIndicator {

private final AmazonSQSAsync amazonSQSAsync;
private final String queueName;

public SqsQueueHealthIndicator(AmazonSQSAsync amazonSQSAsync, String queueName) {
this.amazonSQSAsync = amazonSQSAsync;
this.queueName = queueName;
}

@Override
protected void doHealthCheck(Health.Builder builder) {
try {
amazonSQSAsync.getQueueUrl(queueName);
builder.up();
} catch (QueueDoesNotExistException e) {
e.printStackTrace();
builder.down(e);
}
}

}

bean 类

@Bean
SqsQueueHealthIndicator queueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${sqs.queueName}") String queueName) {
return new SqsQueueHealthIndicator(amazonSQSAsync, queueName);
}

@Bean
SqsQueueHealthIndicator deadLetterQueueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${sqs.dlQueueName}") String deadLetterQueueName) {
return new SqsQueueHealthIndicator(amazonSQSAsync, deadLetterQueueName);
}

最佳答案

您必须编写如下所示的自定义运行状况检查,以通过调用getQueueUrl来检查您的队列是否存在。使用AWS Java SDK lib .

    @Component
public class SQSHealthCheck 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();
}

public int check() {
/**
your logic to check queue exists or not using by calling getQueueUrl . e.g you will get queue url of a queue named "SampleQueue" like https://sqs.us-east-1.amazonaws.com/12XXX56789XXXX/SampleQueue
**/
return 0; // 0 or 1 based on result
}
}

关于java - Spring Boot 健康检查 - SQS Consumer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50283239/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com