1

我有一个带有多个节点的集群/服务器来处理来自应用程序的请求。

用户正在运行的应用程序打开 2 个具有以下 URL 的 Web 客户端:

为了支持粘性(我希望 2 个 Web 客户端中的每一个都保持与节点的连接 - 持久性),ConnectionLeaseTimeout保持默认值,这意味着“不要关闭连接”并且因为DefaultPersistentConnectionLimit为 2默认情况下,我将DefaultConnectionLimit设置为 1。

问题:

  1. servicePoint.CurrentConnections 是 2,尽管 servicePoint.ConnectionLimit 是 1。

  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.com/api/json/v1 servicePoint.ConnectionLimit=1 servicePoint.CurrentConnections=2

...

SendAsync _uri= https://myprocess.myapp.com/api/protobuf/v1 servicePoint.Address= https://myprocess.myapp.com/api/json/v1 servicePoint.ConnectionLimit=1 servicePoint.CurrentConnections=2

...

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))
        {
            ...
        }
    }
}
4

1 回答 1

0

如果您仍然有问题,请检查您的 CommunicationWebClient 是否未经常处理。它处理 HttpClient 但它的行为不像人们通常期望的那样。

查看这篇文章:https ://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/

简而言之,当您在 windows 的情况下处理 HttpClient 时,您会要求 windows 关闭所有打开的套接字。但默认情况下,windows 有 4 分钟的超时时间来完全关闭套接字。因此,在这 4 分钟内,您的 HttpClient 和 Web 服务器之间将建立连接。

于 2018-05-08T12:23:25.943 回答