简体   繁体   English

AWS SQS 和 SNS 中的消息结构

[英]Message structure in AWS SQS and SNS

I have a SNS topic t1 that is subscribed into a SQS queue q1 .我有一个订阅到 SQS 队列q1的 SNS 主题t1 I have a NodeJS process p1 that publishes to t1 , process p2 that subscribes from q1 .我有一个发布到t1的 NodeJS 进程p1 ,从q1订阅的进程p2 I also have a process p3 that writes to q1 directly.我还有一个直接写入q1的进程p3

Suppose, data is populated when p2 reads from the queue.假设当p2从队列中读取data时填充数据。 Then, while the following snippet works with p1 as the writer to the queue, I get a JSON parser error with p3 .然后,虽然下面的代码片段使用p1作为队列的写入者,但我得到一个 JSON 解析器错误p3

for (var i = 0; i < data.Messages.length; i++) {
 var message = data.Messages[i];
 let messageBody = JSON.parse(message.Body)
 let payload = JSON.parse(messageBody.Message)

The only way I could process data from p3 working is to not have JSON.parse(messageBody.Message) and process messageBody directly.我可以处理来自p3工作的数据的唯一方法是没有JSON.parse(messageBody.Message)并直接处理messageBody Thus, it seems that, the message structure as received in a queue is different if the writer was a SNS topic subscription or a SQS writer.因此,如果写入者是 SNS 主题订阅或 SQS 写入者,队列中接收到的消息结构似乎是不同的。

Can you please advise if I can have a single NodeJS application that can process data from SQS regardless of what wrote into that queue?您能否告知我是否可以拥有一个可以处理来自 SQS 的数据的 NodeJS 应用程序,而不管该队列中写入了什么?

Not 100% sure if this will solve all your issues, but as a first step at least you probably want to do enable 'Raw Message Delivery'不是 100% 确定这是否会解决您的所有问题,但作为第一步,至少您可能想要启用“原始消息传递”

https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html https://docs.aws.amazon.com/sns/latest/dg/sns-large-payload-raw-message-delivery.html

In this way, the messages that SNS puts in the queue won't have any additional properties added, and thus should match messages that are put in directly.这样,SNS 放入队列的消息不会添加任何额外的属性,因此应该匹配直接放入的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM