使用 Azure 资源管理器模板创建服务总线命名空间Create a Service Bus namespace by using an Azure Resource Manager template

了解如何部署 Azure 资源管理器模板以创建服务总线命名空间。Learn how to deploy an Azure Resource Manager template to create a Service Bus namespace. 可将此模板用于自己的部署,或自定义此模板以满足要求。You can use this template for your own deployments, or customize it to meet your requirements. 有关创建模板的详细信息,请参阅 Azure 资源管理器文档For more information about creating templates, see Azure Resource Manager documentation.

还可使用以下模板创建服务总线命名空间:The following templates are also available for creating Service Bus namespaces:

备注

本文进行了更新,以便使用新的 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.

如果没有 Azure 订阅,请在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

创建服务总线命名空间Create a service bus namespace

在本快速入门中,使用 Azure 快速启动模板中的现有资源管理器模板In this quickstart, you use an existing Resource Manager template from Azure Quickstart Templates:

{
  "$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"
      }
    },
    "serviceBusSku": {
      "type": "string",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ],
      "defaultValue": "Standard",
      "metadata": {
        "description": "The messaging tier for service Bus namespace"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "variables": {
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "defaultAuthRuleResourceId": "[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": "[parameters('location')]",
      "sku": {
        "name": "[parameters('serviceBusSku')]"
      }
    }
  ],
  "outputs": {
    "NamespaceDefaultConnectionString": {
      "type": "string",
      "value": "[listkeys(variables('defaultAuthRuleResourceId'), variables('sbVersion')).primaryConnectionString]"
    },
    "DefaultSharedAccessPolicyPrimaryKey": {
      "type": "string",
      "value": "[listkeys(variables('defaultAuthRuleResourceId'), variables('sbVersion')).primaryKey]"
    }
  }
}

若要查找更多模板示例,请参阅 Azure 快速启动模板To find more template samples, see Azure Quickstart Templates.

若要通过部署模板创建服务总线命名空间,请执行以下操作:To create a service bus namespace by deploying a template:

  1. 从以下代码块中选择“试一试” ,然后按照说明登录到 Azure PowerShell。Select Try it from the following code block, and then follow the instructions to sign in to the Azure PowerShell.

    $serviceBusNamespaceName = Read-Host -Prompt "Enter a name for the service bus namespace to be created"
    $location = Read-Host -Prompt "Enter the location (i.e. centralus)"
    $resourceGroupName = "${serviceBusNamespaceName}rg"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-servicebus-create-namespace/azuredeploy.json"
    
    New-AzResourceGroup -Name $resourceGroupName -Location $location
    New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -serviceBusNamespaceName $serviceBusNamespaceName
    
    Write-Host "Press [ENTER] to continue ..."
    

    资源组名称是追加了 rg 的服务总线命名空间名称。The resource group name is the service bus namespace name with rg appended.

  2. 选择“复制”以复制 PowerShell 脚本。 Select Copy to copy the PowerShell script.

  3. 右键单击 shell 控制台,然后选择“粘贴” 。Right-click the shell console, and then select Paste.

创建事件中心需要一些时间。It takes a few moments to create an event hub.

验证部署Verify the deployment

若要查看部署的服务总线命名空间,可以从 Azure 门户打开资源组,或者使用以下 Azure PowerShell 脚本。To see the deployed service bus namespace, you can either open the resource group from the Azure portal, or use the following Azure PowerShell script. 如果 PowerShell 仍处于打开状态,则无需复制/运行以下脚本的第一行和第二行。If the PowerShell is still open, you don't need to copy/run the first and second lines of the following script.

$serviceBusNamespaceName = Read-Host -Prompt "Enter the same service bus namespace name used earlier"
$resourceGroupName = "${serviceBusNamespaceName}rg"

Get-AzServiceBusNamespace -ResourceGroupName $resourceGroupName -Name $serviceBusNamespaceName

Write-Host "Press [ENTER] to continue ..."

在本教程中,请使用 Azure PowerShell 来部署模板。Azure PowerShell is used to deploy the template in this tutorial. 如需其他模板部署方法,请参阅:For other template deployment methods, see:

清理资源Clean up resources

不再需要 Azure 资源时,请通过删除资源组来清理部署的资源。When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group. 如果 PowerShell 仍处于打开状态,则无需复制/运行以下脚本的第一行和第二行。If the PowerShell is still open, you don't need to copy/run the first and second lines of the following script.

$serviceBusNamespaceName = Read-Host -Prompt "Enter the same service bus namespace name used earlier"
$resourceGroupName = "${serviceBusNamespaceName}rg"

Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

Write-Host "Press [ENTER] to continue ..."

后续步骤Next steps

在本文中,我们已创建一个服务总线命名空间。In this article, you created a Service Bus namespace. 请参阅其他快速入门,了解如何创建和使用队列、主题/订阅:See the other quickstarts to learn how to create queues, topics/subscriptions, and use them: