使用 Azure 门户创建一个服务总线主题和多个对该主题的订阅Use the Azure portal to create a Service Bus topic and subscriptions to the topic

在本快速入门中,将使用 Azure 门户创建服务总线主题,然后创建对该主题的订阅。In this quickstart, you use the Azure portal to create a Service Bus topic and then create subscriptions to that topic.

什么是服务总线主题和订阅?What are Service Bus topics and subscriptions?

服务总线主题和订阅支持 发布/订阅 消息通信模型。Service Bus topics and subscriptions support a publish/subscribe messaging communication model. 在使用主题和订阅时,分布式应用程序的组件不会直接相互通信,而是通过充当中介的主题交换消息。When using topics and subscriptions, components of a distributed application do not communicate directly with each other; instead they exchange messages via a topic, which acts as an intermediary.


与每条消息都由单个使用方处理的服务总线队列相比,主题和订阅通过发布/订阅模式提供一对多通信方式。In contrast with Service Bus queues, in which each message is processed by a single consumer, topics and subscriptions provide a one-to-many form of communication, using a publish/subscribe pattern. 可向一个主题注册多个订阅。It is possible to register multiple subscriptions to a topic. 当消息发送到主题时,每个订阅会分别对该消息进行处理。When a message is sent to a topic, it is then made available to each subscription to handle/process independently. 主题订阅类似于接收发送至该主题的消息副本的虚拟队列。A subscription to a topic resembles a virtual queue that receives copies of the messages that were sent to the topic. 可以选择基于每个订阅注册主题的筛选规则,这样就可以筛选或限制哪些主题订阅接收发送至某个主题的哪些消息。You can optionally register filter rules for a topic on a per-subscription basis, which allows you to filter or restrict which messages to a topic are received by which topic subscriptions.

利用服务总线主题和订阅,可以进行扩展以处理跨大量用户和应用程序的许多消息。Service Bus topics and subscriptions enable you to scale to process a large number of messages across a large number of users and applications.

在 Azure 门户中创建命名空间Create a namespace in the Azure portal

若要开始在 Azure 中使用服务总线消息实体,必须先使用在 Azure 中唯一的名称创建一个命名空间。To begin using Service Bus messaging entities in Azure, you must first create a namespace with a name that is unique across Azure. 命名空间提供了用于对应用程序中的 Service Bus 资源进行寻址的范围容器。A namespace provides a scoping container for addressing Service Bus resources within your application.

创建命名空间:To create a namespace:

  1. 登录到 Azure 门户Sign in to the Azure portal

  2. 在门户的左侧导航窗格中,依次选择“+ 创建资源”、“集成”、“服务总线” 。In the left navigation pane of the portal, select + Create a resource, select Integration, and then select Service Bus.


  3. 在“创建命名空间”对话框中执行以下步骤 :In the Create namespace dialog, do the following steps:

    1. 输入 命名空间的名称Enter a name for the namespace. 系统会立即检查该名称是否可用。The system immediately checks to see if the name is available. 若需用于对命名空间进行命名的规则的列表,请参阅创建命名空间 REST APIFor a list of rules for naming namespaces, see Create Namespace REST API.

    2. 选择命名空间的定价层(“基本”、“标准”或“高级”)。Select the pricing tier (Basic, Standard, or Premium) for the namespace. 若要使用主题和订阅,请选择“标准”或“高级”。If you want to use topics and subscriptions, choose either Standard or Premium. 基本定价层中不支持主题/订阅。Topics/subscriptions are not supported in the Basic pricing tier.

    3. 如果选择了“高级”定价层,请执行以下步骤: If you selected the Premium pricing tier, follow these steps:

      1. 指定 消息传送单元 的数目。Specify the number of messaging units. 高级层在 CPU 和内存级别提供资源隔离,使每个工作负荷在隔离的环境中运行。The premium tier provides resource isolation at the CPU and memory level so that each workload runs in isolation. 此资源容器称为消息传送单元。This resource container is called a messaging unit. 高级命名空间至少具有一个消息传送单元。A premium namespace has at least one messaging unit. 可为每个服务总线高级命名空间选择 1、2 或 4 个消息传送单元。You can select 1, 2, or 4 messaging units for each Service Bus Premium namespace. 有关详细信息,请参阅服务总线高级消息传送For more information, see Service Bus Premium Messaging.

      2. 指定是否要将命名空间设为 区域冗余Specify whether you want to make the namespace zone redundant. 区域冗余通过在一个区域中的所有可用性区域之间分散副本来提供增强的可用性。The zone redundancy provides enhanced availability by spreading replicas across availability zones within one region at no additional cost.

    4. 对于“订阅”,请选择要在其中创建命名空间的 Azure 订阅。 For Subscription, choose an Azure subscription in which to create the namespace.

    5. 对于“资源组”,请选择该命名空间驻留到的现有资源组,或创建一个新资源组。 For Resource group, choose an existing resource group in which the namespace will live, or create a new one.

    6. 对于“位置”,请选择托管该命名空间的区域。 For Location, choose the region in which your namespace should be hosted.

    7. 选择“创建” 。Select Create. 系统现已创建命名空间并已将其启用。The system now creates your namespace and enables it. 可能需要等待几分钟,因为系统将为帐户配置资源。You might have to wait several minutes as the system provisions resources for your account.


  4. 确认是否已成功部署服务总线命名空间。Confirm that the service bus namespace is deployed successfully. 若要查看通知,请在工具栏上选择 钟形图标(警报)To see the notifications, select the bell icon (Alerts) on the toolbar. 如下图所示,在通知中选择 资源组的名称Select the name of the resource group in the notification as shown in the image. 将会看到包含服务总线命名空间的资源组。You see the resource group that contains the service bus namespace.


  5. 在资源组的“资源组”页上,选择你的 服务总线命名空间On the Resource group page for your resource group, select your service bus namespace.

    “资源组”页 - 选择服务总线命名空间

  6. 将会看到服务总线命名空间的主页。You see the home page for your service bus namespace.


获取连接字符串Get the connection string

创建新的命名空间时,会自动生成一项初始的共享访问签名 (SAS) 规则,将一对主密钥和辅助密钥关联到一起,向每个密钥授予对命名空间的所有资产的完全控制权限。Creating a new namespace automatically generates an initial Shared Access Signature (SAS) rule with an associated pair of primary and secondary keys that each grant full control over all aspects of the namespace. 请参阅服务总线身份验证和授权,了解如何创建规则来对普通发送者和接收者的权限进行更多限制。See Service Bus authentication and authorization for information about how to create rules with more constrained rights for regular senders and receivers. 若要复制命名空间的主要密钥和辅助密钥,请执行以下步骤:To copy the primary and secondary keys for your namespace, follow these steps:

  1. 单击“所有资源”,然后单击新创建的命名空间名称。 Click All resources, then click the newly created namespace name.

  2. 在命名空间窗口中,单击“共享访问策略” 。In the namespace window, click Shared access policies.

  3. 在“共享访问策略” 屏幕中,单击“RootManageSharedAccessKey” 。In the Shared access policies screen, click RootManageSharedAccessKey.


  4. 在“策略: RootManageSharedAccessKey”窗口中,单击“主连接字符串”旁边的复制按钮,将连接字符串复制到剪贴板供稍后使用 。In the Policy: RootManageSharedAccessKey window, click the copy button next to Primary Connection String, to copy the connection string to your clipboard for later use. 将此值粘贴到记事本或其他某个临时位置。Paste this value into Notepad or some other temporary location.

    屏幕截图显示了名为 RootManageSharedAccessKey 的 S A S 策略,其中包含密钥和连接字符串。

  5. 重复上述步骤,将 主键 的值复制和粘贴到临时位置,以供将来使用。Repeat the previous step, copying and pasting the value of Primary key to a temporary location for later use.

使用 Azure 门户创建主题Create a topic using the Azure portal

  1. 在“服务总线命名空间” 页面上,选择左侧菜单中的“主题” 。On the Service Bus Namespace page, select Topics on the left menu.

  2. 在工具栏中选择“+ 主题”。 Select + Topic on the toolbar.

  3. 输入主题名称 。Enter a name for the topic. 将其他选项保留默认值。Leave the other options with their default values.

  4. 选择“创建” 。Select Create.


创建对主题的订阅Create subscriptions to the topic

  1. 选择在上一部分创建的主题 。Select the topic that you created in the previous section.


  2. 在“服务总线主题” 页面上,从左侧菜单中选择“订阅” ,然后工具栏上选择“+ 订阅” 。On the Service Bus Topic page, select Subscriptions from the left menu, and then select + Subscription on the toolbar.


  3. 在“创建订阅” 页面上,输入 S1 作为订阅名称 ,然后选择“创建” 。On the Create subscription page, enter S1 for name for the subscription, and then select Create.


  4. 重复上述步骤两次,创建名为 S2S3 的订阅。Repeat the previous step twice to create subscriptions named S2 and S3.


可以使用服务总线资源管理器管理服务总线资源。You can manage Service Bus resources with Service Bus Explorer. 服务总线资源管理器允许用户连接到服务总线命名空间并以一种简单的方式管理消息传送实体。The Service Bus Explorer allows users to connect to a Service Bus namespace and administer messaging entities in an easy manner. 该工具提供高级功能,如导入/导出功能或用于对主题、队列、订阅、中继服务、通知中心和事件中心进行测试的功能。The tool provides advanced features like import/export functionality or the ability to test topic, queues, subscriptions, relay services, notification hubs and events hubs.

后续步骤Next steps

在本文中,你创建了服务总线命名空间,在命名空间中创建了一个主题,并为该主题创建了三个订阅。In this article, you created a Service Bus namespace, a topic in the namespace, and three subscriptions to the topic. 若要了解如何将消息发布到主题并从订阅订阅消息,请参阅“发布和订阅消息”部分中的以下快速入门之一。To learn how to publish messages to the topic and subscribe for messages from a subscription, see one of the following quickstarts in the Publish and subscribe for messages section.