背景
设置RestHighLevelClient的超时间,防止请求时间过长,导致接口访问时间过长
es官方设置
Timeouts
Configuring requests timeouts can be done by providing an instance of RequestConfigCallback
while building the RestClient
through its builder. The interface has one method that receives an instance of org.apache.http.client.config.RequestConfig.Builder
as an argument and has the same return type. The request config builder can be modified and then returned. In the following example we increase the connect timeout (defaults to 1 second) and the socket timeout (defaults to 30 seconds).
RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200))
.setRequestConfigCallback(
new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(
RequestConfig.Builder requestConfigBuilder) {
return requestConfigBuilder
.setConnectTimeout(5000)
.setSocketTimeout(60000);
}
});
Timeouts also can be set per request with RequestOptions, which overrides RestClient customizeRequestConfig.
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(60000)
.build();
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
.setRequestConfig(requestConfig)
.build();
详细解释
RequestConfig 查看所在的包是httpclient的并不是elasticsearch的,然后就又查了查相关的超时含义
RequestConfig有三个超时如下
ConnectTimeout
设置连接超时时间,单位毫秒。指的是连接一个url的连接等待时间。比如连google.报错如下
org.apache.http.conn.ConnectTimeoutException: Connect to www.google.com:80 [www.google.com/203.98.7.65] failed: connect timed out
SocketTimeout
请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。指的是连接上一个url,获取response的返回等待时间。
ConnectionRequestTimeout
设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
https://blog.csdn.net/qq_18746961/article/details/118764208
标签:JAVA,builder,RequestConfig,connect,设置,RestHighLevelClient,超时 From: https://www.cnblogs.com/eternityz/p/17044612.html