Elasticsearch java RestClient SocketTimeoutException: 12,000 milliseconds timeout on connection http-outgoing-12

Do I know how to solve this problem.

  // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_timeouts.html
    // setConnectTimeout 默认是 1秒 setSocketTimeout 默认30秒
    private RestClient getNewRestClient(String esAddress) {
        RestClient restClient = RestClient
                .builder(EnvionmentVariables.getHttpHostArray(esAddress))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                        .setKeepAliveStrategy(getConnectionKeepAliveStrategy())
                        .setMaxConnPerRoute(10))
                .setRequestConfigCallback(requestConfigBuilder ->
                        requestConfigBuilder
                                .setConnectTimeout(1 * 1000)
                                .setConnectionRequestTimeout(2 * 1000)
                                .setSocketTimeout(12 * 1000))//连接超时时间设置为12秒 因为前端12秒就断开了连接
                .build();
        return restClient;
    }

    private ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() {
        return (response, context) -> {
            //HeaderElementIterator it = new BasicHeaderElementIterator
            //        (response.headerIterator(HTTP.CONN_KEEP_ALIVE));
            //while (it.hasNext()) {
            //    HeaderElement he = it.nextElement();
            //    String param = he.getName();
            //    String value = he.getValue();
            //    if (value != null && param.equalsIgnoreCase
            //            ("timeout")) {
            //        return Long.parseLong(value) * 1000;
            //    }
            //}
            //return 60 * 1000;//如果没有约定,则默认定义时长为60s
            return 2 * 60 * 1000;
        };
    }
2 Likes