如何在Spring Data Elasticsearch 2.2.3中配置Rest高级客户端的套接字超时
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Spring Data Elasticsearch 2.2.3中配置Rest高级客户端的套接字超时相关的知识,希望对你有一定的参考价值。
我在Elasticsearch v6.8.6中使用Spring Boot Starter Data Elasticsearch 2.2.3.RELEASE。我为连接到群集配置了RestHighLevelClient。
现在,我不断在不同的操作上收到SocketTimeoutException:
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]
我可以在Spring Boot应用程序中配置套接字超时吗,如果可以,在哪里。
我尝试配置ClientConfiguration的套接字超时,我创建了RestHighLevelClient,该超时不起作用:
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(nodes)
.withSocketTimeout(Duration.ofSeconds(30))
.build();
RestHighLevelClient client = RestClients.create(clientConfiguration).rest();
总之,您应该使用
setConnectTimeout
方法(RestClient)。
因此,这是我一直在为ES(6.5v)使用的Java String Boot(2.1.1.RELEASE)配置(成功设置了连接和套接字超时)。
在直接使用以下来源之前,请正确设置您的ENV变量(在.yml文件中)。>
// yml设置
elasticsearch: hosts: host1: // you can set as an array with > sign name: IP port: 9200 username: password: connectTimeout: 6000 socketTimeout: 6000
//配置类
@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));
希望有帮助!
以上是关于如何在Spring Data Elasticsearch 2.2.3中配置Rest高级客户端的套接字超时的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Spring Boot 应用程序的同一个域类上同时使用 Spring Data JPA 和 Spring Data Elasticsearch 存储库?
如何在 Spring-Data-Rest 中实现细粒度的访问控制?
如何在 Spring Data 存储库上测试 Spring 的声明式缓存支持?