ASP.NET Core-HttpClientFactory + Polly 实现熔断降级【转】
2021-04-30 20:26
标签:mes name 说明 method span ext 参考 pst transient 本文主要介绍 创建 .NET Core API 项目(这里使用的是 .NET Core 2.2); 安装 在 Startup.cs 的 ConfigureServices 方法中添加 HttpClient 相关代码: 说明(这部分代码在实际使用中建议重新封装,不应该每次都为当前定义的 使用 HttpClient 说明:从 ASP.NET Core-HttpClientFactory + Polly 实现熔断降级【转】 标签:mes name 说明 method span ext 参考 pst transient 原文地址:https://www.cnblogs.com/fanfan-90/p/12151684.htmlHttpClientFactory
整合 Polly 的使用,实现对 Http 请求的超时、重试、熔断、降级等操作。HttpClientFactory 集成 Polly
Microsoft.Extensions.Http.Polly
NuGet Package;public void ConfigureServices(IServiceCollection services)
{
var fallbackResponse = new HttpResponseMessage
{
Content = new StringContent("Fallback"),
StatusCode = HttpStatusCode.GatewayTimeout
};
services.AddHttpClient("github", client =>
{
client.BaseAddress = new Uri("https://www.github.com");
})
// 降级
.AddPolicyHandler(PolicyHttpResponseMessage>.HandleException>().FallbackAsync(fallbackResponse, async b =>
{
await Task.CompletedTask;
_logger.LogWarning($"Fallback: {b.Exception.Message}");
}))
// 熔断
.AddPolicyHandler(PolicyHttpResponseMessage>.HandleTimeoutRejectedException>().CircuitBreakerAsync(5, TimeSpan.FromSeconds(3), (ex, ts) =>
{
_logger.LogWarning($"Open Circuit Breaker:{ts.TotalMilliseconds}");
}, () =>
{
_logger.LogWarning($"Closed Circuit Breaker");
}, () =>
{
_logger.LogWarning($"HalfOpen Circuit Breaker");
}))
// 重试
.AddPolicyHandler(PolicyHttpResponseMessage>
.HandleTimeoutRejectedException>()
.WaitAndRetryAsync(
new[]
{
TimeSpan.FromMilliseconds(100),
TimeSpan.FromMilliseconds(200)
}
)
)
// 超时
.AddPolicyHandler(Policy.TimeoutAsyncHttpResponseMessage>(TimeSpan.FromSeconds(0.5)));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
HttpClient
设置一堆重复代码):
services.AddHttpClient
定义一个名为 github
的 HttpClient
,后续可通过名称获取到这个 HttpClient 对象来发送 Http 请求;AddPolicyHandler
(其他扩展方法参考:Polly and HttpClientFactory AddTransientHttpErrorPolicy
、AddPolicyHandlerFromRegistry
的使用介绍) 为此 HttpClient
添加 Polly
策略;fallbackResponse
;TimeoutRejectedException
异常 5 次,熔断 3s;TimeoutRejectedException
分别等待 100ms、200ms 后重试;TimeoutRejectedException
;public class ValuesController : ControllerBase
{
private readonly IHttpClientFactory _httpClientFactory;
public ValuesController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
[HttpGet]
public async Task
HttpClientFactory
中获取定义好的名为 github
的 HttpClient
对象,和普通的 HttpClient
的区别是此 HttpClient
已具有定义的各种 Polly
策略,其他没任何区别;
转:https://www.jianshu.com/p/f4444c04b05c
文章标题:ASP.NET Core-HttpClientFactory + Polly 实现熔断降级【转】
文章链接:http://soscw.com/index.php/essay/80540.html