You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Actuator SQS健康检查实现及异常场景咨询

Answer to Your SQS Health Check Question

Great question! Let's break this down clearly for you:

Core Answer

Yes, network connectivity issues or SQS service outages will cause your health check endpoint to mark as failed—but your current implementation needs a small adjustment to properly handle these scenarios.

What's Missing in Your Current Code

Right now, your doHealthCheck method only catches QueueDoesNotExistException. While this works for detecting missing queues, it doesn't account for other critical failures:

  • Network problems: If your app can't reach AWS SQS (e.g., DNS failures, firewall blocks, connection timeouts), the AWS SDK will throw a SdkClientException (a wrapper for lower-level network errors).
  • SQS service outages: If SQS itself is down or unresponsive, you'll get an AmazonServiceException (e.g., 5xx status codes like ServiceUnavailable).

These exceptions won't be caught by your current code, which means they'll bubble up and cause Actuator to mark the health indicator as DOWN—but you won't get clean, actionable error details in the health response.

Improved Health Check Implementation

Update your SqsQueueHealthIndicator to catch these broader exception types, so you can explicitly mark the health as DOWN and provide useful context for debugging:

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 {
            String queueUrl = amazonSQSAsync.getQueueUrl(queueName);
            builder.up()
                  .withDetail("queueName", queueName)
                  .withDetail("queueUrl", queueUrl)
                  .withDetail("status", "Queue is reachable");
        } catch (QueueDoesNotExistException e) {
            builder.down(e)
                  .withDetail("queueName", queueName)
                  .withDetail("error", "Specified queue does not exist");
        } catch (SdkClientException e) {
            builder.down(e)
                  .withDetail("queueName", queueName)
                  .withDetail("error", "Network connection failure or cannot reach SQS service");
        } catch (AmazonServiceException e) {
            builder.down(e)
                  .withDetail("queueName", queueName)
                  .withDetail("errorCode", e.getErrorCode())
                  .withDetail("error", "SQS service returned an error: " + e.getMessage());
        } catch (Exception e) {
            // Catch-all for unexpected errors
            builder.down(e)
                  .withDetail("queueName", queueName)
                  .withDetail("error", "Unexpected error checking queue health");
        }
    }
}

How This Works

  • For network issues: The SdkClientException catch block will trigger, marking the health as DOWN with a clear message about connectivity problems.
  • For SQS outages: AmazonServiceException will capture service-side errors, including the AWS error code for easier troubleshooting.
  • You also get added context in the health response (like queue URL when healthy, error codes when failing) which is invaluable for ops teams.

Quick Note on Spring Boot Actuator Behavior

Even if you don't catch these exceptions, Actuator will still mark the health indicator as DOWN if an uncaught exception is thrown from doHealthCheck. But explicitly handling them gives you control over the error details and ensures consistent, readable health responses.

内容的提问来源于stack exchange,提问作者Punter Vicky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:12:32