这是有效的过程:
https://docs.microsoft.com/en-us/rest/api/eventhub/create-consumer-group
脚步:
- 创建 SAS 令牌
- 提供正确的标头并对 REST api 进行 https 调用
- 只能创建一次,第二次调用会报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.
如果您发现任何其他问题,请发表评论。