你们都知道,使用HttpClient,在并发量不大的状况,通常没有任何问题;可是在并发量一上去,若是使用不当,会形成很严重的堵塞的状况。json
解决方案以下:api
1、能够参考微软官方提供的方法:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2并发
2、个人解决方案是根据官方提供的方法,选择一种最适合项目的写法进行改造。app
一、nuget添加包Microsoft.AspNetCore.Http;异步
二、startup里ConfigureServices方法添加代码:async
//添加httpclient方法 services.AddHttpClient("ApiServiceName", c => { c.BaseAddress = new Uri(Configuration["ApiConfig:SystemService"]);//地址从配置文件appsettings.json里取 c.Timeout = TimeSpan.FromSeconds(30);//超时间时间设置为30秒 });
三、在须要用的地方注入进去:(通常在构造函数里)函数
private ILogger logHelper; private readonly IHttpContextAccessor httpContextAccessor; private readonly IHttpClientFactory _clientFactory; HttpClient client; public articleService(ILogger<reportService> logger, IHttpContextAccessor _httpContextAccessor, IHttpClientFactory clientFactory) { logHelper = logger; httpContextAccessor = _httpContextAccessor; _clientFactory = clientFactory; client = _clientFactory.CreateClient("ApiServiceName"); }
client = _clientFactory.CreateClient("SystemService"); 这句话也能够在具体的方法里执行,由于咱们的项目所有都是调用接口,因此放构造函数比较省事。post
四、添加一个公共方法:ui
using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace MuXue.Common { /// <summary> /// 异步调用,而且client是注入方式,传过来 /// 2019-8-21 /// 李朴 /// </summary> public class MyHttpClientHelper { public async Task<T> GetData<T>(HttpClient client, GateWay action, dynamic param) { string paramStr = JsonConvert.SerializeObject(param); string jrclientguid = Guid.NewGuid().ToString("n"); try { LogHelper.Info($"MyHttpClientHelper开始,url={client.BaseAddress},action={Utils.GetEnumDescription(action)},postData={paramStr} ,jrclientguid={jrclientguid}---------"); client.DefaultRequestHeaders.Add(CommonConstant.jrclientguid, jrclientguid); HttpResponseMessage response; using (HttpContent httpContent = new StringContent(paramStr, Encoding.UTF8)) { httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); response = await client.PostAsync("api/" + Utils.GetEnumDescription(action), httpContent); } if (response != null && response.IsSuccessStatusCode) { T resp = await response.Content.ReadAsAsync<T>(); return resp; } else { return default(T); } } catch (Exception ex) { throw; } finally { LogHelper.Info($"MyHttpClientHelper结束,url={client.BaseAddress},action={Utils.GetEnumDescription(action)},postData={paramStr} ,jrclientguid={jrclientguid}---------"); } } } }
这里的参数 GateWay action 也能够换作是接口地址(不包含域名);
五、接着在第3步的那个类里,须要调用接口的地方写方法:
//二、取接口里取 MyHttpClientHelper myHttpClientHelper = new MyHttpClientHelper(); Result<List<Article>> resp=await myHttpClientHelper.GetData<Result<List<Article>>>(client, GateWay.GetNoticeList, null);
这样就完成了。url
六、所有用异步方式;