我正在使用最新的Azure.Data.Tablesnuget 包版本12.3.0連接到 ASP.NET Core C# 應用程式中的 Azure 表存盤。
如果主要區域發生故障,我的應用程式需要故障轉移到次要區域以進行讀取。
目前TableServiceClient在 Startup.cs 中的設定如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new TableServiceClient(new Uri("PrimaryRegionConnectionURL"), new DefaultAzureCredential()));
}
如何TableServiceClient使用指向次要區域的實體更新當前實體?有沒有更好的方法來實作這種故障轉移?
澄清一下:我知道客戶端不支持故障轉移,團隊已經創建了一張票,以便將來查看此功能。我意識到我需要一個新的TableServiceClient.
我只是不確定如何將在啟動時創建的實體替換為出現故障時指向輔助實體的新實體。
這是消耗 TableServiceClient
public class TableRepository : ITableStorageRepository
{
readonly TableServiceClient _serviceClient;
public TableRepository(TableServiceClient serviceClient)
{
_serviceClient = serviceClient;
}
public async Task<ICollection<T>> GetPartitionEntities<T>(string partitionKey, string tableName)
where T : class, ITableEntity, new()
{
var listOfEntities = new List<T>();
var tableClient = _serviceClient.GetTableClient(tableName);
var queryResults = tableClient.QueryAsync<T>(filter => filter.PartitionKey == partitionKey);
await foreach (var row in queryResults)
{
listOfEntities.Add(row);
}
return listOfEntities;
}
}
uj5u.com熱心網友回復:
當前不支持使用相同客戶端的自動故障轉移;要使用次要區域,TableServiceClient需要一個單獨的區域。此處提供更多背景關系:https : //github.com/Azure/azure-sdk-for-net/issues/25456
正在此處跟蹤添加支持的作業:https : //github.com/Azure/azure-sdk-for-net/issues/25710
uj5u.com熱心網友回復:
不確定這是否是實作它的最佳方式,但考慮到我必須自己處理在主要和次要端點之間切換的邏輯,我會這樣做。
首先,我會創建兩個實體TableServiceClient- 一個用于主要,另一個用于次要。
public void ConfigureServices(IServiceCollection services)
{
Dictionary<string, TableServiceClient> tableServiceClients = new Dictionary()
{
"Primary", new TableServiceClient(new Uri("PrimaryRegionConnectionURL"), new DefaultAzureCredential()),
"Secondary", new TableServiceClient(new Uri("SecondaryRegionConnectionURL"), new DefaultAzureCredential())
}
services.AddSingleton(tableServiceClients);
}
接下來,我會在一個單獨的函式中提取用于獲取物體的邏輯,并將客戶端傳遞給該函式(讓我們稱之為GetPartitionEntitiesImpl)。
然后在GetPartitionEntities方法中,我會嘗試從主要端點獲取物體并捕獲例外。如果例外表明主要端點失敗,我會GetPartitionEntitiesImpl再次呼叫函式并嘗試從次要端點獲取物體。
public class TableRepository : ITableStorageRepository
{
readonly TableServiceClient _primaryServiceClient, _secondaryServiceClient;
public TableRepository(Dictionary<string, TableServiceClient> tableServiceClients)
{
_primaryServiceClient = tableServiceClients["Primary"];
_secondaryServiceClient = tableServiceClients["Secondary"];
}
public async Task<ICollection<T>> GetPartitionEntities<T>(string partitionKey, string tableName)
where T : class, ITableEntity, new()
{
try
{
return await GetPartitionEntitiesImpl(_primaryServiceClient, partitionKey, tableName);
}
catch (Exception exception)
{
//Check if there is a need for failover
if (shouldTrySecondaryEndpoint)
{
return await GetPartitionEntitiesImpl(_secondaryServiceClient, partitionKey, tableName);
}
}
}
private async Task<ICollection<T>> GetPartitionEntitiesImpl<T>(TableServiceClient serviceClient, string partitionKey, string tableName)
where T : class, ITableEntity, new()
{
var listOfEntities = new List<T>();
var tableClient = serviceClient.GetTableClient(tableName);
var queryResults = tableClient.QueryAsync<T>(filter => filter.PartitionKey == partitionKey);
await foreach (var row in queryResults)
{
listOfEntities.Add(row);
}
return listOfEntities;
}
}
此外,請查看舊版 Azure 存盤 SDK(9.x 版)的代碼,了解有關在主要和次要端點之間切換的邏輯。該 SDK 可以很好地處理這種情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382148.html
標籤:C# 天蓝色 asp.net核心 天蓝色表存储 自动故障转移
