Spring Boot运行状况检查-SQS使用者 [英] Spring Boot Health Check - SQS Consumer
问题描述
是否有用于SQS的Spring Boot Actuator运行状况检查端点?我已经建立了一个SQS使用者,我想检查SQS是否已启动并正在运行.我不是使用JMSlistener连接到SQS,而是使用Spring Cloud Libraries.
Is there a Spring Boot Actuator Health Check endpoint for SQS? I have built a SQS consumer and I want to check if SQS is up and running. I am not using JMSlistener for connecting to SQS but rather using Spring Cloud Libraries.
我实现了以下运行状况检查端点.当我删除队列并尝试运行状况检查终结点时,这将返回以下错误.如果存在连接问题或SQS服务出现故障,我是否会收到类似的错误,最终将导致运行状况检查端点失败?
I implemented the below health check endpoint. This returns the below error when I delete the queue and try to hit the health check endpoint. If there is a connectivity issue or if the SQS service goes down , will I be getting a similar error which will eventually cause the health check endpoint to fail?
com.amazonaws.services.sqs.model.QueueDoesNotExistException:此wsdl版本不存在指定的队列.(服务:AmazonSQS;状态码:400;错误代码:AWS.SimpleQueueService.NonExistentQueue;要求编号:cd8e205d-dc43-535e-931f-7332733bd16c)
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
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);
}
推荐答案
您必须编写如下的自定义运行状况检查,才能通过调用 AWS Java SDK库.
You have to write a custom health check like below to check your queue exists or not by calling getQueueUrl using 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
}
}
这篇关于Spring Boot运行状况检查-SQS使用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!