這很重要
Azure Cache for Redis 宣布了所有 SKU 的淘汰時間表。 建議您儘快將現有的 Azure Cache for Redis 執行個體移至 Azure 受控 Redis 。
有關退役的更詳細資訊:
Azure Event Grid 是一項雲端事件服務。 在本快速入門中,您將使用 Azure PowerShell 來訂閱適用於 Redis 的 Azure 快取事件、觸發事件,以及檢視結果。
通常,您會將事件傳送至可處理事件資料及採取行動的端點。 但為簡化本快速入門,您會傳送事件至 Web 應用程式,應用程式會收集並顯示訊息。 完成本快速入門所述的步驟後,您會看到事件資料傳送至 Web 應用程式。
設定
本快速入門需要您執行最新版本的 Azure PowerShell。 如果您需要安裝或升級,請參閱安裝和設定 Azure PowerShell。
登入 Azure
使用 Connect-AzAccount 命令登入 Azure 訂用帳戶,並遵循畫面上的指示進行驗證。
Connect-AzAccount
此範例使用 westus2 ,並將選取範圍儲存在變數中,以便在整個變數中使用。
$location = "westus2"
建立資源群組
事件方格主題會部署為個別的 Azure 資源,且須佈建在 Azure 資源群組下。 資源群組是在其中部署與管理 Azure 資源的邏輯集合。
使用 New-AzResourceGroup 命令來建立資源群組。
下列範例會在 westus2 位置建立名為 gridResourceGroup 的資源群組。
$resourceGroup = "gridResourceGroup"
New-AzResourceGroup -Name $resourceGroup -Location $location
建立 Azure Cache for Redis 執行個體
New-AzRedisCache
-ResourceGroupName <String>
-Name <String>
-Location <String>
[-Size <String>]
[-Sku <String>]
[-RedisConfiguration <Hashtable>]
[-EnableNonSslPort <Boolean>]
[-TenantSettings <Hashtable>]
[-ShardCount <Int32>]
[-MinimumTlsVersion <String>]
[-SubnetId <String>]
[-StaticIP <String>]
[-Tag <Hashtable>]
[-Zone <String[]>]
[-DefaultProfile <IAzureContextContainer>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
如需在 PowerShell 中建立快取執行個體的詳細資訊,請參閱 Azure PowerShell 參考。
建立訊息端點
訂閱主題之前,讓我們建立事件訊息的端點。 端點通常會根據事件資料來採取動作。 若要簡化此快速入門,請部署預先建置的 Web 應用程式以顯示事件訊息。 部署的解決方案包括 App Service 方案、App Service Web 應用程式,以及 GitHub 的原始程式碼。
以 Web 應用程式的唯一名稱取代 <your-site-name>。 Web 應用程式名稱必須是唯一的,因為它是 DNS 項目的一部分。
$sitename="<your-site-name>"
New-AzResourceGroupDeployment `
-ResourceGroupName $resourceGroup `
-TemplateUri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/main/azuredeploy.json" `
-siteName $sitename `
-hostingPlanName viewerhost
部署需要幾分鐘的時間才能完成。 成功部署之後,檢視 Web 應用程式,確定它正在執行。 在網頁瀏覽器中,瀏覽至:https://<your-site-name>.azurewebsites.net
您應該會看到這個網站目前沒有顯示任何訊息。
訂閱 Azure Cache for Redis 事件
在此步驟中,您將訂閱主題,以告知事件方格您要追蹤哪些事件。下列範例會訂閱您建立的快取執行個體,並將 Web 應用程式中的 URL 傳遞為事件通知的端點。 Web 應用程式的端點必須包含的尾碼 /api/updates/。
$cacheId = (Get-AzRedisCache -ResourceGroupName $resourceGroup -Name $cacheName).Id
$endpoint="https://$sitename.azurewebsites.net/api/updates"
New-AzEventGridSubscription `
-EventSubscriptionName <event_subscription_name> `
-Endpoint $endpoint `
-ResourceId $cacheId
再次檢視 Web 應用程式,並注意訂用帳戶的驗證事件已傳送給它。 選取眼睛圖示來展開事件資料。 Event Grid 會傳送驗證事件,以便端點確認它要接收事件資料。 Web 應用程式包含用來驗證訂用帳戶的程式碼。
從 Azure Cache for Redis 觸發事件
現在,讓我們觸發事件以了解 Event Grid 如何將訊息散發至您的端點。
Import-AzRedisCache
[-ResourceGroupName <String>]
-Name <String>
-Files <String[]>
[-Format <String>]
[-Force]
[-PassThru]
[-DefaultProfile <IAzureContextContainer>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
如需在 PowerShell 中匯入的詳細資訊,請參閱 Azure PowerShell 參考。
您已觸發此事件,而 Event Grid 會將訊息傳送至您在訂閱時設定的端點。 檢視您的 Web 應用程式以查看剛傳送的事件。
[{
"id": "e1ceb52d-575c-4ce4-8056-115dec723cff",
"eventType": "Microsoft.Cache.ImportRDBCompleted",
"topic": "/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Cache/Redis/{cache_name}",
"data": {
"name": "ImportRDBCompleted",
"timestamp": "2020-12-10T18:07:54.4937063+00:00",
"status": "Succeeded"
},
"subject": "ImportRDBCompleted",
"dataversion": "1.0",
"metadataVersion": "1",
"eventTime": "2020-12-10T18:07:54.4937063+00:00"
}]
清理資源
如果您打算繼續使用此 Azure Cache for Redis 執行個體和事件訂閱,請勿清除本快速入門中建立的資源。 如果您不打算繼續,請使用下列命令來刪除您在本快速入門中建立的資源。
Remove-AzResourceGroup -Name $resourceGroup
後續步驟
現在您已瞭解如何建立主題和事件訂用帳戶,請深入瞭解 Azure Cache for Redis 事件,以及事件方格可協助您執行的動作: