3

我已经能够弄清楚如何设置 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": {
  }
}
4

6 回答 6

3

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. 我使用这个 PowerShell 脚本来创建一个带有规则的主题订阅。我通过在规则中添加名称对脚本进行了一些修改,$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),我确实成功地获得了上述规则。这意味着规则已成功创建。

于 2016-03-29T03:25:01.593 回答
3

只需将以下内容添加到您的订阅资源中即可创建 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'" } } }]

于 2016-12-27T20:53:50.317 回答
2

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

于 2016-04-22T21:39:37.493 回答
1

现在可以根据以下说明添加 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')]"
        }
      }
    }
  ]
于 2016-11-07T21:25:34.007 回答
0

使用服务总线资源管理器应用。如果您已经知道要创建什么 SQL 过滤器,我建议您下载并使用此应用程序。从这里下载

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

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

在此处输入图像描述

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

在此处输入图像描述

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

于 2018-09-05T10:14:33.200 回答
0

添加 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

于 2017-12-20T10:17:36.130 回答