每个服务端点的HttpClient实例化 [英] HttpClient Instancing Per Service-Endpoint

查看:61
本文介绍了每个服务端点的HttpClient实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实例化HttpClient时,一个常见的建议是:

When instancing an HttpClient, the one common piece of advice is:

但是,基于在此链接上,我看到了一些评论,我认为这暗示着另一条规则:

However, based on this link I see commentary which I think implies another rule:

HttpClient类实例充当发送HTTP请求的会话.HttpClient实例是应用于该实例执行的所有请求的设置的集合.此外,每个HttpClient实例都使用自己的连接池,将其请求与其他HttpClient实例执行的请求隔离开来.

The HttpClient class instance acts as a session to send HTTP requests. An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances.

这使我想知道是否应该为与之交互的每个服务端点创建一个HttpClient实例.服务端点"是指不同的基地址.以下每个都是不同的服务端点":

This makes me wonder if I am supposed to create one HttpClient instance for each service-endpoint I interact with. By "service-endpoint", I mean a distinct base address. Each of the following would be a distinct "service-endpoint":

  • "http://foo.net/api/Message/"
  • "http://bar.com/api/Message/"
  • "http://wow.gov/api/Message/"
  • "http://now.com/api/Message/"
  • "http://mom.org/api/Message/"
  • "http://dog.com/api/Message/"

当然,如果我打算使用HttpClient的"BaseAddress"属性,并且要处理并发调用,那么每个服务端点"都需要有一个HttpClient实例.

Certainly if I intend on using the "BaseAddress" property of the HttpClient, and if I'm dealing with concurrent calls, then I will need to have one instance of HttpClient per "service-endpoint".

但是,HttpClient确实允许我明确指定绝对地址:

However, HttpClient does allow me to specify an absolute address explicitly:

HttpClient client = new HttpClient(...);

client.PostAsJsonAsync("http://foo.net/api/Message/", ...);
client.PostAsJsonAsync("http://bar.com/api/Message/", ...);
client.PostAsJsonAsync("http://wow.gov/api/Message/", ...);
client.PostAsJsonAsync("http://now.com/api/Message/", ...);
client.PostAsJsonAsync("http://mom.org/api/Message/", ...);
client.PostAsJsonAsync("http://dog.com/api/Message/", ...);

上面的代码有效,这正是我要构建的当前应用程序想要的.但是,棘手的问题仍然存在……如果我的应用程序与之通信的所有服务端点都使用一个HttpClient,我是否做错了?

The above code works, and it is exactly what I want for the current application I'm building. But the nagging question remains...am I doing something wrong if I use one HttpClient for all service-endpoints my application communicates with?

是否有理由真正需要以上引用中提到的连接池隔离"?

Is there a reason why I would truly need the "connection pool isolation" that was mentioned in the above quotation?

推荐答案

但是我想知道我是否会折衷于HttpClient,因为我要让许多端点共享同一连接池.

But I want to know if I will be compromising the inner workings of HttpClient because I'm making many endpoints share the same connection pool.

不,我认为 HttpClient 的单个实例不会耗尽您的资源,但这实际上取决于您将发出的并发请求数. HttpClient 旨在处理并发请求,通过使用异步API( XXXAsync ),您可以实现此目的.

No, I don't think a single instance of HttpClient will exhaust your resources, but that really depends on how many concurrent requests you'll be making. HttpClient is designed to serve concurrent requests, and by using asynchronous API's such (XXXAsync), you can achieve just that.

我建议不要忘记设置 ServicePointManager.DefaultConnectionLimit 设置为更高的数字,因为它的默认值为2(并发请求).

I would advise not to forget to set ServicePointManager.DefaultConnectionLimit to a higher number, as it's default value is 2 (concurrent requests).

此外,如果您认为您将很快遇到一个实例,那么我建议您像往常一样对您的应用程序进行概要分析,以了解确切的瓶颈.

Also, If you think you'll be hitting the wall quickly with a single instance, I'd suggest as always to profile your application to understand the exact bottleneck.

这篇关于每个服务端点的HttpClient实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆