简体   繁体   English

Elasticsearch传输客户端连接

[英]Elasticsearch Transport Client connection

I'm building a Search Web Application using Servlet which connects to Elasticsearch. 我正在使用连接到Elasticsearch的Servlet构建搜索Web应用程序。 I have a question regarding the Transport module of Elasticsearch. 我有一个关于Elasticsearch的Transport模块的问题。 I'm opening the connection to Elasticsearch using the TransportClient in the class that implements ServletContextListener. 我正在使用实现ServletContextListener的类中的TransportClient打开与Elasticsearch的连接。 Below is the code for ElasticsearchServletContextListener class. 下面是ElasticsearchServletContextListener类的代码。

public class ElasticsearchContextListener implements ServletContextListener {

  @Override
  public void contextInitialized(ServletContextEvent servletContextEvent) {
    System.out.println("Starting up!");

    try {
      Client client = TransportClient.builder().build().addTransportAddress(
          new InetSocketTransportAddress(InetAddress.getByName("IP-address"),9300));
    //Storing the client connection in a static variable
      Parameters.setESclient(client);

    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  @Override
  public void contextDestroyed(ServletContextEvent servletContextEvent) {
    Parameters.getESclient().close();
    System.out.println("Shutting down!");
  }
}

Now whenever the user searches for a query it uses the same 'client' connection that is initialized in the ServletContextListener class. 现在,只要用户搜索查询,它就会使用在ServletContextListener类中初始化的相同“客户端”连接。 Can the client connection handle multiple requests at the same time? 客户端连接可以同时处理多个请求吗? Or does every user need a separate client connection to query the elasticsearch? 或者每个用户是否需要单独的客户端连接来查询elasticsearch? Thank you for your assistance. 谢谢您的帮助。

That Client instance is able to handle multiple calls and handle multiple threads. Client实例能够处理多个调用并处理多个线程。 And you should have one Client instance only as it's expensive to create one. 而且你应该只有一个Client实例,因为它创建一个很昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM