httpclient4.4 出現NoHttpResponseException的異常解決

httpclient4.4簡單初始化httpclient的方式:java

HttpClient httpClient = HttpClientBuilder.create().build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        restTemplate = new RestTemplate(requestFactory);

使用鏈接池初始化httpclient的方式:web

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(100);
        connManager.setDefaultMaxPerRoute(10);
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(connManager);
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
        HttpClient httpClient = HttpClientBuilder.create().build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        restTemplate = new RestTemplate(requestFactory);

但在測試過程當中發現,有時候出現以下的異常,但重試一次就沒有問題:apache

Caused by: org.apache.http.NoHttpResponseException: 10.0.0.25:8080 failed to respond
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)

網上查了相關的報錯信息,找到官網上的描述tomcat

org.apache.commons.httpclient.NoHttpResponseException java.io.IOException +- org.apache.commons.httpclient.NoHttpResponseExceptionIn some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This may cause the server to drop the connection to the client without giving any response. HttpClient throws NoHttpResponseException when it encounters such a condition. In most cases it is safe to retry a method that failed with NoHttpResponseException.ide

根據描述主要就是由於服務端斷開鏈接,但並無通知客戶端,致使下次請求該服務時httpclient繼續使用該鏈接致使報錯。性能

查看了tomcat7的文檔,發如今Connector中keepAliveTimeout能夠配置鏈接空閒多久就關閉該鏈接,它的值默認是和connectionTimeout同樣,而在server.xml中connectionTimeout值默認爲20s。測試

經過驗證,確實是在第一次請求20秒後,再請求就會報出NoHttpResponseException的異常。ui

經過查看debug日誌,以上代碼生成的connection默認是keep alive的,經過以下代碼能夠將httpclient生成的connection的alive時間可配置。this

ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {
            @Override
            public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
                return 20 * 1000; // tomcat默認keepAliveTimeout爲20s
            }
        };
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(20, TimeUnit.SECONDS);
        connManager.setMaxTotal(200);
        connManager.setDefaultMaxPerRoute(200);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(10 * 1000)
                .setSocketTimeout(10 * 1000)
                .setConnectionRequestTimeout(10 * 1000)
                .build();
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setConnectionManager(connManager);
        httpClientBuilder.setDefaultRequestConfig(requestConfig);
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler());
        httpClientBuilder.setKeepAliveStrategy(connectionKeepAliveStrategy);
        HttpClient httpClient = httpClientBuilder.build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        restTemplate = new RestTemplate(requestFactory);

主要是增長keepalive的策略,但這又帶來一個問題,全部的鏈接只有20秒,沒法使用長鏈接的性能優點,在查看API時發現PoolingHttpClientConnectionManager提供有debug

@Override
    public void closeIdleConnections(final long idleTimeout, final TimeUnit tunit) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
        }
        this.pool.closeIdle(idleTimeout, tunit);
    }

的方法,後續研究可否使用該API解決短連接的問題。

相關文章
相關標籤/搜索