PoolingHttpClientConnectionManager 分配比设置为 MAX 更多的连接

问题描述

在我的一个项目中,我通过配置 PoolingHttpClientConnectionManager 将 RestTemplate 定义如下:

    @Bean
    @Qualifier("ssl-all-allowed")
    public RestTemplate restTemplate(@Autowired PoolingHttpClientConnectionConfig poolingHttpClientConnectionConfig) throws KeyStoreException,NoSuchAlgorithmException,KeyManagementException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain,String authType) -> true;

        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null,acceptingTrustStrategy)
                .build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(poolingHttpClientConnectionConfig.getConnectionMaxTotal());
        connectionManager.setDefaultMaxPerRoute(poolingHttpClientConnectionConfig.getConnectionMaxPerRoute());

        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .setSSLSocketFactory(csf)
                .build();

        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        return new RestTemplate(requestFactory);
    }

下面是配置类:

@Configuration
@Getter
public class PoolingHttpClientConnectionConfig
{
    @Value("${poolingHttp.connection.maxPerRoute: #{4}}")
    private Integer connectionMaxPerRoute;

    @Value("${poolingHttp.connection.maxTotal: #{40}}")
    private Integer connectionMaxTotal;
}

这些认值不会在任何属性文件中被覆盖。所以 connectionMaxPerRoute = 4 和 connectionMaxTotal = 40(在我的本地也打印并检查了调试)。但是下面的日志打印 route allocated: 1 of 4; total allocated: 11 of 40 这是可以理解的,但我无法理解的是这些行 -> route allocated: 5 of 100; total allocated: 5 of 100

100 个连接来自哪里?为什么要分配 100 个?

以下是示例日志:

[18 Feb 2021 07:23:22,704] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [1035235996] : Connection request: [route: {s}->https://example.org][total kept alive: 5; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:22,705] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [1035235996] : Connection leased: [id: 4949][route: {s}->https://example.org][total kept alive: 4; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:23,008] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [1035235996] : Connection [id: 4949][route: {s}->https://example.org] can be kept alive for 60.0 seconds

[18 Feb 2021 07:23:23,008] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [1035235996] : Connection released: [id: 4949][route: {s}->https://example.org][total kept alive: 5; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:23,160] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection request: [route: {s}->https://xxc.org][total kept alive: 10; route allocated: 0 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:23,160] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection leased: [id: 5023][route: {s}->https://xxc.org][total kept alive: 10; route allocated: 1 of 4; total allocated: 11 of 40]

[18 Feb 2021 07:23:23,256] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection released: [id: 5023][route: {s}->https://xxc.org][total kept alive: 10; route allocated: 0 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:23,266] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection request: [route: {}->http://booss.org][total kept alive: 10; route allocated: 2 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:23,266] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection leased: [id: 4841][route: {}->http://booss.org][total kept alive: 9; route allocated: 2 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:23,361] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection [id: 4841][route: {}->http://booss.org] can be kept alive indefinitely

[18 Feb 2021 07:23:23,361] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1197994833] : Connection released: [id: 4841][route: {}->http://booss.org][total kept alive: 10; route allocated: 2 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:25,233] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection request: [route: {s}->https://example.org][total kept alive: 5; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:25,234] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection leased: [id: 4949][route: {s}->https://example.org][total kept alive: 4; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:25,234] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection request: [route: {s}->https://example.org][total kept alive: 4; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:25,235] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection leased: [id: 4204][route: {s}->https://example.org][total kept alive: 3; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:25,540] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection [id: 4204][route: {s}->https://example.org] can be kept alive for 60.0 seconds

[18 Feb 2021 07:23:25,540] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection released: [id: 4204][route: {s}->https://example.org][total kept alive: 4; route allocated: 5 of 100; total allocated: 5 of 100]

[18 Feb 2021 07:23:25,748] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1590040578] : Connection request: [route: {s}->https://xxc.org][total kept alive: 10; route allocated: 0 of 4; total allocated: 10 of 40]

[18 Feb 2021 07:23:25,748] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [frontsite~1590040578] : Connection leased: [id: 5024][route: {s}->https://xxc.org][total kept alive: 10; route allocated: 1 of 4; total allocated: 11 of 40]

[18 Feb 2021 07:23:25,784] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection [id: 4949][route: {s}->https://example.org] can be kept alive for 60.0 seconds

[18 Feb 2021 07:23:25,784] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager  - [2043776099] : Connection released: [id: 4949][route: {s}->https://example.org][total kept alive: 5; route allocated: 5 of 100; total allocated: 5 of 100]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)