简体   繁体   English

Java Elasticsearch客户端结构

[英]Java Elasticsearch Client Structure

This is a structure question (what is best practice to use). 这是一个结构性问题(最佳实践是使用什么)。

I have 50 Servlets for a domain site that I manage where the client makes js calls to java Servlet which in turn calls ElasticSearch and then servlet responds to client. 我为一个域站点管理了50个Servlet,我在其中管理客户端向Java Servlet进行js调用的地方,而Java Servlet依次调用ElasticSearch,然后Servlet响应客户端。

In each of my servlets immediately after the class name I create a static settings and client config for my ES connection. 在紧随类名之后的每个Servlet中,为ES连接创建一个静态设置和客户端配置。 This is because in each servlet there may be 2 or more methods that call same ES cluster and I dont want to create this config in each method. 这是因为在每个servlet中,可能有2个或更多调用相同ES群集的方法,而我不想在每个方法中创建此配置。

/**
 * Servlet implementation class Results
 */
@WebServlet("/Results")
public class Results extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
    Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("143.79.236.xxx", 9300));

HOWEVER, now Im wondering should I do that 50 times (1 config at top of each class)? 但是,现在我想知道我应该做50次吗(每个类的顶部配置1次)? Or do I create a single new java class with the same config and in each servlet reference that one new java class? 还是我用相同的配置创建一个新的Java类,并在每个servlet引用中创建一个新的Java类? Does that save memory and better practice? 这样可以节省内存和更好的做法吗?

**--Inside name.java --**

public static Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
public static Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("143.79.236.xxx", 9300));

--Inside ALL methods in 50 servlets reference name.client instead-- -在50个servlet中的ALL方法内部引用name.client-

SearchResponse response = name.client.prepareSearch().setQuery(......

Also, at the bottom of my doPost (after I have sent out printWriter.println(results)) do I close the client? 另外,在doPost的底部(发送出printWriter.println(results)之后),我是否关闭客户端? client.close(); client.close();

Thank you. 谢谢。

I wouldn't store these objects on the class scope. 我不会将这些对象存储在类范围内。 I think it's better to open the client connection only at the beginning of your doPost, and close it at the end. 我认为最好只在doPost的开头打开客户端连接,然后在结尾关闭它。 In this way, the memory and network resources are only taken when necessary, and you don't have to take care about keeping a connection alive, keeping data in a good state. 这样,仅在必要时才占用内存和网络资源,而不必担心保持连接活动并保持数据处于良好状态。

Apart from that, if you want to avoid duplicating code in all your classes, just create a new class (kind of "helper", or "delegate", or name it whatever you like) that contains a method that does the connection job, and return the client. 除此之外,如果您想避免在所有类中重复代码,只需创建一个新类(包含“ helper”或“ delegate”,或随便命名),即可完成连接工作,并返回客户。 Again, without any class member, just everything in the method. 同样,没有任何类成员,只是方法中的所有内容。

More generally, if you want some more theory, I suggest reading on stateless programming Advantages of stateless programming? 更笼统地说,如果您想了解更多理论,我建议您阅读无状态编程。

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

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