【问题标题】:HttpConnectionManagerParams.setDefaultMaxConnectionsPerHost(int) seems not working correctlyHttpConnectionManagerParams.setDefaultMaxConnectionsPerHost(int) 似乎无法正常工作
【发布时间】:2013-10-09 15:21:37
【问题描述】:

我的应用程序必须处理客户端 http 请求,与一些 API 通信,并将数据返回给客户端。我为我的应用程序声明了一个全局 RestTemplate,如下所示:

<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
    <constructor-arg index="0">
        <bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams">
            <property name="authenticationPreemptive" value="false" />
        </bean>
    </constructor-arg>
    <constructor-arg index="1">
        <bean class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
            <property name="params">
                <bean class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
                    <property name="connectionTimeout" value="20000" />
                    <property name="soTimeout" value="20000" />
                    <property name="defaultMaxConnectionsPerHost" value="30" />
                    <property name="maxTotalConnections" value="500" />
                </bean>
            </property>
        </bean>
    </constructor-arg>
</bean>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="requestFactory">
        <bean class="org.springframework.http.client.CommonsClientHttpRequestFactory">
            <property name="httpClient" ref="httpClient" />
        </bean>
    </property>
</bean>

通过设置defaultMaxConnectionsPerHost,我预计每台主机的同时连接数为30。但是通过我的测试,无论我设置多大defaultMaxConnectionsPerHost,实际数字似乎最多6

我如何进行测试:

  • 客户端:通过浏览器同时向我的 Web 应用程序发送大约 20 个请求(我尝试了 FireFox 和 Chrome)。
  • 服务器端:通过netstat获取连接数

问题:

  • 问题一:为什么每台主机的同时连接数不是预期的30?
  • 问题 2:我的应用程序必须每秒向一台主机发送大约 10 个请求。每个请求持续 2 秒。那我应该设置defaultMaxConnectionsPerHost的合适值是多少?

【问题讨论】:

    标签: spring httpclient keep-alive resttemplate


    【解决方案1】:

    经过更多测试,我发现问题中提到的测试方式可能是错误的。 defaultMaxConnectionsPerHost 按预期工作。

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2012-05-06
      • 2013-02-17
      • 2017-10-09
      • 2017-08-03
      • 2014-05-02
      • 2011-09-11
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多