使用自动转发链接服务总线实体Chaining Service Bus entities with autoforwarding

通过服务总线自动转发功能可将队列或订阅链接到作为相同命名空间组成部分的另一个队列或主题。The Service Bus autoforwarding feature enables you to chain a queue or subscription to another queue or topic that is part of the same namespace. 启用自动转发时,服务总线会自动删除放置在第一个队列或订阅(源)中的消息,并将其放入第二个队列或主题(目标)中。When autoforwarding is enabled, Service Bus automatically removes messages that are placed in the first queue or subscription (source) and puts them in the second queue or topic (destination). 仍可将消息直接发送到目标实体。It is still possible to send a message to the destination entity directly.

使用自动转发Using autoforwarding

可通过在源的 QueueDescriptionSubscriptionDescription 对象上设置 QueueDescription.ForwardToSubscriptionDescription.ForwardTo 属性来启用自动转发,如以下示例所示:You can enable autoforwarding by setting the QueueDescription.ForwardTo or SubscriptionDescription.ForwardTo properties on the QueueDescription or SubscriptionDescription objects for the source, as in the following example:

SubscriptionDescription srcSubscription = new SubscriptionDescription (srcTopic, srcSubscriptionName);
srcSubscription.ForwardTo = destTopic;
namespaceManager.CreateSubscription(srcSubscription));

创建源实体时,目标实体必须存在。The destination entity must exist at the time the source entity is created. 如果目标实体不存在,则当创建源实体时,服务总线返回异常。If the destination entity does not exist, Service Bus returns an exception when asked to create the source entity.

可使用自动转发扩大单个主题。You can use autoforwarding to scale out an individual topic. 服务总线将给定主题的订阅数限制为 2,000。Service Bus limits the number of subscriptions on a given topic to 2,000. 可以通过创建二级主题来容纳其他订阅。You can accommodate additional subscriptions by creating second-level topics. 即使订阅数并未受到服务总线限制,添加二级主题也可以提高主题的整体吞吐量。Even if you are not bound by the Service Bus limitation on the number of subscriptions, adding a second level of topics can improve the overall throughput of your topic.

自动转发方案

自动转发还可用于分离消息发送方与接收方。You can also use autoforwarding to decouple message senders from receivers. 例如,考虑一个由以下三个模块组成的 ERP 系统:订单处理、库存管理和客户关系管理。For example, consider an ERP system that consists of three modules: order processing, inventory management, and customer relations management. 每个模块都会生成消息,这些消息将被排入相应的主题队。Each of these modules generates messages that are enqueued into a corresponding topic. Alice 和 Bob 是两名销售代表,他们想了解与其客户相关的所有消息。Alice and Bob are sales representatives that are interested in all messages that relate to their customers. 要接收这些消息,Alice 和 Bob 各自创建一个个人队列和一个针对 ERP 主题的订阅,该订阅将所有消息自动转发给该队列。To receive those messages, Alice and Bob each create a personal queue and a subscription on each of the ERP topics that automatically forward all messages to their queue.

自动转发方案

如果 Alice 去度假,则填充她的个人队列,而不是 ERP 主题。If Alice goes on vacation, her personal queue, rather than the ERP topic, fills up. 在此方案中,由于销售代表没有接收到任何消息,所以所有 ERP 主题都没有达到配额。In this scenario, because a sales representative has not received any messages, none of the ERP topics ever reach quota.

Note

设置自动转发时,源和目标上的 AutoDeleteOnIdle 值自动设置为数据类型的最大值。When autoforwarding is setup, the value for AutoDeleteOnIdle on both the Source and the Destination is automatically set to the maximum value of the data type.

  • 在源端,自动转发充当接收操作。On the Source side, autoforwarding acts as a receive operation. 因此,具有自动转发设置的源从未真正“空闲”。So the source which has autoforwarding setup is never really "idle".
  • 在目标端,这样做是为了确保始终有要将消息转发到的目标。On the destination side, this is done to ensure that there is always a destination to forward the message to.

自动转发注意事项Autoforwarding considerations

如果目标实体累积了过多消息并超出配额,或禁用了目标实体,则源实体会将消息添加到其死信队列,直到目标中存在可用空间(或重新启用了该实体)。If the destination entity accumulates too many messages and exceeds the quota, or the destination entity is disabled, the source entity adds the messages to its dead-letter queue until there is space in the destination (or the entity is re-enabled). 这些消息将继续位于死信队列中,因此,必须从死信队列显式接收和处理它们。Those messages continue to live in the dead-letter queue, so you must explicitly receive and process them from the dead-letter queue.

将单个主题链接到一起以获取包含许多订阅的复合主题时,建议设置适量的一级主题订阅和许多二级主题订阅。When chaining together individual topics to obtain a composite topic with many subscriptions, it is recommended that you have a moderate number of subscriptions on the first-level topic and many subscriptions on the second-level topics. 例如,一个包含 20 个订阅的一级主题(其中每个订阅都链接到包含 200 个订阅的二级主题)就能够比包含 200 个订阅的一级主题(其中每个订阅链接到包含 20 个订阅的二级主题)具有更高的吞吐量。For example, a first-level topic with 20 subscriptions, each of them chained to a second-level topic with 200 subscriptions, allows for higher throughput than a first-level topic with 200 subscriptions, each chained to a second-level topic with 20 subscriptions.

服务总线对于每条转发的消息收取一个操作的费用。Service Bus bills one operation for each forwarded message. 例如,将一条消息发送到一个包含 20 个订阅(每个订阅配置为将消息自动转发到另一队列或主题)的主题,如果所有第一级别的订阅都接收到此消息的副本,则会作为 21 次操作进行计费。For example, sending a message to a topic with 20 subscriptions, each of them configured to autoforward messages to another queue or topic, is billed as 21 operations if all first-level subscriptions receive a copy of the message.

若要创建链接到另一个队列或主题的订阅,则订阅创建者必须具有源和目标实体的管理权限。To create a subscription that is chained to another queue or topic, the creator of the subscription must have Manage permissions on both the source and the destination entity. 将消息发送到源主题仅需要源主题的发送权限。Sending messages to the source topic only requires Send permissions on the source topic.

后续步骤Next steps

有关自动转发的详细信息,请参阅以下参考主题:For detailed information about autoforwarding, see the following reference topics:

若要深入了解服务总线性能提升,请参阅To learn more about Service Bus performance improvements, see