快速入門:使用 Azure PowerShell 建立事件中樞

在本快速入門中,您會使用 Azure PowerShell 建立事件中樞。

必要條件

具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶

Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。

要啟動 Azure Cloud Shell:

選項 範例/連結
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 Screenshot that shows an example of Try It for Azure Cloud Shell.
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 Button to launch Azure Cloud Shell.
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 Screenshot that shows the Cloud Shell button in the Azure portal

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

如果您在本機使用 PowerShell,您必須執行最新版本的 PowerShell,才能完成本快速入門。 如果您需要安裝或升級,請參閱 安裝和設定 Azure PowerShell

建立資源群組

執行下列 命令以建立資源群組。 資源群組是 Azure 資源的邏輯集合。 所有資源都會在資源群組中部署與管理。

如果您使用 Azure Cloud Shell,請從左上角的 Bash 切換至 PowerShell。 選取 [複製 ] 以複製命令,並將它貼到 Cloud Shell 中,然後執行它。

下列範例會在美國東部區域建立資源群組。 將取代 myResourceGroup 為您要使用的資源組名。

$rgName="myResourceGroup$(Get-Random)"
$region="eastus"
New-AzResourceGroup –Name $rgName –Location $region

您會看到類似以下的輸出。 您會看到具有隨機數位後綴的資源名稱。

ResourceGroupName : myResourceGroup1625872532
Location          : eastus
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/0000000000-0000-0000-0000-0000000000000/resourceGroups/myResourceGroup1625872532

建立事件中樞命名空間

執行下列命令,以在資源群組中建立事件中樞命名空間。 事件中樞命名空間提供唯一的完整功能變數名稱,您可以在其中建立一或多個事件中樞。 如有需要,請更新命名空間的值。

$namespaceName="myNamespace$(Get-Random)"
New-AzEventHubNamespace -ResourceGroupName $rgName -NamespaceName $namespaceName -Location $region

您會看到類似以下的輸出。 您會在 Name 欄位中看到命名空間的名稱。

Name                   : myNamespace143349827
Id                     : /subscriptions/0000000000-0000-0000-0000-00000000000000/resourceGroups/myResourceGroup162587253
                         2/providers/Microsoft.EventHub/namespaces/myNamespace143349827
ResourceGroupName      : myResourceGroup1625872532
Location               : East US
Sku                    : Name : Standard , Capacity : 1 , Tier : Standard
Tags                   : 
ProvisioningState      : Succeeded
Status                 : Active
CreatedAt              : 3/13/2023 10:22:54 PM
UpdatedAt              : 3/13/2023 10:23:41 PM
ServiceBusEndpoint     : https://myNamespace143349827.servicebus.windows.net:443/
Enabled                : True
KafkaEnabled           : True
IsAutoInflateEnabled   : False
MaximumThroughputUnits : 0
ZoneRedundant          : False
ClusterArmId           : 
DisableLocalAuth       : False
MinimumTlsVersion      : 1.2
KeySource              : 
Identity               : 
IdentityType           : 
IdentityId             : 
EncryptionConfig       :

建立事件中樞

既然您有事件中樞命名空間,請執行下列命令,在該命名空間內建立事件中樞。

$ehubName="myEventHub"
New-AzEventHub -ResourceGroupName $rgName -NamespaceName $namespaceName -EventHubName $ehubName

您會看到類似下列的輸出。

ArchiveNameFormat            : 
BlobContainer                : 
CaptureEnabled               : 
CreatedAt                    : 3/13/2023 10:26:07 PM
DataLakeAccountName          : 
DataLakeFolderPath           : 
DataLakeSubscriptionId       : 
DestinationName              : 
Encoding                     : 
Id                           : /subscriptions/00000000000-0000-0000-0000-00000000000000/resourceGroups/myResourceGroup162
                               5872532/providers/Microsoft.EventHub/namespaces/myNamespace143349827/eventhubs/myEven
                               tHub
IntervalInSeconds            : 
Location                     : eastus
MessageRetentionInDays       : 7
Name                         : myEventHub
PartitionCount               : 4
PartitionId                  : {0, 1, 2, 3}
ResourceGroupName            : myResourceGroup1625872532
SizeLimitInBytes             : 
SkipEmptyArchive             : 
Status                       : Active
StorageAccountResourceId     : 
SystemDataCreatedAt          : 
SystemDataCreatedBy          : 
SystemDataCreatedByType      : 
SystemDataLastModifiedAt     : 
SystemDataLastModifiedBy     : 
SystemDataLastModifiedByType : 
Type                         : Microsoft.EventHub/namespaces/eventhubs
UpdatedAt                    : 3/13/2023 10:26:07 PM

恭喜! 您已使用 Azure PowerShell 來建立事件中樞命名空間,以及該命名空間內的事件中樞。

清除資源

如果您想保留此事件中樞,以便測試傳送和接收事件,請忽略本節。 否則,執行下列命令來刪除整個資源群組。 此命令會刪除資源群組中的所有資源和資源群組本身。

Remove-AzResourceGroup $rgName

下一步

在本文中,您已建立事件中樞命名空間,並使用範例應用程式從事件中樞傳送和接收事件。 如需將事件傳送至事件中樞或從事件中樞接收事件的逐步指示,請參閱傳送及接收事件教學課程: