使用 Azure PowerShell 创建服务总线命名空间和队列Use Azure PowerShell to create a Service Bus namespace and a queue

本快速入门介绍如何使用 Azure PowerShell 创建服务总线命名空间和队列。This quickstart shows you how to create a Service Bus namespace and a queue using the Azure PowerShell. 本快速入门还介绍了如何获取客户端应用程序向队列发送消息或从队列接收消息所使用的授权凭据。It also shows you how to get authorization credentials that a client application can use to send/receive messages to/from the queue.

什么是 Service Bus 队列?What are Service Bus queues?

服务总线队列支持中转消息传送 通信模型。Service Bus queues support a brokered messaging communication model. 在使用队列时,分布式应用程序的组件不会直接相互通信,而是通过充当中介(代理)的队列交换消息。When using queues, components of a distributed application do not communicate directly with each other; instead they exchange messages via a queue, which acts as an intermediary (broker). 消息创建方(发送方)将消息传送到队列,然后继续对其进行处理。A message producer (sender) hands off a message to the queue and then continues its processing. 消息使用方(接收方)以异步方式从队列中提取消息并处理它。Asynchronously, a message consumer (receiver) pulls the message from the queue and processes it. 创建方不必等待使用方的答复即可继续处理并发送更多消息。The producer does not have to wait for a reply from the consumer in order to continue to process and send further messages. 队列为一个或多个竞争使用方提供先入先出 (FIFO) 消息传递方式。Queues offer First In, First Out (FIFO) message delivery to one or more competing consumers. 也就是说,接收方通常会按照消息添加到队列中的顺序来接收并处理消息,并且每条消息仅由一个消息使用方接收并处理。That is, messages are typically received and processed by the receivers in the order in which they were added to the queue, and each message is received and processed by only one message consumer.

QueueConcepts

Service Bus 队列是一种可用于各种应用场景的通用技术:Service Bus queues are a general-purpose technology that can be used for a wide variety of scenarios:

  • 多层 Azure 应用程序中 Web 角色和辅助角色之间的通信。Communication between web and worker roles in a multi-tier Azure application.
  • 混合解决方案中本地应用程序和 Azure 托管应用程序之间的通信。Communication between on-premises apps and Azure-hosted apps in a hybrid solution.
  • 在不同组织或组织的各部门中本地运行的分布式应用程序组件之间的通信。Communication between components of a distributed application running on-premises in different organizations or departments of an organization.

利用队列,可以更轻松地缩放应用程序,并增强体系结构的弹性。Using queues enables you to scale your applications more easily, and enable more resiliency to your architecture.

先决条件Prerequisites

若要完成本快速入门,请确保你有一个 Azure 订阅。To complete this quickstart, make sure you have an Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, you can create a trial account before you begin.

可以在本地计算机上安装和使用 Azure PowerShell。You can install and use Azure PowerShell on your local machine.

预配资源Provision resources

  1. 首先,如果尚未安装服务总线 PowerShell 模块,请安装:First, install the Service Bus PowerShell module, if you haven't already:

    Install-Module Az.ServiceBus
    
  2. 运行以下命令来登录到 Azure:Run the following command to sign in to Azure:

    Connect-AzAccount -Environment AzureChinaCloud
    
  3. 发出以下命令来设置当前的订阅上下文,或者查看当前活动的订阅:Issue the following commands to set the current subscription context, or to see the currently active subscription:

    Select-AzSubscription -SubscriptionName "MyAzureSubName" 
    Get-AzContext
    
  4. 运行以下命令来创建 Azure 资源组。Run the following command to create an Azure resource group. 如有需要,请更新资源组名称和位置。Update the resource group name and the location if you want.

    New-AzResourceGroup -Name ContosoRG -Location chinaeast
    
  5. 运行以下命令以创建服务总线消息命名空间。Run the following command to create a Service Bus messaging namespace. 在此示例中,ContosoRG 是在上一步中创建的资源组。In this example, ContosoRG is the resource group you created in the previous step. ContosoSBusNS 是在该资源组中创建的服务总线命名空间的名称。ContosoSBusNS is the name of the Service Bus namespace created in that resource group.

    New-AzServiceBusNamespace -ResourceGroupName ContosoRG -Name ContosoSBusNS -Location chinaeast
    
  6. 运行以下命令,在上一步中创建的命名空间中创建一个队列。Run the following to create a queue in the namespace you created in the previous step.

    New-AzServiceBusQueue -ResourceGroupName ContosoRG -NamespaceName ContosoSBusNS -Name ContosoOrdersQueue 
    
  7. 获取命名空间的主连接字符串。Get the primary connection string for the namespace. 使用此连接字符串连接到队列并发送和接收消息。You use this connection string to connect to the queue and send and receive messages.

    Get-AzServiceBusKey -ResourceGroupName ContosoRG -Namespace ContosoSBusNS -Name RootManageSharedAccessKey
    

    请记下该连接字符串和队列名称。Note down the connection string and the queue name. 使用它们发送和接收消息。You use them to send and receive messages.

后续步骤Next steps

在本文中,你创建了服务总线命名空间并在其中创建了队列。In this article, you created a Service Bus namespace and a queue in the namespace. 若要了解如何向队列发送消息或从队列接收消息,请参阅“发送和接收消息”部分中的以下某个快速入门。To learn how to send/receive messages to/from the queue, see one of the following quickstarts in the Send and receive messages section.