休息模板 - 没有缓冲区 space 可用(达到最大连接数?)

Rest Template - No buffer space available (maximum connections reached?)

我正在使用 RestTemplate 来执行并发 HTTP 请求。一段时间后,我开始

java.net.SocketException: No buffer space available (maximum connections reached?)

  1. 我知道这与处于 TIME_WAIT 状态的套接字有关。
  2. 我已经尝试安装 Windows 7 修复程序,这是大多数来源所鼓励的。
  3. 我将 RestTemplate 配置为使用 HttpClient,如下所示:

    val httpClient = HttpClientBuilder.create()
                .setMaxConnPerRoute(properties.concurrencyLimit)
                .setMaxConnTotal(properties.concurrencyLimit)
                .build()
    return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))
    
  4. 我试过使用十亿其他HttpClient配置

  5. 我尝试过 20-100 之间的不同数量的并发请求

就在我要按下Post你的问题按钮之前,我的同事找到了一个解决方案,这对我来说没有任何意义:

 val httpClient = HttpClientBuilder.create()
            .setMaxConnPerRoute(properties.concurrencyLimit * 2)
            .setMaxConnTotal(properties.concurrencyLimit * 2)
            .build()
    return RestTemplate(HttpComponentsClientHttpRequestFactory(httpClient))

基本上,当我将连接池设置为线程数的两倍时,整个过程就像一个魅力。

为什么?为什么第一个配置不起作用而第二个配置起作用?

所有依赖项都由 Spring Boot 1.4.0.RELEASE parent pom.

管理

连接基于路由汇集。由于您至少有 2 条路线,因此连接总数应至少为每条路线最大连接数的 2 倍。另请参阅其他相关 是的,如果您一个接一个地处理您的批处理,或者存在一些异步性质,我建议您使每个死记硬背的 conenctionPool 甚至超过线程数。(不确定您如何使用 concurrencyLimit)

除了来自 here:

PoolingHttpClientConnectionManager is a more complex implementation that manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route for which the manager already has a persistent connection available in the pool will be serviced by leasing a connection from the pool rather than creating a brand new connection. PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.