获取事件中心连接字符串Get an Event Hubs connection string

若要使用事件中心,需要创建一个事件中心命名空间。To use Event Hubs, you need to create an Event Hubs namespace. 命名空间是多个事件中心或 Kafka 主题的范围容器。A namespace is a scoping container for multiple event hubs or Kafka topics. 此命名空间提供唯一的 FQDN。This namespace gives you a unique FQDN. 创建命名空间后,可以获取与事件中心通信所需的连接字符串。Once a namespace is created, you can obtain the connection string required to communicate with Event Hubs.

Azure 事件中心的连接字符串中嵌入了以下组成部分:The connection string for Azure Event Hubs has the following components embedded within it,

  • FQDN = 创建的事件中心命名空间的 FQDN(包括事件中心命名空间的名称,后接 servicebus.chinacloudapi.cn)FQDN = the FQDN of the EventHubs namespace you created (it includes the EventHubs namespace name followed by servicebus.chinacloudapi.cn)
  • SharedAccessKeyName = 为应用程序的 SAS 密钥选择的名称SharedAccessKeyName = the name you chose for your application's SAS keys
  • SharedAccessKey = 生成的密钥值。SharedAccessKey = the generated value of the key.

连接字符串模板如下所示The connection string template looks like

Endpoint=sb://<FQDN>/;SharedAccessKeyName=<KeyName>;SharedAccessKey=<KeyValue>

Endpoint=sb://dummynamespace.servicebus.chinacloudapi.cn/;SharedAccessKeyName=DummyAccessKeyName;SharedAccessKey=5dOntTRytoC24opYThisAsit3is2B+OGY1US/fuL3ly= 是连接字符串的示例An example connection string might look like Endpoint=sb://dummynamespace.servicebus.chinacloudapi.cn/;SharedAccessKeyName=DummyAccessKeyName;SharedAccessKey=5dOntTRytoC24opYThisAsit3is2B+OGY1US/fuL3ly=

本文逐步讲解获取连接字符串的各种方法。This article walks you through various ways of obtaining the connection string.

从门户中获取连接字符串Get connection string from the portal

  1. 登录到 Azure 门户Sign in to Azure portal.

  2. 在左侧导航菜单中,选择“所有服务”。Select All services on the left navigational menu.

  3. 选择“分析”部分中的“事件中心” 。Select Event Hubs in the Analytics section.

  4. 在事件中心列表中,选择事件中心。In the list of event hubs, select your event hub.

  5. 在“事件中心命名空间”页中的左侧菜单上选择“共享访问策略” 。On the Event Hubs Namespace page, select Shared Access Policies on the left menu.

    共享访问策略菜单项

  6. 在策略列表中选择“共享访问策略”。Select a shared access policy in the list of policies. 默认值命名为:RootManageSharedAccessPolicy。The default one is named: RootManageSharedAccessPolicy. 可以添加具有适当权限(读取、写入)的策略,并使用该策略。You can add a policy with appropriate permissions (read, write), and use that policy.

    事件中心共享访问策略

  7. 选择“连接字符串 - 主密钥”字段旁边的“复制”按钮 。Select the copy button next to the Connection string-primary key field.

    事件中心 - 获取连接字符串

使用 Azure PowerShell 获取连接字符串Getting the connection string with Azure PowerShell

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

可以使用 Get-AzEventHubKey 获取特定策略/规则名称的连接字符串,如下所示:You can use the Get-AzEventHubKey to get the connection string for the specific policy/rule name as shown below:

Get-AzEventHubKey -ResourceGroupName dummyresourcegroup -NamespaceName dummynamespace -AuthorizationRuleName RootManageSharedAccessKey

使用 Azure CLI 获取连接字符串Getting the connection string with Azure CLI

可使用以下命令获取命名空间的连接字符串:You can use the following to get the connection string for the namespace:

az eventhubs namespace authorization-rule keys list --resource-group dummyresourcegroup --namespace-name dummynamespace --name RootManageSharedAccessKey

或者可以使用以下命令获取 EventHub 实体的连接字符串:Or you can use the following to get the connection string for an EventHub entity:

az eventhubs eventhub authorization-rule keys list --resource-group dummyresourcegroup --namespace-name dummynamespace --eventhub-name dummyeventhub --name RootManageSharedAccessKey

有关事件中心 Azure CLI 命令的详细信息,请参阅事件中心的 Azure CLIFor more information about Azure CLI commands for Event Hubs, see Azure CLI for Event Hubs.

后续步骤Next steps

访问以下链接可以了解有关事件中心的详细信息:You can learn more about Event Hubs by visiting the following links: