使用Azure ARM模板使用Sql Filter创建服务总线主题订阅?

时间:2016-03-28 19:48:08

标签: azure azure-servicebus-topics azure-resource-manager

我已经能够弄清楚如何设置Azure ARM模板来创建/管理Azure服务总线命名空间,主题和订阅以接收所有消息。但是,在ARM Tempates上仍然非常缺乏Microsoft文档,我无法弄清楚如何在模板中为使用.NET SDK管理的订阅定义SqlFilter。

有谁知道如何将Sql过滤器添加到ARM模板中的服务总线主题订阅中?

这是我用于创建不带Sql过滤器的服务总线主题和订阅的ARM模板的链接:

https://github.com/crpietschmann/azure-quickstart-templates/blob/101-servicebus-topic-subscription/101-servicebus-topic-subscription/azuredeploy.json

此外,这里是我所指的ARM模板的来源:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Namespace"
      }
    },
    "serviceBusTopicName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Topic"
      }
    },
    "serviceBusTopicSubscriptionName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus Topic Subscription"
      }
    }
  },
  "variables": {
    "sbVersion": "2015-08-01"
  },
  "resources": [
    {
      "apiVersion": "[variables('sbVersion')]",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[resourceGroup().location]",
      "properties": {
      },
      "resources": [
        {
          "apiVersion": "[variables('sbVersion')]",
          "name": "[parameters('serviceBusTopicName')]",
          "type": "Topics",
          "dependsOn": [
            "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
          ],
          "properties": {
            "path": "[parameters('serviceBusTopicName')]"
          },
          "resources": [
            {
              "apiVersion": "[variables('sbVersion')]",
              "name": "[parameters('serviceBusTopicSubscriptionName')]",
              "type": "Subscriptions",
              "dependsOn": [
                "[parameters('serviceBusTopicName')]"
              ],
              "properties": {
              },
              "resources": [
              ]
            }
          ]
        }
      ]
    }
  ],
  "outputs": {
  }
}

6 个答案:

答案 0 :(得分:3)

Sql Filter应位于规则内,因此我们应在Service Bus主题订阅中创建规则。例如:

      "resources": [
        {
          "apiVersion": "[variables('sbVersion')]",
          "name": "[parameters('serviceBusTopicSubscriptionName')]",
          "type": "Subscriptions",
          "dependsOn": [
            "[parameters('serviceBusTopicName')]"
          ],
          "properties": {
          },
          "resources": [
            {
              "apiVersion": "[variables('sbVersion')]",
              "name": "[parameters('serviceBusTopicSubscriptionRuleName')]",
              "type": "Rules",
              "dependsOn": [
                "[parameters('serviceBusTopicSubscriptionName')]"
              ],
              "properties": {
              },
              "resources": [
              ]
            }
          ]
        }
      ]

我尝试部署此模板,但是出现以下错误:

New-AzureRmResourceGroupDeployment : InvalidTemplate: Deployment template validation failed: 'The template resource 'Microsoft.ServiceBus/namespaces/<serviceBusNamespaceName>/Topics/<serviceBusTopicName>/Subscriptions/<serviceBusTopicSubscriptionName>' cannot reference itself. Please see http://aka.ms/arm-template-expressions/#reference for usage details.'.
At line:1 char:1
+ New-AzureRmResourceGroupDeployment -Name ServiceBusTest -ResourceGrou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmResourceGroupDeployment], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupDeploymentCommand

从错误消息“'模板资源无法引用自身”,我猜测在ARM模板中尚未实现为主题订阅创建Sql过滤器。

经过多次挖掘后,我认为资源管理器尚无法管理主题订阅规则。这是我尝试过的事情。

  1. 我使用this PowerShell script创建一个带有规则的主题订阅。我通过在规则$RuleDescription.Name = "rule1"中添加名称对脚本进行了一些修改。

  2. 已成功创建主题订阅,我可以使用以下PowerShell命令获取主题订阅。

    Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS `
                       -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions `
                       -ResourceName <namespace>/<topic>/<subscription> `
                       -ApiVersion 2014-09-01
    
  3. 当我尝试使用类似的PowerShell命令获取主题订阅规则时:

    Get-AzureRmResource -ResourceGroupName Default-ServiceBus-EastUS `
                 -ResourceType Microsoft.ServiceBus/namespaces/topics/Subscriptions/Rules `
                 -ResourceName <namespace>/<topic>/<subscription>/rule1 `
                 -ApiVersion 2014-09-01
    

    我收到以下错误:

    No HTTP resource was found that matches the request URI
    'https://sbgm.windows.net/subscriptions/<subscriptionid>/resourceGroups/Default-ServiceBus-EastUS/providers/Microsoft.ServiceBus/namespaces/<namespace>/topics/<topic>/Subscriptions/<subscription>/Rules/rule1?api-version=2014-09-01'
    
  4. 但是,如果我使用$NamespaceManager.GetRules($TopicPath,$Name),我会成功获得上述规则。这意味着规则已成功创建。

答案 1 :(得分:3)

只需在订阅资源中添加以下内容即可创建SQL过滤器和操作:

JserseyConfig

答案 2 :(得分:2)

目前,ARM模板不支持创建/管理Azure Service Bus主题订阅过滤器。

答案 3 :(得分:1)

现在可以按照以下快速启动模板进行操作,该模板说明了添加SQL过滤器:

https://github.com/Azure/azure-quickstart-templates/blob/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json

此外,如果您希望通过ARM添加关联过滤器,我可以通过设置function saveMerchant(merchant_id, access_token, locations) { return new Promise(function (resolve, reject) { const merchantRef = database.ref('merchants').child(merchant_id); const location_ids = locations.map(location => location.id); merchantRef.update({ access_token, location_ids, }, function (error) { if (error) return reject(error); resolve(); }); }); } 资源来实现此目的:

Rules

答案 4 :(得分:0)

最近更改了添加Sql过滤器的Subscription语法。

<snip>
"apiVersion": "2017-04-01",
"name": "[parameters('serviceBusSubscriptionName')]",
<snip>
"resources": [
    {
      "apiVersion": "2017-04-01",
      "name": "[parameters('serviceBusRuleName')]",
      "type": "Rules",
      "dependsOn": [
        "[parameters('serviceBusSubscriptionName')]"
      ],
      "properties": {
        "filterType": "SqlFilter",
        "sqlFilter": {
          "sqlExpression": "FilterTag = 'true'",
          "requiresPreprocessing": "false"
        },
        "action": {
          "sqlExpression": "set FilterTag = 'true'"
        }
      }
    }
]

您可以在此ARM模板中找到最新示例:
https://github.com/Azure/azure-quickstart-templates/blob/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json

答案 5 :(得分:0)

使用Service Bus Explorer应用程序。 如果您已经知道要创建的SQL筛选器,建议您下载并使用此应用程序。从here

下载
  1. 您只需要连接到服务总线,即可向订阅添加规则。
  2. 然后,转到Azure门户并检索该特定服务总线的ARM模板。
  3. 您将能够看到如何构建SQL过滤器。

这是向订阅中添加规则的方法:

enter image description here

这是您查看创建的规则的方式:

enter image description here

使用Service Bus Explorer应用程序非常简单。 由于它是用户交互的,因此您始终可以配置服务总线,然后转到Azure门户以检索ARM模板。