ElasticSearch为什么在客户端或节点上调用close()? [英] ElasticSearch why call close() on client or node?

查看:181
本文介绍了ElasticSearch为什么在客户端或节点上调用close()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有理由在客户端或节点上调用 close()?如果没有,会有什么影响?

Is there a reason why you would call close() on a client or node ? What are the impacts if you do not ?

Node node = nodeBuilder().client(true).node();
Client client = node.client();
// index some stuff
client.close(); // <-- why do this ?
node.close(); // <-- why do this ?

我们有一个与我们的应用程序匹配的生命周期的单个客户端。我想知道我们是否需要在应用程序删除时调用 client.close() node.close()

We have a single client that has a life-cycle matching our application. Am wondering if we need bother calling client.close() and or node.close() at application tear down ?

推荐答案

考虑这个代码片段:

// on startup

Node node = nodeBuilder().node();
Client client = node.client();

// on shutdown

node.close();

根据文档,当您创建一个节点它连接本地弹性搜索集群。这意味着自己向集群中的其他节点发布...并且可能开始监听来自其他节点或客户端的请求。

According to the documentation, when you create a Node it joins the local elastic search cluster. That means that announces itself to the other nodes in in the cluster ... and presumably starts listening for requests from other nodes or clients.

当您的应用程序退出时, JVM终止,节点显然不能处理来自其他节点的请求。

When your application exits, and the JVM terminates, the node obviously cannot handle requests from other nodes anymore.


  • 如果您的应用程序如上所述调用 node.close()该节点将告诉其他节点它将要消失,以便其他节点将知道不再发送任何请求。

  • If your application calls node.close() as above, the node will tell the other nodes that it is going away, so that the other nodes will know not to send any more requests.

如果您的应用程序调用 node.close() ,那么其他节点就不知道这个节点已经消失了,可能会继续发送它的请求。这些请求将失败,可能导致异常,日志消息等。至少,向节点发送无法响应的请求是浪费时间/资源。

If your application doesn't call node.close(), then the other nodes won't know that this node has gone away, and may continue to send it requests. These requests will fail, potentially resulting in exceptions, log messages and so on. At the very least, sending requests to node that cannot respond is a waste of time / resources.

显然,这个如果您的应用程序是集群中唯一的节点,那么这个方法是不成功但是,假设这个假设是一个坏主意,并且它可能值得调用 close()的小开销(至少)当前应用程序的 Node 对象。

Obviously, this is moot if your application is the only node in the cluster. However, it is a bad idea to make that assumption, and it is probably worth the small overhead of calling close() on (at least) the current application's Node object.

这篇关于ElasticSearch为什么在客户端或节点上调用close()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆