基于ARM模板创建Azure IoT Hub端点及兼容Event Hub的动态连接字符串咨询
我来帮你搞定这两个关于ARM模板创建IoT Hub端点的问题,结合实际的模板示例给你讲清楚:
1. 使用ARM模板创建Azure IoT Hub端点
IoT Hub的路由端点(比如指向Event Hub、Service Bus队列/主题等)需要定义在ARM模板中Microsoft.Devices/IotHubs资源的properties.routingEndpoints属性下。下面是一个创建指向自定义Event Hub端点的完整模板示例:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ // 先部署Event Hub命名空间和Event Hub(如果还未存在) { "type": "Microsoft.EventHub/namespaces", "apiVersion": "2021-11-01", "name": "[parameters('eventHubNamespaceName')]", "location": "[parameters('location')]", "sku": { "name": "Standard", "tier": "Standard", "capacity": 1 } }, { "type": "Microsoft.EventHub/namespaces/eventhubs", "apiVersion": "2021-11-01", "name": "[concat(parameters('eventHubNamespaceName'), '/', parameters('eventHubName'))]", "dependsOn": [ "[resourceId('Microsoft.EventHub/namespaces', parameters('eventHubNamespaceName'))]" ], "properties": { "messageRetentionInDays": 7, "partitionCount": 4 } }, // 部署IoT Hub并添加自定义Event Hub端点 { "type": "Microsoft.Devices/IotHubs", "apiVersion": "2021-07-01", "name": "[parameters('iotHubName')]", "location": "[parameters('location')]", "sku": { "name": "S1", "capacity": 1 }, "properties": { "routingEndpoints": { "eventHubs": [ { "connectionString": "[concat('Endpoint=sb://', parameters('eventHubNamespaceName'), '.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=', listKeys(resourceId('Microsoft.EventHub/namespaces/authorizationRules', parameters('eventHubNamespaceName'), 'RootManageSharedAccessKey'), '2021-11-01').primaryKey, ';EntityPath=', parameters('eventHubName'))]", "name": "[parameters('customEndpointName')]", "subscriptionId": "[subscription().subscriptionId]", "resourceGroup": "[resourceGroup().name]" } ] } }, "dependsOn": [ "[resourceId('Microsoft.EventHub/namespaces/eventhubs', parameters('eventHubNamespaceName'), parameters('eventHubName'))]" ] } ], "parameters": { "iotHubName": { "type": "string" }, "eventHubNamespaceName": { "type": "string" }, "eventHubName": { "type": "string" }, "customEndpointName": { "type": "string" }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]" } } }
这里的核心是用listKeys函数动态获取Event Hub命名空间的根授权密钥,然后拼接成符合格式的连接字符串,完全避免了硬编码密钥的问题,实现自动化部署。
2. 创建与Event Hub兼容的IoT Hub端点并动态生成连接字符串
这里要分两种场景来处理:
场景一:IoT Hub内置的Event Hub兼容端点(用于接收设备遥测)
这个端点是IoT Hub默认自带的,不需要手动创建。它的连接字符串可以通过ARM模板的listKeys和reference函数动态生成,你可以把它作为部署输出获取:
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "resources": [ { "type": "Microsoft.Devices/IotHubs", "apiVersion": "2021-07-01", "name": "[parameters('iotHubName')]", "location": "[parameters('location')]", "sku": { "name": "S1", "capacity": 1 } } ], "parameters": { "iotHubName": { "type": "string" }, "location": { "type": "string", "defaultValue": "[resourceGroup().location]" } }, "outputs": { "eventHubCompatibleConnectionString": { "type": "string", "value": "[concat('Endpoint=sb://', replace(reference(resourceId('Microsoft.Devices/IotHubs', parameters('iotHubName')), '2021-07-01').eventHubEndpoints.events.endpoint, 'https://', 'sb://'), ';SharedAccessKeyName=iothubowner;SharedAccessKey=', listKeys(resourceId('Microsoft.Devices/IotHubs/IotHubKeys', parameters('iotHubName'), 'iothubowner'), '2021-07-01').primaryKey, ';EntityPath=', parameters('iotHubName'))]" } } }
解释一下这个拼接逻辑:
- 用
reference获取IoT Hub内置兼容端点的地址,把https://替换成Event Hub SDK需要的sb://前缀 - 通过
listKeys获取IoT Hub的iothubowner密钥 - 最后加上EntityPath(就是你的IoT Hub名称),拼成完整的连接字符串,直接就能用Event Hub SDK读取设备遥测数据。
场景二:自定义Event Hub兼容路由端点(作为IoT Hub路由目标)
其实就是第一个问题里的自定义Event Hub端点,因为Event Hub本身就和IoT Hub路由兼容,只要按照第一个例子的方式创建,就能作为IoT Hub的路由目标接收数据,并且通过listKeys动态生成连接字符串,完全满足你的自动化需求。
内容的提问来源于stack exchange,提问作者Akhilesh Balakrishnan
相关产品推荐
相关产品推荐

