在这个例子中,AWS SNS的通知将被转发到AWS SQS并从那里进行处理。我们将在3次重试后将消息从AWS SQS源队列转移到DLQ。如果消息由于某种原因没有被处理,就会发生重试。每次尝试之间,消息将每隔30秒可见一次。
设置
// Create SQS queue
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs create-queue \
--queue-name test-queue.dlq
// Create SQS DLQ queue
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs create-queue \
--queue-name test-queue \
--attributes '{"RedrivePolicy":"{\"deadLetterTargetArn\":\"arn:aws:sqs:eu-west-1:000000000000:test-queue.dlq\",\"maxReceiveCount\":\"3\"}"}'
// Create SNS topic
$ aws --profile localstack --endpoint-url http://localhost:4566 sns create-topic \
--name test-topic
// Subscribe SQS queue to SNS topic
$ aws --profile localstack --endpoint-url http://localhost:4566 sns subscribe \
--protocol sqs \
--topic-arn arn:aws:sns:eu-west-1:000000000000:test-topic \
--notification-endpoint arn:aws:sqs:eu-west-1:000000000000:test-queue
测试
$ aws --profile localstack --endpoint-url http://localhost:4566 sns publish \
--topic-arn arn:aws:sns:eu-west-1:000000000000:test-topic \
--message 'Hello'
尝试消耗消息3次:
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
--queue-url http://localhost:4566/000000000000/test-queue \
--attribute-names All --message-attribute-names All
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
--queue-url http://localhost:4566/000000000000/test-queue \
--attribute-names All --message-attribute-names All
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
--queue-url http://localhost:4566/000000000000/test-queue \
--attribute-names All --message-attribute-names All
消息现在应该在DLQ中:
$ aws --profile localstack --endpoint-url http://localhost:4566 sqs receive-message \
--queue-url http://localhost:4566/000000000000/test-queue.dlq \
--attribute-names All --message-attribute-names All