【问题标题】:How to create ConsumerGroups for Eventhub programatically using nodejs in Azure?如何使用 Azure 中的节点 js 以编程方式为 Eventhub 创建消费者组?
【发布时间】:2021-07-07 05:06:14
【问题描述】:

如何在 Azure 中使用 nodejs 在 eventthub 中创建消费者组?

我尝试复制 .net SDK 提供的功能,但没有成功。

const { NamespaceManager } = require("@azure/service-bus");                                                                                                   
let namespaceManager = NamespaceManager.CreateFromConnectionString(eventHubConnectionString);                                                                 
let ehd = namespaceManager.GetEventHub(eventHubPath);                                                                                                         
namespaceManager.CreateConsumerGroupIfNotExists(ehd.Path, consumerGroupName);

【问题讨论】:

    标签: node.js azure azureservicebus azure-eventhub azure-node-sdk


    【解决方案1】:

    这是有效的过程:

    https://docs.microsoft.com/en-us/rest/api/eventhub/create-consumer-group

    步骤:

    1. 创建 SAS 令牌
    2. 提供正确的标头并对 REST api 进行 https 调用
    3. 只能创建一次,第二次调用会报409错误。如果您需要更新或插入调用,则需要检查它。

    SAS 令牌:

    https://docs.microsoft.com/en-us/rest/api/eventhub/generate-sas-token

    uri -- 你的 eventthub 的 url saName -- 您的托管策略的名称 saKey -- EventHub 管理策略的主键/辅助键(确保它具有管理)

    function createSharedAccessToken(uri, saName, saKey) { 
        if (!uri || !saName || !saKey) { 
                throw "Missing required parameter"; 
            } 
        var encoded = encodeURIComponent(uri); 
        var now = new Date(); 
        var week = 60*60*24*7;
        var ttl = Math.round(now.getTime() / 1000) + week;
        var signature = encoded + '\n' + ttl; 
        var signatureUTF8 = utf8.encode(signature); 
        var hash = crypto.createHmac('sha256', saKey).update(signatureUTF8).digest('base64'); 
        return 'SharedAccessSignature sr=' + encoded + '&sig=' +  
            encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName; 
    }
    

    请求参数:

    网址:

    https://your-namespace.servicebus.windows.net/your-event-hub/consumergroups/testCG?timeout=60&api-version=2014-01

    标题:

    Content-Type: application/atom+xml;type=entry;charset=utf-8
    Host: your-namespace.servicebus.windows.net
    Authorization: {replace with the content from your SAS Token}
    

    有效载荷:

    <entry xmlns="http://www.w3.org/2005/Atom">  
       <content type="application/xml">  
          <ConsumerGroupDescription xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">Any name you want</ConsumerGroupDescription>  
       </content>  
    </entry>
    

    可能的退货状态:

    201 -- Successful Creation
    404 -- Not found, you are using a name that does not exist
    409 -- The messaging entity 'XXX' already exists.
    

    如果您发现任何其他问题,请发表评论。

    【讨论】:

      【解决方案2】:

      事件中心 Node.JS SDK 不支持管理操作。

      试试像https://www.nuget.org/packages/Microsoft.Azure.Management.EventHub/这样的管理客户端

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-03
      • 2014-08-02
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多