【问题标题】:Use Azure ARM Template to create Service Bus Topic Subscription with Sql Filter?使用 Azure ARM 模板通过 Sql 筛选器创建服务总线主题订阅?
【发布时间】:2016-07-16 03:51:16
【问题描述】:

我已经能够弄清楚如何设置一个 Azure ARM 模板来创建/管理 Azure 服务总线命名空间、主题和订阅以接收所有消息。但是,Microsoft 文档在 ARM Tempates 上仍然非常缺乏,我无法弄清楚如何在您可以使用 .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": {
  }
}

【问题讨论】:

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


    【解决方案1】:

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

    ,"resources": [{ "apiVersion": "[variables('sbVersion')]", "name": "$Default", "type": "Rules", "dependsOn": ["[parameters('serviceBusSubscriptionName')]"], "properties": { "filterType": "SqlFilter", "sqlFilter": { "sqlExpression": "1=1", "requiresPreprocessing": false }, "action": { "sqlExpression": "set something = 'something'" } } }]

    【讨论】:

      【解决方案2】:

      Sql 过滤器应该在规则中,因此我们应该在服务总线主题订阅中创建规则。例如:

            "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
      

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

      经过更多的挖掘,我相信主题订阅规则还不能由资源管理器管理。这是我尝试过的东西。

      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),我确实成功获得了上述规则。即表示规则创建成功。

      【讨论】:

        【解决方案3】:

        目前,ARM 模板不支持创建/管理 Azure 服务总线主题订阅筛选器。

        【讨论】:

        • 这有什么更新吗?我正在寻找相同的功能并很想知道现在是否包含此功能
        • @MaheshJasti 宣布服务总线团队正在努力将 ARM 支持添加到服务总线。它也应该在今年晚些时候推出,所以很快我们终于获得了对服务总线 ARM 模板的支持!
        【解决方案4】:

        现在可以根据以下说明添加 SQL 过滤器的快速入门模板:

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

        此外,如果您希望通过 ARM 添加关联过滤器,我可以通过如下设置 Rules 资源来实现:

         "resources": [
            {
              "apiVersion": "[variables('sbVersion')]",
              "name": "$Default",
              "type": "Rules",
              "dependsOn": [
                "[parameters('serviceBusSubscriptionName')]"
              ],
              "properties": {
                "filter": {
                  "correlationId": "[parameters('correlationId')]"
                }
              }
            }
          ]
        

        【讨论】:

          【解决方案5】:

          添加 Sql 过滤器的订阅语法最近发生了变化。

          <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

          【讨论】:

            【解决方案6】:

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

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

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

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

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

            【讨论】:

            • 谢谢,但您的回答对自动化或从 ARM 模板进行部署没有帮助。
            • 从某种意义上说,您可以从此服务总线资源管理器应用程序创建规则,然后转到 Azure 门户以检索生成的 ARM 模板,这很有帮助。在该 ARM 模板中,您将能够看到如何在 ARM 模板中生成 Sql Filter to a Service Bus Topic Subscription,并将其用作创建您自己的 Sql Filters 的指南
            猜你喜欢
            • 1970-01-01
            • 2018-03-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多