快速入門:使用 Azure 入口網站將 Azure Cache for Redis 事件路由至 web 端點Quickstart: Route Azure Cache for Redis events to web endpoint with the Azure portal

Azure Event Grid 是一項雲端事件服務。Azure Event Grid is an eventing service for the cloud. 在本快速入門中,您將使用 Azure 入口網站建立 Azure Cache for Redis 實例、訂閱該實例的事件、觸發事件,以及查看結果。In this quickstart, you'll use the Azure portal to create an Azure Cache for Redis instance, subscribe to events for that instance, trigger an event, and view the results. 通常,您會將事件傳送至可處理事件資料及採取行動的端點。Typically, you send events to an endpoint that processes the event data and takes actions. 不過,為了簡化此快速入門,您會將事件傳送至 web 應用程式,以收集和顯示訊息。However, to simplify this quickstart, you'll send events to a web app that will collect and display the messages.

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶If you don't have an Azure subscription, create a free account before you begin.

當您完成時,您會看到事件資料已傳送至 web 應用程式。When you're finished, you'll see that the event data has been sent to the web app.

以 JSON 格式調整的 Azure 事件方格檢視器。

建立 Azure Cache for Redis 快取實例Create an Azure Cache for Redis cache instance

  1. 若要建立快取,請登入 Azure 入口網站,然後選取 [建立資源] 。To create a cache, sign in to the Azure portal and select Create a resource.

    [建立資源] 在左側導覽窗格中反白顯示。

  2. 在 [新增] 頁面上選取 [資料庫] ,然後選取 [Azure Cache for Redis] 。On the New page, select Databases and then select Azure Cache for Redis.

    在 [新增] 上,[資料庫] 和 [Azure Cache for Redis] 會反白顯示。

  3. 在 [新的 Redis 快取] 頁面上,設定新快取的設定。On the New Redis Cache page, configure the settings for your new cache.

    設定Setting 建議的值Suggested value 描述Description
    DNS 名稱DNS name 輸入全域唯一名稱。Enter a globally unique name. 快取名稱必須是 1 到 63 個字元的字串,且只能包含數字、字母或連字號。The cache name must be a string between 1 and 63 characters that contains only numbers, letters, or hyphens. 名稱的開頭和結尾必須是數字或字母,且不可包含連續的連字號。The name must start and end with a number or letter, and can't contain consecutive hyphens. 您的快取執行個體 主機名稱 將是 <DNS name>.redis.cache.windows.netYour cache instance's host name will be <DNS name>.redis.cache.windows.net.
    訂用帳戶Subscription 下拉並選取您的訂用帳戶。Drop down and select your subscription. 這個新的 Azure Cache for Redis 執行個體建立所在的訂用帳戶。The subscription under which to create this new Azure Cache for Redis instance.
    資源群組Resource group 下拉並選取資源群組,或選取 [新建] 並輸入新的資源群組名稱。Drop down and select a resource group, or select Create new and enter a new resource group name. 用來建立快取和其他資源的資源群組名稱。Name for the resource group in which to create your cache and other resources. 將所有的應用程式資源放在一個資源群組中,您將可輕鬆地一併管理或刪除這些資源。By putting all your app resources in one resource group, you can easily manage or delete them together.
    位置Location 下拉並選取位置。Drop down and select a location. 選取其他將使用快取的服務附近的區域Select a region near other services that will use your cache.
    定價層Pricing tier 下拉並選取 定價層Drop down and select a Pricing tier. 快取的可用大小、效能和功能取決於定價層。The pricing tier determines the size, performance, and features that are available for the cache. 如需詳細資訊,請參閱 Azure Cache for Redis 概觀For more information, see Azure Cache for Redis Overview.
  4. 選取 [網路] 索引標籤,或按一下頁面底部的 [網路] 按鈕。Select the Networking tab or click the Networking button at the bottom of the page.

  5. 在 [網路功能] 索引標籤中,選取您的連線方法。In the Networking tab, select your connectivity method.

  6. 選取頁面底部的 [下一步:進階] 索引標籤,或按一下頁面底部的 [下一步:進階] 按鈕。Select the Next: Advanced tab or click the Next: Advanced button on the bottom of the page.

  7. 在基本或標準快取執行個體的 [進階] 索引標籤中,如果您想要啟用非 TLS 連接埠,請選取啟用切換。In the Advanced tab for a basic or standard cache instance, select the enable toggle if you want to enable a non-TLS port. 您也可以選擇要使用 Redis 版本 4 或 (預覽) 6。You can also select which Redis version you would like use, either 4 or (PREVIEW) 6.

    Redis 版本 4 或 6。

  8. 在高階快取執行個體的 [進階] 索引標籤中,設定非 TLS 連接埠、叢集和資料持續性的設定。In the Advanced tab for premium cache instance, configure the settings for non-TLS port, clustering, and data persistence. 您也可以選擇要使用 Redis 版本 4 或 (預覽) 6。You can also select which Redis version you would like use, either 4 or (PREVIEW) 6.

  9. 選取頁面底部的 [下一步:標記] 索引標籤,或按一下頁面底部的 [下一步:標記] 按鈕。Select the Next: Tags tab or click the Next: Tags button at the bottom of the page.

  10. 在 [標記] 索引標籤中,如果您想要分類資源,可以選擇性地輸入名稱和值。Optionally, in the Tags tab, enter the name and value if you wish to categorize the resource.

  11. 選取 [檢閱 + 建立]。Select Review + create. 您會移至 [檢閱 + 建立] 索引標籤,其中 Azure 會驗證您的組態。You're taken to the Review + create tab where Azure validates your configuration.

  12. 出現綠色的「通過驗證」訊息之後,請選取 [建立]。After the green Validation passed message appears, select Create.

建立快取需要一些時間。It takes a while for the cache to create. 您可以在 Azure Cache for Redis 的 [概觀] 頁面上監視進度。You can monitor progress on the Azure Cache for Redis Overview page. 當 [狀態] 顯示為 [執行中] 時,表示快取已可供使用。When Status shows as Running, the cache is ready to use.

建立訊息端點Create a message endpoint

在訂閱快取實例的事件之前,讓我們先建立事件訊息的端點。Before subscribing to the events for the cache instance, let's create the endpoint for the event message. 通常,端點會根據事件資料採取動作。Typically, the endpoint takes actions based on the event data. 為了簡化此快速入門,您將部署 預先建立的 web 應用程式 ,以顯示事件訊息。To simplify this quickstart, you'll deploy a pre-built web app that displays the event messages. 已部署的解決方案包含 App Service 方案、App Service Web 應用程式,以及 GitHub 中的原始程式碼。The deployed solution includes an App Service plan, an App Service web app, and source code from GitHub.

  1. 選取 GitHub 讀我檔案中的 [ 部署至 Azure ],將解決方案部署至您的訂用帳戶。Select Deploy to Azure in GitHub README to deploy the solution to your subscription.

[部署至 Azure] 按鈕。

  1. 在 [自訂部署] 頁面上,執行下列步驟:On the Custom deployment page, do the following steps:

    1. 針對 [ 資源群組],選取您在建立快取實例時所建立的資源群組。For Resource group, select the resource group that you created when creating the cache instance. 當您完成本教學課程之後,您可以藉由刪除資源群組,更輕鬆地進行清除。It will be easier for you to clean up after you are done with the tutorial by deleting the resource group.
    2. 在 [網站名稱] 輸入 Web 應用程式的名稱。For Site Name, enter a name for the web app.
    3. 在 [裝載方案名稱] 輸入要用來裝載 Web 應用程式的 App Service 方案名稱。For Hosting plan name, enter a name for the App Service plan to use for hosting the web app.
    4. 選取 [我同意上方所述的條款及條件] 核取方塊。Select the check box for I agree to the terms and conditions stated above.
    5. 選取 [購買] 。Select Purchase.
    設定Setting 建議的值Suggested value 描述Description
    訂用帳戶Subscription 下拉並選取您的訂用帳戶。Drop down and select your subscription. 用來建立此 web 應用程式的訂用帳戶。The subscription under which to create this web app.
    資源群組Resource group 下拉並選取資源群組,或選取 [新建] 並輸入新的資源群組名稱。Drop down and select a resource group, or select Create new and enter a new resource group name. 將所有的應用程式資源放在一個資源群組中,您將可輕鬆地一併管理或刪除這些資源。By putting all your app resources in one resource group, you can easily manage or delete them together.
    網站名稱Site Name 輸入 web 應用程式的名稱。Enter a name for your web app. 這個值不能空白。This value cannot be empty.
    主控方案名稱Hosting plan name 輸入要用來裝載 web 應用程式之 App Service 方案的名稱。Enter a name for the App Service plan to use for hosting the web app. 這個值不能空白。This value cannot be empty.
  2. 選取入口網站中的警示 (鐘圖示),然後選取 [移至資源群組]。Select Alerts (bell icon) in the portal, and then select Go to resource group.

    Azure 入口網站部署通知。

  3. 在 [資源群組] 頁面上的資源清單中,選取您所建立的 Web 應用程式。On the Resource group page, in the list of resources, select the web app that you created. 您也會在此清單中看到 App Service 方案和快取實例。You'll also see the App Service plan and the cache instance in this list.

  4. 在 Web 應用程式的 [App Service] 頁面上,選取瀏覽至網站的 URL。On the App Service page for your web app, select the URL to navigate to the web site. URL 應該是此格式:https://<your-site-name>.azurewebsites.netThe URL should be in this format: https://<your-site-name>.azurewebsites.net.

  5. 確認您看到網站,但其中尚未發佈任何事件。Confirm that you see the site but no events have been posted to it yet.

    空白事件方格檢視器網站。

啟用事件格線資源提供者Enable Event Grid resource provider

如果您先前在 Azure 訂用帳戶中從未使用過事件方格,可能必須註冊事件方格資源提供者。If you haven't previously used Event Grid in your Azure subscription, you may need to register the Event Grid resource provider.

在 Azure 入口網站中:In the Azure portal:

  1. 選取左側功能表上的 [訂用帳戶] 。Select Subscriptions on the left menu.
  2. 選取您用於事件方格的訂用帳戶。Select the subscription you're using for Event Grid.
  3. 在左側功能表的 [設定] 下,選取 [資源提供者] 。On the left menu, under Settings, select Resource providers.
  4. 尋找 Microsoft.EventGridFind Microsoft.EventGrid.
  5. 如果未註冊,請選取 [註冊] 。If not registered, select Register.

可能需要一點時間才能完成註冊。It may take a moment for the registration to finish. 選取 [重新整理] 來更新狀態。Select Refresh to update the status. 當 [狀態] 是 [已註冊] 時,代表已準備好繼續進行。When Status is Registered, you're ready to continue.

訂閱 Azure Cache for Redis 實例Subscribe to the Azure Cache for Redis instance

在此步驟中,您將訂閱一個主題,告知事件方格您想要追蹤的事件,以及要將事件傳送至何處。In this step, you'll subscribe to a topic to tell Event Grid which events you want to track, and where to send the events.

  1. 在入口網站中,流覽至您稍早建立的快取實例。In the portal, navigate to your cache instance that you created earlier.

  2. 在 [ Azure Cache for Redis ] 頁面上,選取左側功能表上的 [ 事件 ]。On the Azure Cache for Redis page, select Events on the left menu.

  3. 選取 [ webhook]。Select Web Hook. 您正在使用端點的 Webhook 將事件傳送至您的檢視器應用程式。You are sending events to your viewer app using a web hook for the endpoint.

    Azure 入口網站事件] 頁面。

  4. 在 [ 建立事件訂 用帳戶] 頁面上,輸入下列內容:On the Create Event Subscription page, enter the following:

    設定Setting 建議的值Suggested value 描述Description
    名稱Name 輸入事件訂閱的名稱。Enter a name for the event subscription. 值的長度必須介於3到64個字元之間。The value must be between 3 and 64 characters long. 它只能包含字母、數位和虛線。It can only contain letters, numbers, and dashes.
    事件種類Event Types 下拉式清單,並選取您想要推送至目的地的事件種類 (s) 。Drop down and select which event type(s) you want to get pushed to your destination. 在本快速入門中,我們將會調整快取實例。For this quickstart, we'll be scaling our cache instance. 修補、調整、匯入和匯出都是可用的選項。Patching, scaling, import and export are the available options.
    端點類型Endpoint Type 選取 [ webhook]。Select Web Hook. 用來接收事件的事件處理常式。Event handler to receive your events.
    端點Endpoint 按一下 [ 選取端點],並輸入您的 web 應用程式 url,並將其新增 api/updates 至首頁 URL (例如: https://cache.azurewebsites.net/api/updates) ,然後選取 [ 確認選取專案]。Click Select an endpoint, and enter the URL of your web app and add api/updates to the home page URL (for example: https://cache.azurewebsites.net/api/updates), and then select Confirm Selection. 這是您稍早建立的 web 應用程式 URL。This is the URL of your web app that you created earlier.
  5. 現在,在 [建立事件訂閱] 頁面上,選取 [建立] 以建立事件訂閱。Now, on the Create Event Subscription page, select Create to create the event subscription.

  6. 再次檢視 Web 應用程式,並注意訂用帳戶的驗證事件已傳送給它。View your web app again, and notice that a subscription validation event has been sent to it. 選取眼睛圖示來展開事件資料。Select the eye icon to expand the event data. Event Grid 會傳送驗證事件,以便端點確認它要接收事件資料。Event Grid sends the validation event so the endpoint can verify that it wants to receive event data. Web 應用程式包含用來驗證訂用帳戶的程式碼。The web app includes code to validate the subscription.

    Azure 事件方格檢視器。

將事件傳送至端點Send an event to your endpoint

現在,讓我們觸發事件以了解 Event Grid 如何將訊息散發至您的端點。Now, let's trigger an event to see how Event Grid distributes the message to your endpoint. 我們將會調整您的 Azure Cache for Redis 實例。We'll be scaling your Azure Cache for Redis instance.

  1. 在 Azure 入口網站中,流覽至您的 Azure Cache for Redis 實例,然後選取左側功能表上的 [ 調整 ]。In the Azure portal, navigate to your Azure Cache for Redis instance and select Scale on the left menu.

  2. 從 [ 規模 ] 頁面中選取所需的定價層,然後按一下 [ 選取]。Select the desired pricing tier from the Scale page and click Select.

    您可以調整具有下列限制的不同定價層:You can scale to a different pricing tier with the following restrictions:

    • 您無法從較高的定價層調整至較低的定價層。You can't scale from a higher pricing tier to a lower pricing tier.
      • 您無法從 進階 快取向下調整至 標準基本 快取。You can't scale from a Premium cache down to a Standard or a Basic cache.
      • 您無法從 標準 快取向下調整到 基本 快取。You can't scale from a Standard cache down to a Basic cache.
    • 您可以從 基本 快取調整到 標準 快取,但您無法同時變更大小。You can scale from a Basic cache to a Standard cache but you can't change the size at the same time. 如果您需要不同的大小,您可以進行後續調整作業,調整到您需要的大小。If you need a different size, you can do a subsequent scaling operation to the desired size.
    • 您無法直接從 基本 快取調整至 進階 快取。You can't scale from a Basic cache directly to a Premium cache. 首先,在單一調整作業中從 基本 調整至 標準,然後在後續的調整作業中從 標準 調整至 進階First, scale from Basic to Standard in one scaling operation, and then from Standard to Premium in a subsequent scaling operation.
    • 您無法從較大的大小向下調整至 C0 (250 MB) 的大小。You can't scale from a larger size down to the C0 (250 MB) size.

    當快取正在調整成新的定價層時,[Azure Redis 快取] 刀鋒視窗中會顯示[正在調整規模] 狀態。While the cache is scaling to the new pricing tier, a Scaling status is displayed in the Azure Cache for Redis blade. 調整完成時,狀態會從 [正在調整] 變更為 [執行中]。When scaling is complete, the status changes from Scaling to Running.

  3. 您已觸發此事件,而 Event Grid 會將訊息傳送至您在訂閱時設定的端點。You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. 訊息為 JSON 格式,且其包含具有一或多個事件的陣列。The message is in the JSON format and it contains an array with one or more events. 在下列範例中,JSON 訊息會包含具有單一事件的陣列。In the following example, the JSON message contains an array with one event. 查看您的 web 應用程式,並注意已收到 ScalingCompleted 事件。View your web app and notice that a ScalingCompleted event was received.

    以 JSON 格式調整的 Azure 事件方格檢視器。

清除資源Clean up resources

如果您打算繼續使用此事件,請勿清除在此快速入門中建立的資源。If you plan to continue working with this event, don't clean up the resources created in this quickstart. 否則,請刪除您在本快速入門中建立的資源。Otherwise, delete the resources you created in this quickstart.

選取資源群組,然後選取 [刪除資源群組]。Select the resource group, and select Delete resource group.

後續步驟Next steps

您現在知道如何建立自訂主題和事件訂閱,深入了解 Event Grid 可協助您:Now that you know how to create custom topics and event subscriptions, learn more about what Event Grid can help you do: