使用 Azure Resource Manager 模板创建服务总线资源Create Service Bus resources using Azure Resource Manager templates

本文介绍如何使用 Azure Resource Manager 模板、PowerShell 和服务总线资源提供程序创建和部署服务总线资源。This article describes how to create and deploy Service Bus resources using Azure Resource Manager templates, PowerShell, and the Service Bus resource provider.

Azure Resource Manager 模板可帮助你定义要为解决方案部署的资源,以及指定可用于为不同环境输入值的参数和变量。Azure Resource Manager templates help you define the resources to deploy for a solution, and to specify parameters and variables that enable you to input values for different environments. 此模板以 JSON 编写并包含可用于为部署构造值的表达式。The template is written in JSON and consists of expressions that you can use to construct values for your deployment. 有关编写 Azure Resource Manager 模板的详细信息以及关于模板格式的讨论,请参阅 Azure Resource Manager 模板的结构和语法For detailed information about writing Azure Resource Manager templates, and a discussion of the template format, see structure and syntax of Azure Resource Manager templates.

备注

本文中的示例演示如何使用 Azure Resource Manager 来创建服务总线命名空间和消息实体(队列)。The examples in this article show how to use Azure Resource Manager to create a Service Bus namespace and messaging entity (queue). 有关其他模板示例,请访问 Azure 快速启动模板库并搜索“服务总线” 。For other template examples, visit the Azure Quickstart Templates gallery and search for Service Bus.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

服务总线 Resource Manager 模板Service Bus Resource Manager templates

这些服务总线 Azure Resource Manager 模板可供下载和部署。These Service Bus Azure Resource Manager templates are available for download and deployment. 单击以下链接可获得有关每个链接的详细信息,其中包含指向 GitHub 上的模板的链接:Click the following links for details about each one, with links to the templates on GitHub:

使用 PowerShell 进行部署Deploy with PowerShell

以下过程描述如何使用 PowerShell 部署 Azure 资源管理器模板以创建“标准”层服务总线命名空间和该命名空间中的一个队列。The following procedure describes how to use PowerShell to deploy an Azure Resource Manager template that creates a Standard tier Service Bus namespace, and a queue within that namespace. 本示例基于 创建包含队列的服务总线命名空间 模板。This example is based on the Create a Service Bus namespace with queue template. 大概的工作流如下所示:The approximate workflow is as follows:

  1. 安装 PowerShell。Install PowerShell.
  2. 创建模板和(可选)参数文件。Create the template and (optionally) a parameter file.
  3. 在 PowerShell 中,登录到 Azure 帐户。In PowerShell, log in to your Azure account.
  4. 创建新资源组(如果不存在)。Create a new resource group if one does not exist.
  5. 测试部署。Test the deployment.
  6. 如果需要,设置部署模式。If desired, set the deployment mode.
  7. 部署模板。Deploy the template.

有关部署 Azure 资源管理器模板的完整信息,请参阅使用 Azure 资源管理器模板部署资源For complete information about deploying Azure Resource Manager templates, see Deploy resources with Azure Resource Manager templates.

安装 PowerShellInstall PowerShell

按照 Azure PowerShell 入门中的说明安装 Azure PowerShell。Install Azure PowerShell by following the instructions in Getting started with Azure PowerShell.

创建模板Create a template

从 GitHub 克隆存储库或复制 201-servicebus-create-queue 模板:Clone the repository or copy the 201-servicebus-create-queue template from GitHub:

{
  "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus namespace"
      }
    },
    "serviceBusQueueName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Queue"
      }
    }
  },
  "variables": {
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.ServiceBus/namespaces/authorizationRules', parameters('serviceBusNamespaceName'), variables('defaultSASKeyName'))]",
    "sbVersion": "2017-04-01"
  },
  "resources": [
    {
      "apiVersion": "2017-04-01",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/Namespaces",
      "location": "[resourceGroup().location]",
      "sku": {
        "name": "Standard"
      },
      "properties": {},
      "resources": [
        {
          "apiVersion": "2017-04-01",
          "name": "[parameters('serviceBusQueueName')]",
          "type": "Queues",
          "dependsOn": [
            "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
          ],
          "properties": {
            "lockDuration": "PT5M",
            "maxSizeInMegabytes": "1024",
            "requiresDuplicateDetection": "false",
            "requiresSession": "false",
            "defaultMessageTimeToLive": "P10675199DT2H48M5.4775807S",
            "deadLetteringOnMessageExpiration": "false",
            "duplicateDetectionHistoryTimeWindow": "PT10M",
            "maxDeliveryCount": "10",
            "autoDeleteOnIdle": "P10675199DT2H48M5.4775807S",
            "enablePartitioning": "false",
            "enableExpress": "false"
          }
        }
      ]
    }
  ],
  "outputs": {
    "NamespaceConnectionString": {
      "type": "string",
      "value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryConnectionString]"
    },
    "SharedAccessPolicyPrimaryKey": {
      "type": "string",
      "value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryKey]"
    }
  }
}

创建参数文件(可选)Create a parameters file (optional)

若要使用可选参数文件,请复制 201-servicebus-create-queue 文件。To use an optional parameters file, copy the 201-servicebus-create-queue file. serviceBusNamespaceName 的值替换为要在此部署中创建的服务总线命名空间的名称,并将 serviceBusQueueName 的值替换为要创建的队列的名称。Replace the value of serviceBusNamespaceName with the name of the Service Bus namespace you want to create in this deployment, and replace the value of serviceBusQueueName with the name of the queue you want to create.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "serviceBusNamespaceName": {
            "value": "<myNamespaceName>"
        },
        "serviceBusQueueName": {
            "value": "<myQueueName>"
        },
        "serviceBusApiVersion": {
            "value": "2017-04-01"
        }
    }
}

登录到 Azure 并设置 Azure 订阅Log in to Azure and set the Azure subscription

在 PowerShell 提示符下,运行以下命令:From a PowerShell prompt, run the following command:

Connect-AzAccount -Environment AzureChinaCloud

系统会提示你登录到 Azure 帐户。You are prompted to log on to your Azure account. 登录后,运行以下命令以查看可用订阅:After logging on, run the following command to view your available subscriptions:

Get-AzSubscription

此命令返回可用 Azure 订阅的列表。This command returns a list of available Azure subscriptions. 通过运行以下命令为当前会话选择订阅。Choose a subscription for the current session by running the following command. <YourSubscriptionId> 替换为要使用的 Azure 订阅的 GUID:Replace <YourSubscriptionId> with the GUID for the Azure subscription you want to use:

Set-AzContext -SubscriptionID <YourSubscriptionId>

设置资源组Set the resource group

如果没有现有的资源组,请使用 New-AzResourceGroup 命令创建新的资源组。If you do not have an existing resource group, create a new resource group with the New-AzResourceGroup command. 提供资源组的名称,以及要使用的位置。Provide the name of the resource group and location you want to use. 例如:For example:

New-AzResourceGroup -Name MyDemoRG -Location "China North"

如果成功,则会显示新的资源组的摘要。If successful, a summary of the new resource group is displayed.

ResourceGroupName : MyDemoRG
Location          : chinanorth
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/<GUID>/resourceGroups/MyDemoRG

测试部署Test the deployment

通过运行 Test-AzResourceGroupDeployment cmdlet 验证部署。Validate your deployment by running the Test-AzResourceGroupDeployment cmdlet. 测试部署时,请提供与执行部署时所提供的完全相同的参数。When testing the deployment, provide parameters exactly as you would when executing the deployment.

Test-AzResourceGroupDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

创建部署Create the deployment

若要创建新部署,请运行 New-AzResourceGroupDeployment cmdlet,并在出现提示时提供必需的参数。To create the new deployment, run the New-AzResourceGroupDeployment cmdlet, and provide the necessary parameters when prompted. 参数包括部署的名称、资源组的名称,以及模板文件的路径或 URL。The parameters include a name for your deployment, the name of your resource group, and the path or URL to the template file. 如果未指定 Mode 参数,则将使用默认值 Incremental。 If the Mode parameter is not specified, the default value of Incremental is used. 有关详细信息,请参阅 增量部署和完整部署For more information, see Incremental and complete deployments.

以下命令会提示在 PowerShell 窗口中输入三个参数:The following command prompts you for the three parameters in the PowerShell window:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

若要改为指定参数文件,请使用以下命令:To specify a parameters file instead, use the following command:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json -TemplateParameterFile <path to parameters file>\azuredeploy.parameters.json

运行部署 cmdlet 时,还可以使用内联参数。You can also use inline parameters when you run the deployment cmdlet. 该命令如下所示:The command is as follows:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json -parameterName "parameterValue"

若要运行完整部署,请将 Mode 参数设置为 Complete: To run a complete deployment, set the Mode parameter to Complete:

New-AzResourceGroupDeployment -Name MyDemoDeployment -Mode Complete -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

验证部署Verify the deployment

如果资源已成功部署,会在 PowerShell 窗口中显示部署的摘要:If the resources are deployed successfully, a summary of the deployment is displayed in the PowerShell window:

DeploymentName    : MyDemoDeployment
ResourceGroupName : MyDemoRG
ProvisioningState : Succeeded
Timestamp         : 4/19/2017 10:38:30 PM
Mode              : Incremental
TemplateLink      :
Parameters        :
                    Name             Type                       Value
                    ===============  =========================  ==========
                    serviceBusNamespaceName  String             <namespaceName>
                    serviceBusQueueName  String                 <queueName>
                    serviceBusApiVersion  String                2017-04-01

后续步骤Next steps

现在已了解用于部署 Azure Resource Manager 模板的基本工作流和命令。You've now seen the basic workflow and commands for deploying an Azure Resource Manager template. 有关更多详细信息,请访问以下链接:For more detailed information, visit the following links: