共用方式為


Azure 事件方格中的用戶端故障轉移實作

災害復原通常牽涉到建立備份資源,以防止區域狀況不良時中斷。 在此流程期間,您的工作負載中將需要 Azure 事件方格資源的主要和次要區域。

有不同方式可從嚴重遺失的應用程式功能中復原。 在本文中,我們將說明您需要遵循的檢查清單,以便準備客戶因資源或區域狀況不佳而從故障中恢復。

事件方格支援伺服器端的手動和自動異地災害復原(GeoDR)。 如果您想要更充分地控管容錯移轉程序,仍可實作用戶端災害復原邏輯。 如需有關自動 GeoDR 的詳細資訊,請參閱 Azure 事件方格中的伺服器端異地災害復原

下表說明事件方格中的用戶端容錯移轉和異地災害復原支援。

事件方格資源 用戶端容錯移轉支援 異地災害復原 (GeoDR) 支援
自訂主題 支持 跨地理位置/區域
系統主題 不支援 自動啟用
網域 支持 跨地理位置/區域
合作夥伴命名空間 支持 不支援
命名空間 支持 不支援

用戶端故障轉移考慮機制

  1. 建立及設定 主要 事件方格資源。
  2. 建立及設定 次要 事件方格資源。
  3. 請記住,這兩個資源必須啟用相同的組態、子資源與功能。
  4. 事件方格資源必須裝載於不同的區域。
  5. 如果事件網格資源具有相依資源,例如用於死信的儲存資源,您應該使用次要事件網格資源中使用的相同區域。
  6. 請確定您的端點會定期進行測試,以提供復原方案資源已就緒且正常運作的保固。

自定義主題的基本用戶端故障轉移實作範例

下列範例程式碼是一個簡單的 .NET 發行者,它會先嘗試發佈至您的主要主題。 如果不成功,它將切換到次要主題。 不論是哪一種情況,它也會藉由對https://<topic-name>.<topic-region>.eventgrid.azure.net/api/health執行GET來檢查另一個主題的健康API。 當 /api/health 端點上建立 GET 時,狀況良好的主題應該一律回應 200 OK

備註

下列範例程式代碼僅供示範之用,不適用於生產環境。

using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using Azure;
using Azure.Messaging.EventGrid;

namespace EventGridFailoverPublisher
{
    // This captures the "Data" portion of an EventGridEvent on a custom topic
    class FailoverEventData
    {
        public string TestStatus { get; set; }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            // TODO: Enter the endpoint each topic. You can find this topic endpoint value
            // in the "Overview" section in the "Event Grid topics" page in Azure Portal..
            string primaryTopic = "https://<primary-topic-name>.<primary-topic-region>.eventgrid.azure.net/api/events";
            string secondaryTopic = "https://<secondary-topic-name>.<secondary-topic-region>.eventgrid.azure.net/api/events";

            // TODO: Enter topic key for each topic. You can find this in the "Access Keys" section in the
            // "Event Grid topics" page in Azure Portal.
            string primaryTopicKey = "<your-primary-topic-key>";
            string secondaryTopicKey = "<your-secondary-topic-key>";

            Uri primaryTopicUri = new Uri(primaryTopic);
            Uri secondaryTopicUri = new Uri(secondaryTopic);

            Uri primaryTopicHealthProbe = new Uri($"https://{primaryTopicUri.Host}/api/health");
            Uri secondaryTopicHealthProbe = new Uri($"https://{secondaryTopicUri.Host}/api/health");

            var httpClient = new HttpClient();

            try
            {
                var client = new EventGridPublisherClient(primaryTopicUri, new AzureKeyCredential(primaryTopicKey));

                await client.SendEventsAsync(GetEventsList());
                Console.Write("Published events to primary Event Grid topic.");

                HttpResponseMessage health = httpClient.GetAsync(secondaryTopicHealthProbe).Result;
                Console.Write("\n\nSecondary Topic health " + health);
            }
            catch (RequestFailedException ex)
            {
                var client = new EventGridPublisherClient(secondaryTopicUri, new AzureKeyCredential(secondaryTopicKey));

                await client.SendEventsAsync(GetEventsList());
                Console.Write("Published events to secondary Event Grid topic. Reason for primary topic failure:\n\n" + ex);

                HttpResponseMessage health = await httpClient.GetAsync(primaryTopicHealthProbe);
                Console.WriteLine($"Primary Topic health {health}");
            }

            Console.ReadLine();
        }

        static IList<EventGridEvent> GetEventsList()
        {
            List<EventGridEvent> eventsList = new List<EventGridEvent>();

            for (int i = 0; i < 5; i++)
            {
                eventsList.Add(new EventGridEvent(
                    subject: "test" + i,
                    eventType: "Contoso.Failover.Test",
                    dataVersion: "2.0",
                    data: new FailoverEventData
                    {
                        TestStatus = "success"
                    }));
            }

            return eventsList;
        }
    }
}

試試看

既然您已備妥所有元件,您可以測試故障轉移實作。

若要確保您的故障轉移正常運作,您可以修改主要主題鍵中的幾個字元,使其變得無效。 請再試一次執行發行者。 當您查看用戶端時,下列範例事件會繼續流經事件方格,您會看到它們現在正透過次要主題發佈。

可能的擴充功能

有許多方法可根據您的需求擴充此範例。 針對高容量使用場景,您可以定期獨立檢查主題的健康狀態 API。 如此一來,如果主題不再可用,您就不需要每次發佈時都檢查。 一旦知道主題不健康,您就可以改為發佈至次要主題。

同樣地,您可能想要根據特定需求實作容錯回復邏輯。 如果發佈至最接近的數據中心對於降低延遲至關重要,您可以定期探查已故障轉移之主題的健康情況 API。 一旦系統恢復正常,就可以安全地切回距離較近的數據中心。

後續步驟