前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于RestHighLevelClient

关于RestHighLevelClient

作者头像
只喝牛奶的杀手
发布2020-05-22 11:04:18
4.8K0
发布2020-05-22 11:04:18
举报

RestHighLevelClient从字面意思理解就是restful风格的高级别的客户端,看一下Elastic官网怎么定义的:

The high-level client will internally create the low-level client used to perform requests based on the provided builder. That low-level client maintains a pool of connections and starts some threads so you should close the high-level client when you are well and truly done with it and it will in turn close the internal low-level client to free those resources. This can be done through the close().

RestHighLevelClient 底层封装的是一个http连接池,当需要执行 update、index、delete操作时,直接从连接池中取出一个连接,然后发送http请求到ElasticSearch服务端,服务端基于Netty接收请求。

新版本的elasticsearch java client 都推荐用RestHighLevelClient去连接ES集群,放弃掉之前的transport client的方式。下面是构建RestHighLevelClient的方法:

代码语言:javascript
复制
protected void buildRestHighLevelClient() {
      final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
      credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
      HttpHost[] httpHosts = Arrays.stream(elasticSearchHosts.split(",")).map(host -> new HttpHost(host, elasticSearchPort)).filter(Objects::nonNull).toArray(HttpHost[]::new);
      RestClientBuilder builder = RestClient.builder(httpHosts)
              .setHttpClientConfigCallback(httpClientBuilder -> {
                  RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                          .setConnectTimeout(connectTimeout)
                          .setSocketTimeout(socketTimeout)
                          .setConnectionRequestTimeout(connectionRequestTimeout);
                  httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
                  httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                  return httpClientBuilder;
              });
      restHighLevelClient = new RestHighLevelClient(builder);
  }

RestHighLevelClient归根结底也是HttpClient 我们以4.5版本api来说,配置方式:设置在RequestConfig中

  • connectTimeout:设置连接超时时间,单位毫秒。指的是连接目标url的连接超时时间,即客服端发送请求到与目标url建立起连接的最大时间。如果在该时间范围内还没有建立起连接,会抛出connectionTimeOut异常。
  • connectionRequestTimeout:设置从connect Manager(连接池)获取Connection 超时时间,单位毫秒。HttpClient中的要用连接时尝试从连接池中获取,若是在等待了一定的时间后还没有获取到可用连接(比如连接池中没有空闲连接了)则会抛出获取连接超时异常。
  • socketTimeout:请求获取数据的超时时间(即响应时间),单位毫秒。即在与目标url建立了连接之后,等待返回数据的时间的最大时间,如果超出时间没有响应,就会抛出socketTimeoutException异常。

RestHighLevelClient可以灵活构建出各种Request对象,这个在这里就不再说具体API的用法了。当然抽象层级越高的东西性能相对差一点,但更能满足企业级开发的需求。


每周一句:奉献和热爱让人生一往无前,方法和勤奋能让你变得优秀,特长和兴趣能让你变得卓越。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 只喝牛奶的杀手 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档