使用Elastic Search 5.5.0获得最佳性能时如何正确关闭原始RestClient?

我使用Spring Boot 1.5.4.RELEASE Microservice使用ElasticSearch提供的低级Rest Client连接到ElasticSearch 5.5.0实例。

的pom.xml

 org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE     org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-test test    org.elasticsearch elasticsearch 5.5.0   org.elasticsearch.client transport 5.5.0    org.apache.commons commons-lang3 3.6    com.fasterxml.jackson.core jackson-core 2.8.9   com.fasterxml.jackson.core jackson-databind 2.8.9   com.fasterxml.jackson.core jackson-annotations 2.8.9    log4j log4j 1.2.17    junit junit 4.11 test    io.springfox springfox-swagger2 2.6.1 compile   io.springfox springfox-swagger-ui 2.6.1 compile   

一切都设置正确,但在一堆点击后,客户端应用程序报告HTTP 500错误,这是日志文件中出现的:

 java.io.IOException: Too many open files at sun.nio.ch.IOUtil.makePipe(Native Method) ~[na:1.8.0_141] at sun.nio.ch.EPollSelectorImpl.(EPollSelectorImpl.java:65) ~[na:1.8.0_141] at sun.nio.ch.EPollSelectorProvider.openSelector(EPollSelectorProvider.java:36) ~[na:1.8.0_141] at java.nio.channels.Selector.open(Selector.java:227) ~[na:1.8.0_141] at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.(AbstractMultiworkerIOReactor.java:142) ~[httpcore-nio-4.4.5.jar!/:4.4.5] at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.(DefaultConnectingIOReactor.java:79) ~[httpcore-nio-4.4.5.jar!/:4.4.5] at org.apache.http.impl.nio.client.IOReactorUtils.create(IOReactorUtils.java:43) ~[httpasyncclient-4.1.3.jar!/:4.1.3] at org.apache.http.impl.nio.client.HttpAsyncClientBuilder.build(HttpAsyncClientBuilder.java:666) ~[httpasyncclient-4.1.3.jar!/:4.1.3] at org.elasticsearch.client.RestClientBuilder.createHttpClient(RestClientBuilder.java:202) ~[rest-5.5.0.jar!/:5.5.0] at org.elasticsearch.client.RestClientBuilder.build(RestClientBuilder.java:180) ~[rest-5.5.0.jar!/:5.5.0] at com.myapp.controller.SearchController.getSearchQueryResults(SearchController.java:94) ~[classes!/:1.0] 

在SearchController内部(//注释后第二行是第94行):

 @RestController @RequestMapping("/api/v1") public class SearchController { @RequestMapping(value = "/search", method = RequestMethod.GET, produces="application/json" ) public ResponseEntity getSearchQueryResults(@RequestParam(value = "criteria") String criteria) throws IOException { // Setup HTTP Headers HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); // Setup RestClient RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)) .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000); } }).setMaxRetryTimeoutMillis(60000).build(); // Setup query and send and return ResponseEntity... } } 

显而易见的是,在调用restClient.performRequest()方法之后它从未关闭过……

所以,我把它放到我的代码中:

 Response response = null; try { // Submit Query and Obtain Response response = restClient.performRequest("POST", endPoint, Collections.singletonMap("pretty", "true"), entity); } catch (IOException e) { LOG.error("\n\n\tException: " + e + "\n\n"); e.printStackTrace(); } finally { restClient.close(); } 

阅读Elastic Search的文档,了解RestClient类是线程安全的……

另外,请阅读有关restClient.performRequestAsync()方法的信息,但我对线程缺乏经验,文档中的描述含糊不清。

问题(S):

  1. 我的解决方案是处理和关闭一堆套接字资源的最佳方法吗?

  2. 如果有人可以向我展示一个更好的方法来使用低级RestClient和弹性搜索,我会很感激,因为它不会导致套接字资源没有被释放导致HTTP 500的相同问题。我是否应该使用restClient.performRequestAsync ? 有人可以提供一个例子吗?

感谢您抽出时间来阅读…

在每个请求上创建一个RestClient并不是一个好习惯。 您应该通过配置bean创建一个单独的实例,如下所示:

 @Configuration public class ElasticsearchConfig { @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port}") private int port; @Bean public RestClient restClient() { return RestClient.builder(new HttpHost(host, port)) .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000); } }).setMaxRetryTimeoutMillis(60000).build(); } } 

然后在您的SearchController类中,您可以像这样注入它(并且还可以在容器关闭时添加一个清理方法来关闭restClient实例):

 @RestController @RequestMapping("/api/v1") public class SearchController { @Autowired private RestClient restClient; @RequestMapping(value = "/search", method = RequestMethod.GET, produces="application/json" ) public ResponseEntity getSearchQueryResults(@RequestParam(value = "criteria") String criteria) throws IOException { // Setup HTTP Headers HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json"); // Setup query and send and return ResponseEntity... Response response = this.restClient.performRequest(...); } @PreDestroy public void cleanup() { try { logger.info("Closing the ES REST client"); this.restClient.close(); } catch (IOException ioe) { logger.error("Problem occurred when closing the ES REST client", ioe); } } }