在2.1以后,有很多新东西,其中HttpClientFactory算是一个。HttpClientFactory涉及的东西也不算少,三四种clients , 请求中间件,与Polly的结合,生命周期等。git
Steeltoe的组件升级到2.1后,很多示例代码已经使用HttpClientFactory了。固然这是个题外话。github
这里主要讲的是与Polly的结合,来完成简单的熔断降级。在这以前,仍是先看看关于HttpClientFactory最简单的用法。api
用个简单的控制台程序来演示async
这里就只是获取一下状态码,没有获取实际的内容。ide
static async Task<string> BasicUsage() { var serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(); var services = serviceCollection.BuildServiceProvider(); var clientFactory = services.GetService<IHttpClientFactory>(); var client = clientFactory.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Get, "https://www.github.com"); var response = await client.SendAsync(request).ConfigureAwait(false); return response.StatusCode.ToString(); }
其实主要的操做就是AddHttpClient,而后经过HttpClientFactory建立一个HttpClient对象,有了HttpClient对象,下面的操做应该就不用多说了。ui
而后在Main方法调用this
Console.WriteLine($"BasicUsage, StatusCode = {BasicUsage().GetAwaiter().GetResult()}");
用法感受并无太多的差异。下面来看看与Polly的结合。3d
Polly的wiki页面已经有了这二者结合使用的文档了。日志
https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactorycode
其实如今对于咱们来讲,要想对http请求使用Polly的一些特性已经很是的简单了。
咱们在使用的时候要添加Microsoft.Extensions.Http.Polly
的Nuget包。
先来看看使用Polly的三种扩展方法
扩展方法 | 说明 |
---|---|
AddTransientHttpErrorPolicy | 主要是处理Http请求的错误,如HTTP 5XX 的状态码,HTTP 408 的状态码 以及System.Net.Http.HttpRequestException异常。 |
AddPolicyHandler | 自定义,和传统定义Polly的方式保持一致 |
AddPolicyHandlerFromRegistry | 从Policy集合(也是自定义的)里面选择本身想要的。 |
后面的操做,是用的AddPolicyHandler。
因为咱们要实现熔断降级,因此,咱们必不可少的要用到CircuitBreakerPolicy和FallbackPolicy,同时为了方便演示,再加个TimeoutPolicy。
因为涉及到多个Policy,因此咱们必需要肯定他们的执行顺序!
Polly的wiki页面有个示例,还配了一幅很详细的时序图。
一句话来讲就是最早起做用的,仍是最后添加的那个。
下面就新建一个API项目,用来演示一下。
修改ConfigureServices方法,具体以下
public void ConfigureServices(IServiceCollection services) { var fallbackResponse = new HttpResponseMessage(); fallbackResponse.Content = new StringContent("fallback"); fallbackResponse.StatusCode = System.Net.HttpStatusCode.TooManyRequests; services.AddHttpClient("cb", x => { x.BaseAddress = new Uri("http://localhost:8000"); x.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Test"); }) //fallback .AddPolicyHandler(Policy<HttpResponseMessage>.Handle<Exception>().FallbackAsync(fallbackResponse, async b => { Logger.LogWarning($"fallback here {b.Exception.Message}"); })) //circuit breaker .AddPolicyHandler(Policy<HttpResponseMessage>.Handle<Exception>().CircuitBreakerAsync(2, TimeSpan.FromSeconds(4), (ex, ts) => { Logger.LogWarning($"break here {ts.TotalMilliseconds}"); }, () => { Logger.LogWarning($"reset here "); })) //timeout .AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(1)); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
而后是在控制器去使用。
[Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { private static int myCount = 0; private readonly IHttpClientFactory _clientFactory; public ValuesController(IHttpClientFactory clientFactory) { this._clientFactory = clientFactory; } // GET api/values/timeout [HttpGet("timeout")] public ActionResult<IEnumerable<string>> Timeout() { if (myCount < 3)//模拟超时 { System.Threading.Thread.Sleep(3000); } myCount++; return new string[] { "value1", "value2" }; } // GET api/values [HttpGet("")] public async Task<string> GetAsync() { var client = _clientFactory.CreateClient("cb"); var request = new HttpRequestMessage(HttpMethod.Get, "/api/values/timeout"); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); return content; } }
效果以下
前面几回请求,会由于超时或熔断,从而咱们获得的结果是fallback。
过了4秒钟后再请求,因为没有超时,正常拿到告终果,因此熔断器会被reset。
来看看日志
比较清晰的看到了全部的操做。
整体来讲,HttpClientFactory仍是很不错的。尤为是它能够直接使用Polly相关的特性。
部分示例代码: HttpClientFactoryDemo