How to configure the socket timeout of the Rest high level client in Spring Data Elasticsearch 2.2.3.RELEASE
Asked Answered
A

3

6

I use Spring Boot Starter Data Elasticsearch 2.2.3.RELEASE with Elasticsearch v6.8.6. I configured the RestHighLevelClient for the connection to the cluster.

Now I constantly get a SocketTimeoutException on different operations:

java.net.SocketTimeoutException: 5,000 milliseconds timeout on connection http-outgoing-95 [ACTIVE]
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.timeout(HttpAsyncRequestExecutor.java:387) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:92) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.client.InternalIODispatch.onTimeout(InternalIODispatch.java:39) ~[httpasyncclient-4.1.4.jar!/:4.1.4]
at org.apache.http.impl.nio.reactor.AbstractIODispatch.timeout(AbstractIODispatch.java:175) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionTimedOut(BaseIOReactor.java:261) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.timeoutCheck(AbstractIOReactor.java:502) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.validate(BaseIOReactor.java:211) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:280) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591) ~[httpcore-nio-4.4.13.jar!/:4.4.13]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

Can I configure the socket timeout in the Spring Boot application and if yes, where.

I tried configuring the socket timeout of the ClientConfiguration I create the RestHighLevelClient with which had no effect:

    ClientConfiguration clientConfiguration = ClientConfiguration.builder()
            .connectedTo(nodes)
            .withSocketTimeout(Duration.ofSeconds(30))
            .build();

    RestHighLevelClient client = RestClients.create(clientConfiguration).rest();
Actinomycin answered 30/1, 2020 at 17:19 Comment(1)
How do you configure your Rest Client? I have just tried this with a custom configuration (se docs.spring.io/spring-data/elasticsearch/docs/3.2.4.RELEASE/…) and it works as expectedRetaliate
U
7

In short, you should use setConnectTimeout method (when building RestClient).

So, here is the Java String Boot (2.1.1.RELEASE) configuration I have been using (successfully set connection and socket time outs) for ES (6.5v).

Please set your ENV variable (in .yml file) properly before directly using following source.

// yml settings

elasticsearch:
hosts:
host1: // you can set as an array with > sign
  name: hostname
  port: 9200
username:
password:
connectTimeout: 6000
socketTimeout: 6000

// Configuration class

@Configuration
public class ElasticSearchConfig {

  @Autowired
  private Environment environment;

  @Bean(destroyMethod = "close", name = "esHighLevelRestClient")
  public RestHighLevelClient highLevelClient() {
    return new RestHighLevelClient(restClientBuilder());
  }

  @Bean(destroyMethod = "close")
  public RestClient restClient() {
    return restClientBuilder().build();
  }

  private RestClientBuilder restClientBuilder() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(
                    environment.getProperty("elasticsearch.username"),
                    environment.getProperty("elasticsearch.password")));

    // you can set N hosts
    List<HttpHost> hosts = new ArrayList<>();
    hosts.add(new HttpHost(
            environment.getProperty("elasticsearch.hosts.host1.name", String.class),
            environment.getProperty("elasticsearch.hosts.host1.port", Integer.class),
            "http"));

    return RestClient.builder(Iterables.toArray(hosts, HttpHost.class))
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                    .setConnectTimeout(environment.getProperty("elasticsearch.connectTimeout", Integer.class))
                    .setSocketTimeout(environment.getProperty("elasticsearch.socketTimeout", Integer.class)))
            .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  }}

Hope it helps!

Unreeve answered 6/2, 2020 at 2:31 Comment(0)
C
2

This is how I set the socket connection timeout:

// Default is 30 sec, changed to 60 sec   

RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, scheme))
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(60 * 1000));
Cruzeiro answered 21/10, 2020 at 7:31 Comment(0)
G
0

My example in kotlin:



@Configuration
class ElasticConfig(private val props: ElasticsearchInfo) : AbstractElasticsearchConfiguration() {
    val port = 9200

    @Bean(name = ["restHighLevelClient", "elasticsearchClient"])
    override fun elasticsearchClient(): RestHighLevelClient {
        val timeout = Duration.of(props.timeout ?: 10, SECONDS)
        return create(builder()
            .connectedTo("${props.host}:$port")
            .withConnectTimeout(timeout)
            .withSocketTimeout(timeout)
            .build()).rest()
    }

    @Bean fun elasticsearchRestTemplate(): ElasticsearchRestTemplate = ElasticsearchRestTemplate(elasticsearchClient())
}
Geffner answered 15/12, 2020 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.