gpt4 book ai didi

elasticsearch - RestHighLevelClient 是否保持连接打开?

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

我想用RestHighLevelClient在不同集群上使用 Cross Cluster mechanizem 不支持的命令(例如关闭和打开索引)。
我的问题是我是否使用了多个 RestHighLevelClient 的实例对于每个集群,它将为每个集群保持连接打开? (以确保我没有阻塞应用程序)

最佳答案

通过查看各种资源,好像RestHighLevelClient除非您明确调用 client.close();,否则保持连接打开在上面。
来自 official RestHighLevelClient initialization

The high-level client will internally create the low-level client usedto perform requests based on the provided builder. That low-levelclient maintains a pool of connections and starts some threads so youshould close the high-level client when you are well and truly donewith it and it will in turn close the internal low-level client tofree those resources. This can be done through the close method:


在您的情况下,如果您有很多 ES 集群并创建多个 RestHighLevelClient与您猜测的相比,它可能会由于线程及其资源的占用而阻塞您的应用程序,因此您应该显式调用 close当您再次创建它时需要更多时间,但在大多数情况下不会阻塞您的应用程序。
我建议您对您的应用程序进行一些资源基准测试,并根据您的权衡选择最佳方法。
  • 创建多个客户端,不要关闭它们,而是分配更多资源,以便应用程序运行速度快且不会卡住。
  • close客户端频繁,这不需要过度分配资源,但是当您为请求创建新客户端时,延迟会更多。
  • 关于elasticsearch - RestHighLevelClient 是否保持连接打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64410714/

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