Share via


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

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

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

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

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

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

用戶端故障轉移考慮

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

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

下列範例程式代碼是一個簡單的 .NET 發行者,會先嘗試發佈至主要主題。 如果失敗,則會故障轉移次要主題。 不論是哪一種情況,它也會藉由在 上執行 GET 來檢查另一個主題的健康情況 https://<topic-name>.<topic-region>.eventgrid.azure.net/api/healthAPI。 當 /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。 一旦再次健康,容錯回復到更接近的數據中心是安全的。

下一步