使用 Azure 入口網站建立服務匯流排命名空間和佇列Use Azure portal to create a Service Bus namespace and a queue

本快速入門會說明如何使用 Azure 入口網站來建立服務匯流排命名空間和佇列。This quickstart shows you how to create a Service Bus namespace and a queue using the Azure portal. 同時也會示範如何取得授權認證,讓用戶端應用程式用來將訊息傳送至佇列,或從佇列接收訊息。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 訂用帳戶。To complete this quickstart, make sure you have an Azure subscription. 如果您沒有 Azure 訂用帳戶,您可以在開始前建立免費帳戶If you don't have an Azure subscription, you can create a free account before you begin.

在 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. 命名空間提供範圍容器,可在應用程式內定址服務匯流排資源。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. 如需詳細資訊,請參閱 Azure 中的可用性區域For more information, see Availability zones in Azure.
    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. 在 [共用存取原則] 畫面中,按一下 RootManageSharedAccessKeyIn 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 queue in the Azure portal

  1. 在 [服務匯流排命名空間] 頁面的左側導覽功能表中選取 [佇列]。On the Service Bus Namespace page, select Queues in the left navigational menu.

  2. 在 [佇列] 頁面上,選取工具列上的 [+ 佇列]。On the Queues page, select + Queue on the toolbar.

  3. 輸入佇列的 [名稱],並且讓其他值保留其預設值。Enter a name for the queue, and leave the other values with their defaults.

  4. 現在,選取 [建立]。Now, select Create.

    佇列 -> + 佇列 -> 輸入名稱 -> 建立

後續步驟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.