[JAVA]RestHighLevelClient的超时设置
背景
设置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 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理