跳到主要内容

第四步:尝试发布-订阅/广播消息交换模式

您现在已经完成了第三步:设置势度Saydo Try-Me CLI工具,并了解了Solly's Coffee的相关信息。


在第四步中,您将使用势度Saydo Try-Me CLI工具更深入地了解Solly's Coffee订单管理系统(OMS),并设置一个发布-订阅消息交换,将事件消息广播给多个订阅者。

在发布-订阅消息传递中,生产者(发布者)发送的消息可以被不同的消费者(订阅者)多次处理。每个消费者都接收消息的副本以进行处理,如下图所示:

img

在我们第三步中介绍的Solly's Coffee OMS中,涉及OM-Order Details事件的新订单传播事件流使用发布-订阅消息交换。订单详情事件由订单管理应用发布,并由厨房楼层和咖啡师站应用接收。

设置发布-订阅消息交换

在这个练习中,我们将设置四个应用,三个订阅者和一个发布者,并发布一个事件。订阅应用是咖啡师站、厨房楼层和人力资源。发布者是订单管理。完成这些步骤后,您将拥有如下动画所示的消息交换模式:

img

要设置您的应用并发布事件,请执行以下步骤:

  1. 打开四个命令行终端窗口,每个应用一个。
  2. 在终端1中,订阅咖啡师站应用接收来自国家ca的所有事件,无论来源类型。输入以下命令:
stm receive --topic "coffeeshop/order/details/v1/*/ca/>" --client-name "Barista Station" --output-mode full

您应该看到以下结果:

ℹ  info      info: loading 'receive' command from configuration 'stm-cli-config.json'
… awaiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******]
ℹ info info: client name: Barista Station
✔ success success: === Barista Station successfully connected and ready to receive events. ===
ℹ info info: subscribing to coffeeshop/order/details/v1/*/ca/>
ℹ info info: press Ctrl-C to exit
✔ success success: successfully subscribed to topic: coffeeshop/order/details/v1/*/ca/>
  1. 在终端2中,订阅厨房楼层应用接收来自国家ca的所有事件,无论来源类型。输入以下命令:
stm receive --topic "coffeeshop/order/details/v1/*/ca/>" --client-name "Kitchen Floor"

您应该看到以下结果:

ℹ  info: loading 'receive' command from configuration 'stm-cli-config.json'
… connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******]
ℹ info: client name: Kitchen Floor
✔ success: === Kitchen Floor successfully connected and ready to receive events. ===
ℹ info: subscribing to coffeeshop/order/details/v1/*/ca/>
ℹ info: press Ctrl-C to exit
✔ success: successfully subscribed to topic: coffeeshop/order/details/v1/*/ca/>
  1. 在终端3中,订阅人力资源应用接收来自国家ca的所有员工到达事件。输入以下命令:
stm receive --topic "coffeeshop/staff/arrive/v2/*/ca/>" --client-name "Human Resources"

您应该看到以下结果:

ℹ  info      info: loading 'receive' command from configuration 'stm-cli-config.json'
… awaiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******]
ℹ info info: client name: Human Resources
✔ success success: === Human Resources successfully connected and ready to receive events. ===
ℹ info info: subscribing to coffeeshop/staff/arrive/v2/*/ca/>
ℹ info info: press Ctrl-C to exit
✔ success success: successfully subscribed to topic: coffeeshop/staff/arrive/v2/*/ca/>
  1. 在配置发布者之前,创建一个JSON格式的消息有效载荷。将以下文本复制并粘贴到文本编辑器中,并保存文件为order.json
{
"source": "mobile",
"device": "app-android",
"country" : "Canada",
"region" : "Ontario",
"storeID" : 437,
"requestId": 44334,
"customer": {
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "123-456-7890"
},
"items": [
{
"productID": "COF001",
"productName": "Americano",
"quantity": 2,
"size": "Medium",
"options": ["Extra Shot", "Whipped Cream"]
},
{
"productID": "PST001",
"productName": "Chocolate Croissant",
"quantity": 1
}
],
"totalPrice": 12.50,
"pickupTime": "2024-01-05T14:30:00",
"specialInstructions": "Please make it extra hot."
}
  1. 在终端4中,使用您刚刚创建的消息有效载荷从订单管理应用发布事件,主题为coffeeshop/order/details/v1/{mobile}/{ca}/{437}/{44334}。输入以下命令:
stm send --topic coffeeshop/order/details/v1/mobile/ca/437/44334 --file order.json --client-name "Order Management"

您应该看到以下结果:

ℹ  info      info: loading 'send' command from configuration 'stm-cli-config.json'
… waiting connecting to broker [ws://localhost:8008, vpn: default, username: default, password: ******]
ℹ info info: client name: Order Management
✔ success success: === Order Management successfully connected and ready to publish events. ===
… waiting publishing...
✔ success success: message published to topic - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT
ℹ info info: Message Properties
Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334]
ℹ info info: Message Payload (bytes): 659
✔ success success: disconnecting from Solace PubSub+ Event Broker...
✔ success success: disconnected
✔ success success: exiting...

现在,让我们检查订阅者是否接收到了订单管理应用发布的事件。

要检查您的订阅者,请执行以下步骤:

  1. 在终端1中检查咖啡师站应用。以下消息应该出现:
✔  success: message Received - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT
ℹ info: Message Properties
Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334]
Class Of Service: COS1
DeliveryMode: DIRECT
Message Id: 1
DMQ Eligible
Binary Attachment: len=664
ℹ info: Message Payload:
{
source: "mobile",
device: "app-android",
country: "Canada",
region: "Ontario",
storeID: 437,
requestId: 44334,
customer: {
name: "John Doe",
email: "john.doe@example.com",
phone: "123-456-7890"
},
items: [
{
productID: "COF001",
productName: "Americano",
quantity: 2,
size: "Medium",
options: [
"Extra Shot",
"Whipped Cream"
]
},
{
productID: "PST001",
productName: "Chocolate Croissant",
quantity: 1
}
],
totalPrice: 12.5,
pickupTime: "2024-01-05T14:30:00",
specialInstructions: "Please make it extra hot."
}
  1. 在终端2中检查厨房楼层应用。以下消息应该出现:
✔  success   success: message Received - [Topic coffeeshop/order/details/v1/mobile/ca/437/44334], type - TEXT
ℹ info info: Message Properties
Destination: [Topic coffeeshop/order/details/v1/mobile/ca/437/44334]
ℹ info info: Message Payload (bytes): 659

  1. 在终端3中检查人力资源应用。没有消息应该出现,因为订阅不匹配发布的主题。
  2. 按Control+C(在Windows和Linux上)或Command+C(在Mac上),停止应用。

现在,您已经见识了Solace事件代理的发布-订阅的强大功能。发布的事件可以根据它们定义的主题订阅被多个消费者接收。

教程步骤

您已经完成了教程的第四步。点击链接进入下一课。

步骤描述
img设置您的第一个事件代理 - 完成
img势度Saydo EDA基础 - 完成
img设置势度Saydo Try-Me CLI工具 - 完成
img尝试发布-订阅消息交换模式 - 完成
img尝试请求-响应消息交换模式 - 下一课
img探索消费者扩展模式
img下一步

您可以随时返回到主教程页面。