0

我有一个涉及 的应用程序AWS SNSSQS需要在多个主机上运行。确切的问题描述是:

每当发生事件时,都会将包含的消息ID发布到订阅了 SQS 队列的 SNS 主题。现在我在队列中收到消息。现在,我希望多个主机从队列中读取消息(没有两个主机应该读取相同的消息)并将消息写入 Amazon S3 中的一个公共文件。应该考虑诸如“如果主机读取消息失败怎么办”和“不在同一主机或不同主机中两次读取同一消息”等问题。

谁能建议一些方法或一些参考资料,我可以通过这些方法或参考资料来完成这项任务?

4

2 回答 2

2

听起来您想要的很大程度上是默认情况下 SQS 的行为方式。当您的一个主机读取消息时,访问队列的其他人将无法看到该消息,直至消息可见性超时。您可以进行 api 调用以延长该超时(即一种心跳)。

您还可以配置死信队列。这样,在消息被接收到一定次数后,它就会被移动到一个单独的队列中,以进行检查或以其他方式进行处理。

这记录在这里

于 2016-05-06T10:55:10.240 回答
0

溶液四处散落。您可以通读SQS 死信队列设置并参考我的示例。您无需编码即可使用 AWS SQS 控制台执行完全相同的操作。

import boto3
sqs = boto3.client("sqs")
# I want to "lock" my queue for 5 minutes to allow my process have time to 
# complete the task and delete the message afterwards.
response = sqs.create_queue(
    QueueName="foo",
    Attributes= {
        "VisibilityTimeout" : "300"
    }
)
# create a queue to store the "dead letter message"
dlq_response = sqs.create_queue(
    QueueName="dlq-foo",
    Attributes= {
        "VisibilityTimeout" : "300"
    }
)
queue_url = response["QueueUrl"]

# Attach RedrivePolicy to drive message to dead letter queue 
# I want to make sure the message only read 1 time. Assume the program crash
# if it is not deleted.
# deadLetterTargetArn : You must specify the queue exact region name, 
# exact Account name(replace 1234567890) and your dead letter queue name dlq-foo
sqs.set_queue_attributes(
    QueueUrl = queue_url,
    Attributes = {
        "RedrivePolicy" : """{
                "maxReceiveCount" : "1" ,
                "deadLetterTargetArn" : "arn:aws:sqs:<region-name>:1234567890:dlq-foo"
            }"""
        }
    )

注意:RedrivePolicy 只访问文字字符串,而不是字典。但是,正如文档指出的那样,您需要将“字典之类”的值放在那里并将其格式化为字符串。您可以使用 str(dict()) 将 dict 转换为 string ,我使用 python 三引号引号来阅读清晰。

于 2016-05-06T12:33:39.700 回答