使用 Amazon SQS 和 Amazon SNS 发送广播事件通知
在本教程中,您将使用 Amazon Simple Notification Service (SNS) 和 Amazon Simple Queue Service (SQS) 实现广播消息传递场景。在此场景中,消息会被“推送”给多个订阅者,从而无需定期检查或轮询更新,订阅者也可以并行异步处理消息。
![](https://d1.awsstatic.com/xuefezha-jennie/cost-icon.84b1b7bbeb5d58956ebf11a05ed8152992f762ba.png)
可通过免费套餐试用
![](https://d1.awsstatic.com/xuefezha-jennie/Group%20286.f8adeba47a39b3d6d21f8cbb0b6a751582e55927.png)
在 AWS 上发送广播通知需要一个 AWS 账户,快速创建免费帐户 »
AWS 免费套餐提供 1000000 次 Amazon Simple Notification Service 发布和 1000000 次 Amazon Simple Queue Service 请求。
![](https://d1.awsstatic.com/guoheng/product.2d7b328b4c088795e2ac7b9c03e03d54eb5ea73f.png)
简介
在本教程中,您将使用 Amazon Simple Notification Service (SNS) 和 Amazon Simple Queue Service (SQS) 实现广播消息传递场景。在此场景中,消息会被“推送”给多个订阅者,从而无需定期检查或轮询更新,订阅者也可以并行异步处理消息。
为了说明这一点,我们假设您正在开发一个云原生应用程序,每当在在网上商店下订单时,该应用程序都会向某个主题发送 Amazon SNS 消息。订阅该主题的 Amazon SQS 队列都会收到新订单的相同通知。
您在本教程中使用的 AWS 服务适用于 AWS 免费套餐。
操作步骤
进入 Amazon SNS 控制台
点击此处时,AWS 管理控制台将在新的浏览器窗口中打开,使本分步指南保持打开状态,以便一边操作一边参照本教程。页面加载后,输入您的用户名和密码即可开始使用。然后在搜索栏中键入 notification,并选择 Simple Notification Service 以打开该服务控制台。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_1a.3098f767fab306d00a598040a7e279b8979a7eb8.png)
a. 如果显示 SNS 控制台登录页面,请点击 Next step(下一步)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/1a-sns-console-home.1fe76d391e896ba06177eb07cb0856f30be5044f.png)
创建 Amazon SNS 主题
在此步骤中,您将创建一个 Amazon SNS 主题。主题是发送消息和订阅通知的通信渠道。在本示例中,每当网上商店收到新订单时,示例电子商务应用程序就会向 Amazon SNS 主题推送消息。
a. 在 Create topic(创建主题)页面的主题名称框中键入 New-Orders,然后点击 Create topic(创建主题)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_2a.0e07b219a04579c03fb8b32c8aea8f318a937c5e.png)
b. Topic details(主题详细信息)页面会确认该主题已成功创建。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/2b-topic-create-confirm.af88fe7eb145627b54f9cbe56ee233095525146b.png)
创建 Amazon SQS 队列
现在,您已使用 Amazon SNS 创建了主题,接下来,您将创建订阅该主题的 Amazon SQS 队列。
当您向某个主题订阅多个队列时,每次向该主题推送消息时,每个队列都会收到相同的通知。然后,与这些队列关联的服务可以异步且并行地处理订单。
例如,连接到其中一个队列的 Amazon EC2 服务器实例可以负责订单的处理或执行,而另一个服务器实例可能连接到数据仓库以分析收到的所有订单。
方便起见,在本教程中我们不会将 EC2 实例附加到队列。
a. 点击此处在新的浏览器窗口中打开 Amazon SQS 控制台。如果此时显示 SQS 登录页面,请点击 Get Started Now(立即开始)。如果没有,继续进行下一步。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_3a.6c736e5eafb080d1f3425f960e90c549aa12f1c3.png)
b. 我们的第一个队列将用于存储一个虚构的库存服务的订单,该服务负责跟踪产品,并根据订单需要从库存中添加或删除产品。
在 Create New Queue(创建新队列)页面上的 Queue Name(队列名称)字段中输入 Orders-for-Inventory。使 Standard Queue(标准队列)保持选定状态,并点击 Quick-Create Queue(快速创建队列)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_3b.7ef3f6951a847d4d01ad7decc904e549f154f0a3.png)
c. 新队列已创建完毕,并在队列列表中处于选定状态。接下来,您将创建另一个队列来处理订单分析。
点击 Create New Queue(创建新队列),创建另一个队列来为分析服务存储订单。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_3c.d439c9d42e2d4619c73a571564cf14442596960b.png)
d. 在 Queue Name(队列名称)字段中输入 Orders-for-Analytics,然后点击 Quick-Create Queue(快速创建队列)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_3d.e8a632498022505eb99ceb9d7c9189dcdcb4a0a3.png)
e. 新建的队列此时将出现在队列列表中。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_3e.89d640ffece3b300b6077eeae034f5173be83c6f.png)
为队列订阅主题
现在您已经创建了两个 Amazon SQS 队列,您需要为它们订阅 Amazon SNS 主题,以广播新订单的通知。
a. 从队列列表中,选择 Orders-for-Inventory 和 Orders-for-Analytics 队列。从 Queue Actions(队列操作)中,选择 Subscribe Queues to SNS Topic(为队列订阅 SNS 主题)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_4a.f7e028563f456213a314c61deb3263ffd5c2b26a.png)
b. 系统会显示 Subscribe to a Topic(订阅主题)对话框。从 Choose a Topic(选择主题)下拉列表中,选择 New-Orders Amazon SNS 主题。
您的 SNS 主题之所以会显示在列表中,是由于您使用相同的账户创建了 SNS 主题和 Amazon SQS 队列。如果 SNS 主题是用另一个账户创建的,您可以使用 Topic ARN(主题 ARN)来订阅该主题。有关更多详细信息,请参阅 Amazon SNS 文档。
保持 Topic Region(主题区域)配置不变,然后点击 Subscribe(订阅)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_4b.1751d9b45c6b1b761810956b19ab9e80262b1a9e.png)
c. 此时将显示 Topic Subscription Result(主题订阅结果)对话框。点击 OK(确定)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_4c.90ab180443acf9258bc5bfad425f46af3f0d6b4d.png)
向主题发布消息
您的队列现在已订阅了主题。在这一步中,您将模拟一个新订单,让虚构的电子商务应用程序向主题推送一条包含订单详情的消息。
a. 在 Amazon SNS 控制台的 New Orders 主题详情页面中,点击 Publish message(发布消息)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/5a-topic-create-confirm.6b64870415cba8ce650084f212cd9b9ee0e5f45b.png)
b. 此时将显示 Publish Message to topic(向主题发布消息)页面。在 Subject(主题)框中,键入订单号 123-4567890-1234567。在 Message(消息)字段中,输入以下表示示例订单的文本:
1 x Widget @ $29.99 USD
2 x Widget Cables @ $4.99
点击 Publish Message(发布消息)。此时将显示确认对话框。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/5b-sns-send-message-wf.7b614e5d1ab2ee6904d7f84b2c7b4d21f5846806.png)
验证订阅
新消息发布后,Amazon SNS 会将该消息传递给订阅了该主题的每个端点。在类似这种广播场景中,Amazon SQS 队列就是端点。
在此步骤中,您将通过查看主题发送到队列的消息来确认该队列是否收到了新订单通知。
a. 在 Amazon SQS 控制台中,从队列列表中选中 Orders-for-Inventory 队列的复选框。从 Queue Action(队列操作)下拉菜单中,选择 View/Delete Messages(查看/删除消息)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6a.0ac33c6e4da94666ab24aa777918861cc75987d8.png)
b. 点击 Start Polling for Messages(开始轮询消息)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6b.c052fa043d5fecb2138d935ccc52c575320aba67.png)
c. 此时将显示 View/Delete Messages in Orders-for-Inventory(查看/删除 Orders-for-Inventory 中的消息)对话框。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6c.8ee35209a31f46670e2caf92829381cb30ca3a2d.png)
d. 在 Body(正文)列中,点击 More Details(更多详情)。Message Details(消息详细信息)框包含一个 JSON 文档,其中包含您发布到相应主题的主题和消息。
您已确认 Orders-for-Inventory 队列收到了来自 New-Orders主题的新订单通知。
点击 Close(关闭)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6d.a2161a233773ff9665adc3a95ac70600842b0911.png)
e. 我们假定虚构的库存服务已经处理完这条信息,现在可以安全地从队列中删除这条信息。
点击 Delete 1 Message(删除 1 条消息)。然后点击 Yes, Delete Checked Messages(是,删除已选中的消息)确认删除。然后点击 Close(关闭)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6e.690b347f665a2455f47255239e7668670477f889.png)
f. 重复步骤 6a 至 6e,确认 Orders-for-Analytics 队列也收到了新订单的通知。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_6f.951483d65d62e6e2694aa8f68043db4e16f941d1.png)
删除资源
在此步骤中,您将删除在学习本教程的过程中创建的资源(包括主题订阅、主题和队列)。删除不再使用的资源可以避免继续产生费用,属于一种最佳实践。
a. 打开 Amazon SNS 控制台,并点击左侧导航栏中的 Topics(主题)。
选择 New-Orders 主题。
点击 Delete(删除)删除主题。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_7a.6ebb768c8a4bf5c3a5d35f9a186dd8d91f7dc54a.png)
b. 此时将显示 Delete(删除)确认对话框。在该对话框中键入 delete me,然后点击 Delete(删除)。该主题及其订阅将被删除。现在可以关闭 SNS 浏览器窗口(但不要退出登录,因为您还需要在 SQS 控制台中删除队列)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_7b.43dcc9bb0ca932ad45ecebffabde6d458ab1d2ca.png)
c. 在 Amazon SQS 控制台中,选择 Orders-for-Inventory和 Orders-for-Analytics队列。从 Queue Actions(队列操作)中,点击 Delete Queues(删除队列)。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_7c.2ee6542d41dc8afd47acda878a1ebf65aefcbf53.png)
d. 此时将显示 Delete Queues(删除队列)对话框。点击 Yes, Delete 2 Queues(是,删除 2 个队列)。这些队列将被删除。
您现在可以退出 Amazon SQS 控制台了。
![](https://d1.awsstatic.com/tmt/tmt_send-fanout-event-notifications/tmt_send-fanout-event-notifications_7d.56680b4d4956c9ae390f04627903e4ab412c4a44.png)
恭喜您!
您已经使用 Amazon SNS 和 Amazon SQS 实现了一个广播场景。现在,您可以将 Amazon SNS 和 Amazon SQS 结合使用,向需要即时事件通知的应用程序发送消息,或将消息缓存在 Amazon SQS 队列中,供其他应用程序稍后处理。