使用 Azure CLI 建立服務匯流排命名空間和佇列Use the Azure CLI to create a Service Bus namespace and a queue

本快速入門會說明如何使用 Azure CLI 來建立服務匯流排命名空間和佇列。This quickstart shows you how to create a Service Bus namespace and a queue using the Azure CLI. 同時也會示範如何取得授權認證,讓用戶端應用程式用來將訊息傳送至佇列,或從佇列接收訊息。It also shows you how to get authorization credentials that a client application can use to send/receive messages to/from the queue.

什麼是服務匯流排佇列?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.

佇列概念

服務匯流排佇列為適用於各種情況的通用技術: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 訂用帳戶,您可以在開始前建立免費帳戶If you don't have an Azure subscription, you can create a free account before you begin.

在本快速入門中,您會使用可在登入 Azure 入口網站後啟動的 Azure Cloud Shell。In this quickstart, you use Azure Cloud Shell that you can launch after sign into the Azure portal. 如需 Azure Cloud Shell 的詳細資訊,請參閱 Azure Cloud Shell 概觀For details about Azure Cloud Shell, see Overview of Azure Cloud Shell. 您也可以安裝 Azure PowerShell 並在電腦上使用。You can also install and use Azure PowerShell on your machine.

佈建資源Provision resources

  1. 登入 Azure 入口網站Sign into the Azure portal.

  2. 選取下圖中的圖示來啟動 Azure Cloud Shell。Launch Azure Cloud Shell by selecting the icon shown in the following image. 如果 Cloud Shell 處於 PowerShell 模式,請切換至 Bash 模式。Switch to Bash mode if the Cloud Shell is in PowerShell mode.

    啟動 Cloud Shell

  3. 執行下列命令以建立 Azure 資源群組。Run the following command to create an Azure resource group. 您可以視需要更新資源群組名稱和位置。Update the resource group name and the location if you want.

    az group create --name ContosoRG --location eastus
    
  4. 執行下列命令來建立服務匯流排傳訊命名空間。Run the following command to create a Service Bus messaging namespace.

    az servicebus namespace create --resource-group ContosoRG --name ContosoSBusNS --location eastus
    
  5. 執行下列命令,在上一個步驟建立的命名空間中建立佇列。Run the following command to create a queue in the namespace you created in the previous step. 在本範例中,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.

    az servicebus queue create --resource-group ContosoRG --namespace-name ContosoSBusNS --name ContosoOrdersQueue
    
  6. 執行下列命令,以取得命名空間的主要連接字串。Run the following command to get the primary connection string for the namespace. 您可以使用此連接字串連線到佇列,並傳送和接收訊息。You use this connection string to connect to the queue and send and receive messages.

    az servicebus namespace authorization-rule keys list --resource-group ContosoRG --namespace-name ContosoSBusNS --name RootManageSharedAccessKey --query primaryConnectionString --output tsv    
    

    記下連接字串和佇列名稱。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.