gpt4 book ai didi

spring - Spring Data Elasticsearch 2.2.3.RELEASE如何配置Rest高级客户端的socket超时

转载 作者:行者123 更新时间:2023-12-02 22:21:25 27 4
gpt4 key购买 nike

我将 Spring Boot Starter Data Elasticsearch 2.2.3.RELEASE 与 Elasticsearch v6.8.6 一起使用。我配置了 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();

最佳答案

In short, you should use setConnectTimeout method (when building 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: hostname
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 - Spring Data Elasticsearch 2.2.3.RELEASE如何配置Rest高级客户端的socket超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59991283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com