我有一个带有多个节点的群集/服务器,可以从应用程序中处理请求。
用户正在运行的应用程序,用以下URL打开2个Web客户端:
-
https://myprocess.myapp.com/api/json/v1
-
https://myprocess.myapp.com/api/protobuf/v1
为了支持粘性(我希望两个Web客户端中的每一个都可以保持与节点的连接 - 持久性),ConnectionLeasEtimeOut与默认值保持在一起,这意味着"不要关闭连接"而且由于DefaultPersistentConnectionLimit默认为2,因此我将DefaultConnectionLimit设置为1。
问题:
-
servicepoint.currentConnections是2
-
在服务器中,我看到远程主机端口正在发生变化,即我看到了2个以上的端口(每个客户端的1个端口以上)。
我在做什么错?
我的课程输出:
communicationwebclient https://myprocess.myapp.com/api/json/v1
communicationwebclient https://myprocess.myapp.com/api/protobuf/v1
sendAsync _uri = https://myprocess.myapp.com/api/json/v1 servicepoint.address = https://myprocess.myapp.myapp.com/api/json/json/json/json/v1 servicepoint.connection.connection.connection.connection.connectionlimit = 1 Servicepoint.correntContopoint.currentConnections.currentConnections.currentConnections.currentConnections.currentConnections = 2
...
sendAsync _uri = https://myprocess.myapp.com/api/protobuf/v1 servicepoint.address.address = https://myprocess.myapp.com/api/json/json/json/json/json/v1 servicepoint.connection.connection.connection.connection.connection.connection.connection = 1 ServicePoint.correntContopoint.correntConnectimpopt
...
public sealed class CommunicationWebClient : IDisposable
{
private HttpClient _httpClient;
private Uri _uri;
public CommunicationWebClient(Uri uri)
{
Logger.Debug($"{nameof(CommunicationWebClient)} {nameof(uri)}={uri}");
_uri = uri;
ServicePointManager.DefaultConnectionLimit = 1;
_httpClient = new HttpClient(new WebRequestHandler())
{
Timeout = 10.Minutes(),
};
}
public void Dispose()
{
_httpClient.Dispose();
}
public async Task SendAsync(
ByteArrayContent content)
{
var servicePoint = ServicePointManager.FindServicePoint(_uri);
Logger.Debug($"{nameof(SendAsync)} " +
$"{nameof(_uri)}={_uri} " +
$"{nameof(servicePoint.Address)}={servicePoint.Address} " +
$"{nameof(servicePoint.ConnectionLimit)}={servicePoint.ConnectionLimit} " +
$"{nameof(servicePoint.CurrentConnections)}={servicePoint.CurrentConnections}");
using (var httpResponseMessage = await _httpClient.PostAsync(_uri, content))
{
...
}
}
}
如果您仍然有问题,请检查您的CommunicationWebClient是否不经常处理。它处置了httpclient,但行为不像通常的人期望。
查看本文:https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/
不久的是,当您在Windows的情况下处置HTTPClient时,您要求Windows关闭所有打开的插座。但是默认情况下,Windows有4分钟的超时,可以完全关闭插座。因此,在所有这4分钟内,您的HTTPClient和Web服务器之间都会有联系。
在大多数情况下,建议使用HTTP客户端使用一个应用程序。处置HTTP客户端不会立即抛出它使用的插座。