当我尝试从两个不同的终端选项卡轮询相同的 Amazon SQS 时,我在这两个选项卡中都没有收到相同的消息

When I am trying to poll the same Amazon SQS from two different terminal tabs, I am not getting same message in both of them

我创建了一个 Amazon SNS 主题。我有一个 Amazon SQS 队列订阅了该主题。

我已经创建了一个默认的 SQS 队列(不是 FIFO 队列)。

我正在使用 sqs-consumer API 对 SQS 队列进行长轮询。

const app = Consumer.create({
    queueUrl: 'https://sqs.us-east-2.amazonaws.com/xxxxxxxxxxxx/xxxxxxxxxxx',
    handleMessage: async (message) => {

        console.log(message);
    },
    sqs: sqs//new AWS.SQS({apiVersion: '2012-11-05'})
});

app.on('error', (err) => {
    console.error(err.message);
});

app.on('processing_error', (err) => {
    console.error(err.message);
});

app.on('timeout_error', (err) => {
    console.error(err.message);
});

app.start();

当我通过 node sqs_client.js 从单个终端 运行ning 这段 js 文件时,一切都运行良好,消息按正确顺序发送。

但是,如果打开另一个终端 window 和 运行 node sqs_client.js ,那么传入消息的顺序就会变得非常随机。较新的消息可能以任何顺序进入第一个终端 window 或第二个终端 window。

为什么会这样?有什么办法可以防止这种情况发生,以便我可以同时在两个终端 windows 中收到相同的消息。

SQS 产品页面位于:https://aws.amazon.com/sqs/faqs/

问:Amazon SQS 是否提供消息排序?

Yes. FIFO (first-in-first-out) queues preserve the exact order in which messages are sent and received. If you use a FIFO queue, you don't have to place sequencing information in your messages. For more information, see FIFO Queue Logic in the Amazon SQS Developer Guide.

Standard queues provide a loose-FIFO capability that attempts to preserve the order of messages. However, because standard queues are designed to be massively scalable using a highly distributed architecture, receiving messages in the exact order they are sent is not guaranteed.

您是否正在使用标准队列并因此使用松散 FIFO?

你问:"Is there any way...that I can get the same message in both the terminal windows at the same time."

这不是 Amazon SQS 的运行方式。 Amazon SQS的大体流程是:

  • 消息被发送到队列
  • 消息在队列中最多保留 14 天(可以延长)
  • 消费者调用 ReceiveMessages(),一次最多请求 10 条消息
  • 收到消息后,将其标记为不可见
  • 当消费者处理完消息后,消费者调用DeleteMessage()从队列中移除消息
  • 如果消费者在隐形超时期限没有调用DeleteMessage(),消息将重新出现 在队列中,消费者可以接收

因此,消息有意一次只提供给一个消费者。消息一旦被抓取,其他消费者就无法接收。

如果您的要求是让两个消费者接收相同的消息,那么您将需要重新设计您的架构。您没有提供足够的详细信息来推荐特定方法,但选项包括使用多个 Amazon SQS 队列或直接通过 Amazon SNS 而不是 Amazon SQS 发送消息。