使用AWS SQS重试后将消息从源队列转移到DLQ的指南

286 阅读1分钟

在这个例子中,我们将在3次重试后将消息从AWS SQS源队列转移到DLQ。如果消息由于某种原因没有被处理,就会发生重试。每次尝试之间,消息将每隔30秒可见一次。

设置

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs create-queue \
    --queue-name work-queue.dlq
    
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs create-queue \
    --queue-name work-queue \
    --attributes '{"RedrivePolicy":"{\"deadLetterTargetArn\":\"arn:aws:sqs:eu-west-1:000000000000:work-queue.dlq\",\"maxReceiveCount\":\"3\"}"}'

测试

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs send-message \
    --queue-url http://localhost:4566/000000000000/work-queue \
    --message-body '{"key":"val"}'

尝试消耗消息3次:

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
    --queue-url http://localhost:4566/000000000000/work-queue \
    --attribute-names All --message-attribute-names All --max-number-of-messages 10

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
    --queue-url http://localhost:4566/000000000000/work-queue \
    --attribute-names All --message-attribute-names All --max-number-of-messages 10

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
    --queue-url http://localhost:4566/000000000000/work-queue \
    --attribute-names All --message-attribute-names All --max-number-of-messages 10

消息现在应该在DLQ中:

$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
    --queue-url http://localhost:4566/000000000000/work-queue.dlq \
    --attribute-names All --message-attribute-names All --max-number-of-messages 10