.NetCore 实践——HttpClientFactory[一]
HttpClientFactory介绍
HttpClientFactory
主要有下面的功能:
管理内部HttpMessageHandler 的生命周期,灵活应对资源问题和DNS刷新问题
支持命名话、类型化配置,集中管理配置,避免冲突。
灵活的出站请求管道配置,轻松管理请求生命周期
内置管道最外层和最内层日志记录器,有information 和 Trace 输出
核心对象:
HttpClient
HttpMessageHandler
SocketsHttpHandler
DelegatingHandler
IHttpClientFactory
IHttpClientBuilder
请求的大概流程图为:
从图中可以看出SocketsHttpHandler
才是发起请求
LogginScopeHttpMessageHandler
、CustomMessageHandler
还有LoggingHttpMessageHandler
,他们都是中间处理,处于管道之中,可以理解为中间件部分。
LogginScopeHttpMessageHandler
是没有经过CustomMessageHandler
的日志,LoggingHttpMessageHandler
是经过CustomMessageHandler
的日志,也就是说LoggingHttpMessageHandler
才是正在去发送请求的数据。
HttpClient创建模式
HttpClientFactory
提供了三种创建HttpClient的模式:
工厂模式
命名客户端模式
类型化客户端模式
接下来详细介绍三种模式的用法
工厂模式
在.net core 中使用工厂模式,要引入:
services.AddHttpClient();
然后使用:
public class OrderServiceClient
{
private IHttpClientFactory _httpClientFactory;
public OrderServiceClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<string> Get()
{
var client = _httpClientFactory.CreateClient();
return await client.GetStringAsync("http://localhost:9000/WeatherForecast");
}
}
可能有些人认为IHttpClientFactory,可能它的实现是HttpClientFactory,因为.net core的基础库中就有HttpClientFactory,然而实际不是,那么来看下源码。
public static IServiceCollection AddHttpClient(
this IServiceCollection services)
{
if (services == null)
throw new ArgumentNullException(nameof (services));
services.AddLogging();
services.AddOptions();
services.TryAddTransient<HttpMessageHandlerBuilder, DefaultHttpMessageHandlerBuilder>();
services.TryAddSingleton<DefaultHttpClientFactory>();
services.TryAddSingleton<IHttpClientFactory>((Func<IServiceProvider, IHttpClientFactory>) (serviceProvider => (IHttpClientFactory) serviceProvider.GetRequiredService<DefaultHttpClientFactory>()));
services.TryAddSingleton<IHttpMessageHandlerFactory>((Func<IServiceProvider, IHttpMessageHandlerFactory>) (serviceProvider => (IHttpMessageHandlerFactory) serviceProvider.GetRequiredService<DefaultHttpClientFactory>()));
services.TryAdd(ServiceDescriptor.Transient(typeof (ITypedHttpClientFactory<>), typeof (DefaultTypedHttpClientFactory<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof (DefaultTypedHttpClientFactory<>.Cache), typeof (DefaultTypedHttpClientFactory<>.Cache)));
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHttpMessageHandlerBuilderFilter, LoggingHttpMessageHandlerBuilderFilter>());
services.TryAddSingleton<HttpClientMappingRegistry>(new HttpClientMappingRegistry());
return services;
}
IHttpClientFactory
的实现类是DefaultHttpClientFactory
;
看下CreateClient:
public HttpClient CreateClient(string name)
{
if (name == null)
throw new ArgumentNullException(nameof (name));
HttpClient httpClient = new HttpClient(this.CreateHandler(name), false);
HttpClientFactoryOptions clientFactoryOptions = this._optionsMonitor.Get(name);
for (int index = 0; index < clientFactoryOptions.HttpClientActions.Count; ++index)
clientFactoryOptions.HttpClientActions[index](httpClient);
return httpClient;
}
public HttpMessageHandler CreateHandler(string name)
{
if (name == null)
throw new ArgumentNullException(nameof (name));
ActiveHandlerTrackingEntry entry = this._activeHandlers.GetOrAdd(name, this._entryFactory).Value;
this.StartHandlerEntryTimer(entry);
return (HttpMessageHandler) entry.Handler;
}
扩展类:
public static HttpClient CreateClient(this IHttpClientFactory factory)
{
if (factory == null)
throw new ArgumentNullException(nameof (factory));
return factory.CreateClient(Microsoft.Extensions.Options.Options.DefaultName);
}
命名客户端方式(Named Client)
services.AddHttpClient();
services.AddHttpClient("NamedClient", client =>
{
client.DefaultRequestHeaders.Add("token","123456");
client.BaseAddress = new Uri("http://locahost:9000");
});
前面提及到client可以命名,那么这里就可以提前创建好对应的客户端配置。
public static IHttpClientBuilder AddHttpClient(this IServiceCollection services,
string name, Action<HttpCLient> configureClient)
{
if (services == null)
throw new ArgumentNullException(nameof (services));
if (name == null)
throw new ArgumentNullException(nameof (name));
if (configureClient == null)
throw new ArgumentNullException(nameof (configureClient));
services.AddHttpClient();
DefaultHttpClientBuilder builder = new DefaultHttpClientBuilder(services, name);
builder.ConfigureHttpClient(configureClient);
return (IHttpClientBuilder) builder;
}
再来看下 ConfigureHttpClient
public static IHttpClientBuilder ConfigureHttpClient(
this IHttpClientBuilder builder,
Action<HttpClient> configureClient)
{
if (builder == null)
throw new ArgumentNullException(nameof (builder));
if (configureClient == null)
throw new ArgumentNullException(nameof (configureClient));
builder.Services.Configure<HttpClientFactoryOptions>(builder.Name, (Action<HttpClientFactoryOptions>) (options => options.HttpClientActions.Add(configureClient)));
return builder;
}
HttpClientFactoryOptions 眼熟吧
client =>{
client.DefaultRequestHeaders.Add("token","123456");
client.BaseAddress = new Uri("http://localhost:9000");
}
然后clientFactoryOptions.HttpClientActions[index]
就会调用上面的这个Action。
我们在使用时这样写:
public class NamedHttpClient
{
private IHttpClientFactory _httpClientFactory;
private const string _clientName = "NamedClient";
public NamedOrderServiceClient(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<string> Get()
{
var client = _httpClientFactory.CreateClient(_clientName);
return await client.GetStringAsync("/WeatherForecast");
}
}
其实在我们使用过程中最好去使用这种方式,有两个好处。
不同客户端可以单独配置
不同的可以的生命周期不同,即使一个httpclient崩溃了,另外一个httpclient也可以正常请求。
那么除了配置client的一些基本配置,如baseurl或者header这种。
services.AddHttpClient("NamedOrderServiceClient", client =>
{
client.DefaultRequestHeaders.Add("token","123456");
client.BaseAddress = new Uri("http://localhost:9000");
}).SetHandlerLifetime(TimeSpan.FromMinutes(20));
当然最重要可以为每个httpclient自定义不同的管道,上文提及到到达正在的执行的过程中,会经过管道,中间我们可以自定义。
public class RequestCustomHandler: DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken)
{
requestMessage.Headers.Add("token2",Guid.NewGuid().ToString());
// 请求前处理
var request = await base.SendAsync(requestMessage, cancellationToken);
// 请求后处理
return request;
}
}
在服务配置时使用:
services.AddSingleton<RequestCustomHandler>();
services.AddHttpClient("NamedOrderServiceClient", client =>
{
client.DefaultRequestHeaders.Add("token","123456");
client.BaseAddress = new Uri("http://localhost:9000");
}).SetHandlerLifetime(TimeSpan.FromMinutes(20))
.AddHttpMessageHandler(provider => provider.GetService<RequestCustomHandler>());
那么来看下其管道怎么实现的:
public static IHttpClientBuilder AddHttpMessageHandler(
this IHttpClientBuilder builder,
Func<IServiceProvider, DelegatingHandler> configureHandler)
{
if (builder == null)
throw new ArgumentNullException(nameof (builder));
if (configureHandler == null)
throw new ArgumentNullException(nameof (configureHandler));
builder.Services.Configure<HttpClientFactoryOptions>(builder.Name, (Action<HttpClientFactoryOptions>) (options => options.HttpMessageHandlerBuilderActions.Add((Action<HttpMessageHandlerBuilder>) (b => b.AdditionalHandlers.Add(configureHandler(b.Services))))));
return builder;
}
其也就是做了一些配置,生成了一些action,那么哪里调用了呢?
在DefaultHttpClientFactory
:
public DefaultHttpClientFactory(
IServiceProvider services,
IServiceScopeFactory scopeFactory,
ILoggerFactory loggerFactory,
IOptionsMonitor<HttpClientFactoryOptions> optionsMonitor,
IEnumerable<IHttpMessageHandlerBuilderFilter> filters)
{
if (services == null)
throw new ArgumentNullException(nameof (services));
if (scopeFactory == null)
throw new ArgumentNullException(nameof (scopeFactory));
if (loggerFactory == null)
throw new ArgumentNullException(nameof (loggerFactory));
if (optionsMonitor == null)
throw new ArgumentNullException(nameof (optionsMonitor));
if (filters == null)
throw new ArgumentNullException(nameof (filters));
this._services = services;
this._scopeFactory = scopeFactory;
this._optionsMonitor = optionsMonitor;
this._filters = filters.ToArray<IHttpMessageHandlerBuilderFilter>();
this._logger = (ILogger) loggerFactory.CreateLogger<DefaultHttpClientFactory>();
this._activeHandlers = new ConcurrentDictionary<string, Lazy<ActiveHandlerTrackingEntry>>((IEqualityComparer<string>) StringComparer.Ordinal);
this._entryFactory = (Func<string, Lazy<ActiveHandlerTrackingEntry>>) (name => new Lazy<ActiveHandlerTrackingEntry>((Func<ActiveHandlerTrackingEntry>) (() => this.CreateHandlerEntry(name)), LazyThreadSafetyMode.ExecutionAndPublication));
this._expiredHandlers = new ConcurrentQueue<ExpiredHandlerTrackingEntry>();
this._expiryCallback = new TimerCallback(this.ExpiryTimer_Tick);
this._cleanupTimerLock = new object();
this._cleanupActiveLock = new object();
}
看到这一行:
this._entryFactory = (Func<string, Lazy<ActiveHandlerTrackingEntry>>) (name => new Lazy<ActiveHandlerTrackingEntry>((Func<ActiveHandlerTrackingEntry>) (() => this.CreateHandlerEntry(name)), LazyThreadSafetyMode.ExecutionAndPublication));
关注一下这个CreateHandlerEntry
,这里调用了,后面会看到。
当我们创建client的时候:
public HttpClient CreateClient(string name)
{
if (name == null)
throw new ArgumentNullException(nameof (name));
HttpClient httpClient = new HttpClient(this.CreateHandler(name), false);
HttpClientFactoryOptions clientFactoryOptions = this._optionsMonitor.Get(name);
for (int index = 0; index < clientFactoryOptions.HttpClientActions.Count; ++index)
clientFactoryOptions.HttpClientActions[index](httpClient);
return httpClient;
}
public HttpMessageHandler CreateHandler(string name)
{
if (name == null)
throw new ArgumentNullException(nameof (name));
ActiveHandlerTrackingEntry entry = this._activeHandlers.GetOrAdd(name, this._entryFactory).Value;
this.StartHandlerEntryTimer(entry);
return (HttpMessageHandler) entry.Handler;
}
在CreateHandler 调用_entryFactory,那么来看一下CreateHandlerEntry:
internal ActiveHandlerTrackingEntry CreateHandlerEntry(string name)
{
IServiceProvider provider = this._services;
IServiceScope scope = (IServiceScope) null;
HttpClientFactoryOptions options = this._optionsMonitor.Get(name);
if (!options.SuppressHandlerScope)
{
scope = this._scopeFactory.CreateScope();
provider = scope.ServiceProvider;
}
try
{
HttpMessageHandlerBuilder requiredService = provider.GetRequiredService<HttpMessageHandlerBuilder>();
requiredService.Name = name;
Action<HttpMessageHandlerBuilder> next = new Action<HttpMessageHandlerBuilder>(Configure);
for (int index = this._filters.Length - 1; index >= 0; --index)
next = this._filters[index].Configure(next);
next(requiredService);
LifetimeTrackingHttpMessageHandler handler = new LifetimeTrackingHttpMessageHandler(requiredService.Build());
return new ActiveHandlerTrackingEntry(name, handler, scope, options.HandlerLifetime);
}
catch
{
scope?.Dispose();
throw;
}
void Configure(HttpMessageHandlerBuilder b)
{
for (int index = 0; index < options.HttpMessageHandlerBuilderActions.Count; ++index)
options.HttpMessageHandlerBuilderActions[index](b);
}
}
在Configure 执行了我们前面执行的action,也就是b => b.AdditionalHandlers.Add(configureHandler(b.Services))。
httpclient 执行请求,其实最后是HttpMessageHandler去执行。那么这个HttpMessageHandler 怎么来的呢?
public abstract class HttpMessageHandlerBuilder
{
public abstract string Name { get; set; }
public abstract HttpMessageHandler PrimaryHandler { get; set; }
public abstract IList<DelegatingHandler> AdditionalHandlers { get; }
public virtual IServiceProvider Services { get; }
public abstract HttpMessageHandler Build();
protected internal static HttpMessageHandler CreateHandlerPipeline(
HttpMessageHandler primaryHandler,
IEnumerable<DelegatingHandler> additionalHandlers)
{
if (primaryHandler == null)
throw new ArgumentNullException(nameof (primaryHandler));
if (additionalHandlers == null)
throw new ArgumentNullException(nameof (additionalHandlers));
IReadOnlyList<DelegatingHandler> delegatingHandlerList = (IReadOnlyList<DelegatingHandler>) ((object) (additionalHandlers as IReadOnlyList<DelegatingHandler>) ?? (object) additionalHandlers.ToArray<DelegatingHandler>());
HttpMessageHandler httpMessageHandler = primaryHandler;
for (int index = delegatingHandlerList.Count - 1; index >= 0; --index)
{
DelegatingHandler delegatingHandler = delegatingHandlerList[index];
if (delegatingHandler == null)
throw new InvalidOperationException(Resources.FormatHttpMessageHandlerBuilder_AdditionalHandlerIsNull((object) nameof (additionalHandlers)));
if (delegatingHandler.InnerHandler != null)
throw new InvalidOperationException(Resources.FormatHttpMessageHandlerBuilder_AdditionHandlerIsInvalid((object) "InnerHandler", (object) "DelegatingHandler", (object) nameof (HttpMessageHandlerBuilder), (object) Environment.NewLine, (object) delegatingHandler));
delegatingHandler.InnerHandler = httpMessageHandler;
httpMessageHandler = (HttpMessageHandler) delegatingHandler;
}
return httpMessageHandler;
}
}
AdditionalHandlers
眼熟吧,这个HttpMessageHandler 和中间件一样玩的都是套娃功能,形成一个小周天。
类型化客户端(Typed Client)
public class TypeServiceClient
{
private HttpClient _httpClient;
public TypeServiceClient(HttpClient httpClientFactory)
{
_httpClient = httpClientFactory;
}
public async Task<string> Get()
{
return await _httpClient.GetStringAsync("http://localhost:9000/WeatherForecast");
}
}
这种最为简单,直接生成了HttpClient ,名字就是TypeServiceClient
。
services.AddHttpClient<TypeOrderServiceClient>( client =>
{
client.DefaultRequestHeaders.Add("token","123456");
client.BaseAddress = new Uri("http://localhost:9000");
}).SetHandlerLifetime(TimeSpan.FromMinutes(20))
.AddHttpMessageHandler(provider => provider.GetService<RequestCustomHandler>());
这样就ok的,没有名字会使用泛型名。
public static IHttpClientBuilder AddHttpClient<TClient>(
this IServiceCollection services,
Action<HttpClient> configureClient)
where TClient : class
{
if (services == null)
throw new ArgumentNullException(nameof (services));
if (configureClient == null)
throw new ArgumentNullException(nameof (configureClient));
services.AddHttpClient();
string typeDisplayName = TypeNameHelper.GetTypeDisplayName(typeof (TClient), false, false, true, '+');
DefaultHttpClientBuilder builder = new DefaultHttpClientBuilder(services, typeDisplayName);
builder.ConfigureHttpClient(configureClient);
builder.AddTypedClientCore<TClient>(true);
return (IHttpClientBuilder) builder;
}