事件中心管理库Event Hubs management libraries

可以使用 Azure 事件中心管理库动态预配事件中心命名空间和实体。You can use the Azure Event Hubs management libraries to dynamically provision Event Hubs namespaces and entities. 这种动态性可以实现复杂的部署和消息传送方案,以便能够以编程方式确定要预配的实体。This dynamic nature enables complex deployments and messaging scenarios, so that you can programmatically determine what entities to provision. 这些库目前可用于 .NET。These libraries are currently available for .NET.

受支持的功能Supported functionality

  • 创建、更新、删除命名空间Namespace creation, update, deletion
  • 事件中心创建、更新、删除Event Hubs creation, update, deletion
  • 资源组创建、更新、删除Consumer Group creation, update, deletion

先决条件Prerequisites

若要开始使用事件中心管理库,必须使用 Azure Active Directory (AAD) 进行身份验证。To get started using the Event Hubs management libraries, you must authenticate with Azure Active Directory (AAD). AAD 要求以提供 Azure 资源访问权限的服务主体身份进行身份验证。AAD requires that you authenticate as a service principal, which provides access to your Azure resources. 有关创建服务主体的信息,请参阅以下文章之一:For information about creating a service principal, see one of these articles:

这些教程提供 AppId(客户端 ID)、TenantIdClientSecret(身份验证密钥),这些都将用于管理库进行的身份验证。These tutorials provide you with an AppId (Client ID), TenantId, and ClientSecret (authentication key), all of which are used for authentication by the management libraries. 若要对资源组运行命令,必须拥有“所有者” 权限。You must have Owner permissions for the resource group on which you want to run.

编程模式Programming pattern

所有事件中心资源的操纵模式都遵循常用协议:The pattern to manipulate any Event Hubs resource follows a common protocol:

  1. 使用 Microsoft.IdentityModel.Clients.ActiveDirectory 库从 AAD 获取令牌。Obtain a token from AAD using the Microsoft.IdentityModel.Clients.ActiveDirectory library.

    var context = new AuthenticationContext($"https://login.chinacloudapi.cn/{tenantId}");
    
    var result = await context.AcquireTokenAsync(
        "https://management.core.chinacloudapi.cn/",
        new ClientCredential(clientId, clientSecret)
    );
    
  2. 创建 EventHubManagementClient 对象。Create the EventHubManagementClient object.

    var creds = new TokenCredentials(token);
    var ehClient = new EventHubManagementClient(creds)
    {
        SubscriptionId = SettingsCache["SubscriptionId"]
    };
    
  3. CreateOrUpdate 参数设置为指定值。Set the CreateOrUpdate parameters to your specified values.

    var ehParams = new EventHubCreateOrUpdateParameters()
    {
        Location = SettingsCache["DataCenterLocation"]
    };
    
  4. 执行调用。Execute the call.

    await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, EventHubName, ehParams);
    

后续步骤Next steps