栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

@Configurationpublic 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<Object> 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);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378326.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号