你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure PowerShell 创建事件中心

在本快速入门中,请使用 Azure PowerShell 创建事件中心。

先决条件

具有活动订阅的 Azure 帐户。 免费创建帐户

Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。 可以将 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

后续步骤

在本文中,我们已创建事件中心命名空间,并使用示例应用程序从事件中心发送和接收事件。 有关如何将事件发送到事件中心(或)从事件中心接收事件的分步说明,请参阅“发送和接收事件”教程: