快速入門:使用ARM範本建立事件中樞

在本快速入門中,您會使用 Azure Resource Manager 範本(ARM 範本)建立事件中樞。 您可以部署 ARM 範本,以建立事件中樞類型的命名空間,以及一個事件中樞。

必要條件

檢閱範本

本快速入門中使用的範本是來自 Azure 快速入門範本

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "16940368634879422816"
    }
  },
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specifies a project name that is used to generate the Event Hub name and the Namespace name."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specifies the Azure location for all resources."
      }
    },
    "eventHubSku": {
      "type": "string",
      "defaultValue": "Standard",
      "allowedValues": [
        "Basic",
        "Standard"
      ],
      "metadata": {
        "description": "Specifies the messaging tier for Event Hub Namespace."
      }
    }
  },
  "variables": {
    "eventHubNamespaceName": "[format('{0}ns', parameters('projectName'))]",
    "eventHubName": "[parameters('projectName')]"
  },
  "resources": [
    {
      "type": "Microsoft.EventHub/namespaces",
      "apiVersion": "2021-11-01",
      "name": "[variables('eventHubNamespaceName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('eventHubSku')]",
        "tier": "[parameters('eventHubSku')]",
        "capacity": 1
      },
      "properties": {
        "isAutoInflateEnabled": false,
        "maximumThroughputUnits": 0
      }
    },
    {
      "type": "Microsoft.EventHub/namespaces/eventhubs",
      "apiVersion": "2021-11-01",
      "name": "[format('{0}/{1}', variables('eventHubNamespaceName'), variables('eventHubName'))]",
      "properties": {
        "messageRetentionInDays": 7,
        "partitionCount": 1
      },
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubNamespaceName'))]"
      ]
    }
  ]
}

樣本中定義的資源包括:

若要尋找更多範本範例,請參閱 Azure 快速入門範本

部署範本

使用 Azure 入口網站 用戶介面

  1. 如果您的環境符合必要條件,而且您很熟悉 ARM 範本,請選取 [部署至 Azure] 按鈕。 範本會在 Azure 入口網站中開啟。

    Button to deploy the Resource Manager template to Azure.

  2. 選取現有的 資源群組或建立資源群組 ,然後加以選取。

  3. 選取區域

  4. 輸入專案的唯一名稱 此名稱可用來產生命名空間中事件中樞命名空間和事件中樞的名稱。

  5. 選取 [檢閱 + 建立]。

  6. 在 [檢閱 + 建立] 頁面上,選取 [建立]

使用 Azure Cloud Shell

若要使用 Azure Cloud Shell 部署範本:

  1. 從下列程式代碼區塊選取 [ 開啟 Cloud Shell ],然後依照指示登入 Azure Cloud Shell。

    $projectName = Read-Host -Prompt "Enter a project name that is used for generating resource names"
    $location = Read-Host -Prompt "Enter the location (i.e. centralus)"
    $resourceGroupName = "${projectName}rg"
    $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.eventhub/eventhubs-create-namespace-and-eventhub/azuredeploy.json"
    
    New-AzResourceGroup -Name $resourceGroupName -Location $location
    New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName $projectName
    
    Write-Host "Press [ENTER] to continue ..."
    

    建立事件中樞需要幾分鐘的時間。

  2. 選取 [複製] 來複製 PowerShell 指令碼。

  3. 以滑鼠右鍵按一下殼層主控台,然後選取 [貼上]

  4. ENTER 鍵以執行命令。

驗證部署

若要確認部署,您可以從 Azure 入口網站 開啟資源群組,或使用下列 Azure PowerShell 腳本。 如果 Cloud Shell 仍然開啟,您就不需要複製/執行第一行 (讀取主機)。

$projectName = Read-Host -Prompt "Enter the same project name that you used in the last procedure"
$resourceGroupName = "${projectName}rg"
$namespaceName = "${projectName}ns"

Get-AzEventHub -ResourceGroupName $resourceGroupName -Namespace $namespaceName

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

清除資源

不再需要 Azure 資源時,可藉由刪除資源群組來清除您所部署的資源。 如果 Cloud Shell 仍然開啟,您就不需要複製/執行第一行 (讀取主機)。

$projectName = Read-Host -Prompt "Enter the same project name that you used in the last procedure"
$resourceGroupName = "${projectName}rg"

Remove-AzResourceGroup -ResourceGroupName $resourceGroupName

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

下一步

在本文中,您已建立事件中樞命名空間,以及命名空間中的事件中樞。 如需將事件傳送至事件中樞或從事件中樞接收事件的逐步指示,請參閱傳送及接收事件教學課程: