使用 Azure Resource Manager 模板创建包含主题、订阅和规则的服务总线命名空间Create a Service Bus namespace with topic, subscription, and rule using an Azure Resource Manager template

本文介绍如何使用 Azure Resource Manager 模板,创建包含主题、订阅和规则(筛选器)的服务总线命名空间。This article shows how to use an Azure Resource Manager template that creates a Service Bus namespace with a topic, subscription, and rule (filter). 本文介绍如何指定要部署的资源以及如何定义执行部署时指定的参数。The article explains how to specify which resources are deployed and how to define parameters that are specified when the deployment is executed. 可将此模板用于自己的部署,或自定义此模板以满足要求You can use this template for your own deployments, or customize it to meet your requirements

有关创建模板的详细信息,请参阅 创作 Azure Resource Manager 模板For more information about creating templates, see Authoring Azure Resource Manager templates.

有关 Azure 资源命名约定的实践和模式的详细信息,请参阅 Azure 资源的建议命名约定For more information about practice and patterns on Azure resources naming conventions, see Recommended naming conventions for Azure resources.

有关完整的模板,请参阅 包含主题、订阅和规则的服务总线命名空间 模板。For the complete template, see the Service Bus namespace with topic, subscription, and rule template.

Note

以下 Azure Resource Manager 模板可供下载和部署。The following Azure Resource Manager templates are available for download and deployment.

若要检查最新模板,请访问 Azure 快速启动模板 库并搜索服务总线。To check for the latest templates, visit the Azure Quickstart Templates gallery and search for Service Bus.

要部署什么?What do you deploy?

使用此模板,你将部署包含主题、订阅和规则(筛选器)的服务总线命名空间。With this template, you deploy a Service Bus namespace with topic, subscription, and rule (filter).

服务总线主题和订阅以“发布/订阅”模式提供一对多的通信形式。Service Bus topics and subscriptions provide a one-to-many form of communication, in a publish/subscribe pattern. 使用主题和订阅时,分布式应用程序的组件之间不会直接通信,它们会通过用作中介的主题来交换消息。主题订阅类似于虚拟队列,接收发送至该主题的消息副本。When using topics and subscriptions, components of a distributed application do not communicate directly with each other, instead they exchange messages via topic that acts as an intermediary.A subscription to a topic resembles a virtual queue that receives copies of messages that were sent to the topic. 通过订阅中的筛选器,可以指定发送到主题的哪些消息应该在特定主题订阅中显示。A filter on subscription enables you to specify which messages sent to a topic should appear within a specific topic subscription.

什么是规则(筛选器)?What are rules (filters)?

在许多情况下,必须以不同方式处理具有特定特征的消息。In many scenarios, messages that have specific characteristics must be processed in different ways. 若要启用此自定义处理,可配置订阅以找到具有特定属性的消息,并对这些属性执行修改。To enable this custom processing, you can configure subscriptions to find messages that have specific properties and then perform modifications to those properties. 虽然服务总线订阅可看到发送到主题的所有消息,但你可仅将部分消息复制到虚拟订阅队列。Although Service Bus subscriptions see all messages sent to the topic, you can only copy a subset of those messages to the virtual subscription queue. 可使用订阅筛选器完成此操作。It is accomplished using subscription filters. 若要深入了解规则(筛选器),请参阅规则和操作To learn more about rules (filters), see Rules and actions.

若要自动运行部署,请单击以下按钮:To run the deployment automatically, click the following button:

部署到 AzureDeploy to Azure

parametersParameters

使用 Azure 资源管理器,可以定义在部署模板时想要指定的值的参数。With Azure Resource Manager, define parameters for values you want to specify when the template is deployed. 该模板具有一个名为 Parameters 的部分,其中包含所有参数值。The template includes a section called Parameters that contains all the parameter values. 请根据要部署的项目或要部署到的环境的不同,为那些值定义参数。Define a parameter for those values that vary based on the project you are deploying or based on the environment you are deploying to. 不要为永远保持不变的值定义参数。Do not define parameters for values that always stay the same. 每个参数值可在模板中用来定义所部署的资源。Each parameter value is used in the template to define the resources that are deployed.

模板定义以下参数:The template defines the following parameters:

serviceBusNamespaceNameserviceBusNamespaceName

要创建的服务总线命名空间的名称。The name of the Service Bus namespace to create.

"serviceBusNamespaceName": {
"type": "string"
}

serviceBusTopicNameserviceBusTopicName

在服务总线命名空间中创建的主题的名称。The name of the topic created in the Service Bus namespace.

"serviceBusTopicName": {
"type": "string"
}

serviceBusSubscriptionNameserviceBusSubscriptionName

在服务总线命名空间中创建的订阅的名称。The name of the subscription created in the Service Bus namespace.

"serviceBusSubscriptionName": {
"type": "string"
}

serviceBusRuleNameserviceBusRuleName

在服务总线命名空间中创建的规则(筛选器)的名称。The name of the rule(filter) created in the Service Bus namespace.

   "serviceBusRuleName": {
   "type": "string",
  }

serviceBusApiVersionserviceBusApiVersion

模板的服务总线 API 版本。The Service Bus API version of the template.

"serviceBusApiVersion": { 
       "type": "string", 
       "defaultValue": "2017-04-01", 
       "metadata": { 
           "description": "Service Bus ApiVersion used by the template" 
       }

要部署的资源Resources to deploy

创建类型为 Messaging的包含主题、订阅和规则的标准服务总线命名空间。Creates a standard Service Bus namespace of type Messaging, with topic and subscription and rules.

 "resources": [{
        "apiVersion": "[variables('sbVersion')]",
        "name": "[parameters('serviceBusNamespaceName')]",
        "type": "Microsoft.ServiceBus/Namespaces",
        "location": "[variables('location')]",
        "sku": {
            "name": "Standard",
        },
        "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('serviceBusSubscriptionName')]",
                "type": "Subscriptions",
                "dependsOn": [
                    "[parameters('serviceBusTopicName')]"
                ],
                "properties": {},
                "resources": [{
                    "apiVersion": "[variables('sbVersion')]",
                    "name": "[parameters('serviceBusRuleName')]",
                    "type": "Rules",
                    "dependsOn": [
                        "[parameters('serviceBusSubscriptionName')]"
                    ],
                    "properties": {
                        "filterType": "SqlFilter",
                        "sqlFilter": {
                            "sqlExpression": "StoreName = 'Store1'",
                            "requiresPreprocessing": "false"
                        },
                        "action": {
                            "sqlExpression": "set FilterTag = 'true'"
                        }
                    }
                }]
            }]
        }]
    }]

运行部署的命令Commands to run deployment

要将资源部署到 Azure,必须登录到 Azure 帐户,并且必须使用 Azure 资源管理器模块。To deploy the resources to Azure, you must be signed in to your Azure account and you must use the Azure Resource Manager module. 若要了解配合使用 Azure Resource Manager 和 Azure PowerShell 或 Azure CLI 的相关信息,请参阅:To learn about using Azure Resource Manager with either Azure PowerShell or Azure CLI, see:

以下示例假定帐户中已具有一个指定名称的资源组。The following examples assume you already have a resource group in your account with the specified name.

PowerShellPowerShell

New-AzureResourceGroupDeployment -Name \<deployment-name\> -ResourceGroupName \<resource-group-name\> -TemplateUri <https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json>

Azure CLIAzure CLI

azure config mode arm

azure group deployment create \<my-resource-group\> \<my-deployment-name\> --template-uri <https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/201-servicebus-create-topic-subscription-rule/azuredeploy.json>

后续步骤Next steps

现在,已使用 Azure Resource Manager 创建并部署了资源,请通过查看以下文章了解如何管理这些资源:Now that you've created and deployed resources using Azure Resource Manager, learn how to manage these resources by viewing these articles: