在實現API Gateway過程中,另外一個需要考慮的問題就是部分失敗。這個問題發生在分布式系統中當一個服務調用另外一個服務超時或者不可用的情況。API Gateway不應該被阻斷並處於無限期等待下游服務的狀態。但是,如何處理這種失敗依賴於特定的場景和具體服務。如果是產品信息服務無響應,那么API Gateway就應該給客戶端返回一個錯誤。
Ocelot 是一個使用.NET Core平台上的一個API Gateway,最近我在參與這個項目的開發,開發完成第一個就是使用Polly 處理部分失敗問題。各位同學可能對Polly這個項目不熟悉,先簡單介紹下,Polly是.NET基金會下的一個開源項目,Polly記錄那些超過預設定的極限值的調用。它實現了 circuit break模 式,使得可以將客戶端從無響應服務的無盡等待中停止。如果一個服務的錯誤率超過預設值,Polly 將中斷服務,並且在一段時間內所有請求立刻失效,Polly 可以為請求失敗定義一個fallback操作,例如讀取緩存或者返回默認值,有時候我們需要調用其他API的時候出現暫時連接不通超時的情況,那這時候也可以通過Polly進行Retry,具體信息參考 http://www.thepollyproject.org/2016/10/25/polly-5-0-a-wider-resilience-framework/ 。
Ocelot從實現上來說就是一系列的中間件組合,在HTTP請求到達Ocelot,經過一系列的中間件的處理轉發到下游的服務,其中負責調用下游服務的中間件是HttpRequestBuilderMiddleware,通過調用HttpClient請求下游的HTTP服務,我們這里就是要給HttpClient 的調用加上熔斷器功能,代碼參看https://github.com/TomPallister/Ocelot/pull/27/files ,主要的一段代碼如下:
using Ocelot.Logging;
using Polly;
using Polly.CircuitBreaker;
using Polly.Timeout;
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Ocelot.Requester
{
public class CircuitBreakingDelegatingHandler : DelegatingHandler
{
private readonly IOcelotLogger _logger;
private readonly int _exceptionsAllowedBeforeBreaking;
private readonly TimeSpan _durationOfBreak;
private readonly Policy _circuitBreakerPolicy;
private readonly TimeoutPolicy _timeoutPolicy;
public CircuitBreakingDelegatingHandler(int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak,TimeSpan timeoutValue
,TimeoutStrategy timeoutStrategy, IOcelotLogger logger, HttpMessageHandler innerHandler)
: base(innerHandler)
{
this._exceptionsAllowedBeforeBreaking = exceptionsAllowedBeforeBreaking;
this._durationOfBreak = durationOfBreak;
_circuitBreakerPolicy = Policy
.Handle<HttpRequestException>()
.Or<TimeoutRejectedException>()
.Or<TimeoutException>()
.CircuitBreakerAsync(
exceptionsAllowedBeforeBreaking: exceptionsAllowedBeforeBreaking,
durationOfBreak: durationOfBreak,
onBreak: (ex, breakDelay) =>
{
_logger.LogError(".Breaker logging: Breaking the circuit for " + breakDelay.TotalMilliseconds + "ms!", ex);
},
onReset: () => _logger.LogDebug(".Breaker logging: Call ok! Closed the circuit again."),
onHalfOpen: () => _logger.LogDebug(".Breaker logging: Half-open; next call is a trial.")
);
_timeoutPolicy = Policy.TimeoutAsync(timeoutValue, timeoutStrategy);
_logger = logger;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Task<HttpResponseMessage> responseTask = null;
try
{
responseTask = Policy.WrapAsync(_circuitBreakerPolicy, _timeoutPolicy).ExecuteAsync<HttpResponseMessage>(() =>
{
return base.SendAsync(request,cancellationToken);
});
return responseTask;
}
catch (BrokenCircuitException ex)
{
_logger.LogError($"Reached to allowed number of exceptions. Circuit is open. AllowedExceptionCount: {_exceptionsAllowedBeforeBreaking}, DurationOfBreak: {_durationOfBreak}",ex);
throw;
}
catch (HttpRequestException)
{
return responseTask;
}
}
private static bool IsTransientFailure(HttpResponseMessage result)
{
return result.StatusCode >= HttpStatusCode.InternalServerError;
}
}
}
上面代碼我們使用Policy.WrapAsync組合了熔斷器和重試的兩個策略來解決部分失敗問題,思路很簡單,定義需要處理的異常有哪些,比如 Policy.Handle<HttpRequestException>() .Or<TimeoutRejectedException>() .Or<TimeoutException>(),當異常發生時候需要如何處理,使用熔斷器還是重試,上面這個代碼當然也是適合調用第三方服務用了。
歡迎大家加入建設.NET Core的微服務開發框架。從給項目Ocelot 點贊和fork代碼開始,一起來建設,春節我已經給項目貢獻了2個特性的代碼,服務發現和本文所講的熔斷器。
https://www.cnblogs.com/lwqlun/p/8119856.html
