以下是我们正在使用的Maven版本
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>5.6.13</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.13</version>
</dependency>
private RestHighLevelClient buildRestClient(ElasticRequestVO elasticRequestVO) {
String elasticHost =
StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
: elasticSearchHost;
int elasticPort =
(elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
String elasticUser =
StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
: elasticSearchUser;
String elasticPassword =
StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
: elasticSearchPassword;
HttpHost host = new HttpHost(elasticHost, elasticPort);
RestClientBuilder restClientBuilder = RestClient.builder(host);
Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);
if (encodedAuth.isPresent()) {
Header[] requestHeaders =
new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
restClientBuilder.setDefaultHeaders(requestHeaders);
}
return new RestHighLevelClient(restClientBuilder.build());
}
如何关闭RestHighLevelClient?
答案 0 :(得分:0)
如果您将看到在最新版本之一中如何实现关闭,例如6.2.4您会发现RestHighLevelClient刚刚释放了RestClient。
因此,对于关闭,我可以为您提供只需调用restHighLevelClient.getLowLevelClient()
并关闭其余客户端lowLevelRestClient.close()
的功能。
P.S。如果只是看一下源代码,就会注意到这只是其余客户端的包装。
答案 1 :(得分:0)
要返回RestHighLevelClient
,请从buildRestClient()
重新调动RestClient
如果代码示例在这里
private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
String elasticHost =
StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
: elasticSearchHost;
int elasticPort =
(elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
String elasticUser =
StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
: elasticSearchUser;
String elasticPassword =
StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
: elasticSearchPassword;
HttpHost host = new HttpHost(elasticHost, elasticPort);
RestClientBuilder restClientBuilder = RestClient.builder(host);
Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);
if (encodedAuth.isPresent()) {
Header[] requestHeaders =
new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
restClientBuilder.setDefaultHeaders(requestHeaders);
}
return restClientBuilder.build();
}
答案 2 :(得分:0)
我的RestHighLevelClient版本是5.6.3,找不到像restHighLevelClient.getLowLevelClient()或restHighLevelClient.close()这样的方法,所以我使用反射来使restClient关闭
Field restClientField = RestHighLevelClient.class.getDeclaredField("client");
restClientField.setAccessible(true);
RestClient restclient = (RestClient)restClientField.get(restHighLevelClient);
restClient.close();
答案 3 :(得分:0)
我有同样的问题。 要关闭连接,您必须更改方法:
EventBus.$on('Logged-Out',() => {
this.isUserLoggedIn = false
localStorage.setItem('JWT', '')
})
EventBus.$on('Logged-in',() => {
this.isUserLoggedIn = true
})
然后,当您调用方法时,就可以使用
private RestClient buildRestClient(ElasticRequestVO elasticRequestVO) {
String elasticHost =
StringUtils.isNotEmpty(elasticRequestVO.getElasticHost()) ? elasticRequestVO.getElasticHost()
: elasticSearchHost;
int elasticPort =
(elasticRequestVO.getElasticPort() != 0) ? elasticRequestVO.getElasticPort() : elasticSearchPort;
String elasticUser =
StringUtils.isNotEmpty(elasticRequestVO.getElasticUser()) ? elasticRequestVO.getElasticUser()
: elasticSearchUser;
String elasticPassword =
StringUtils.isNotEmpty(elasticRequestVO.getElasticPassword()) ? elasticRequestVO.getElasticPassword()
: elasticSearchPassword;
HttpHost host = new HttpHost(elasticHost, elasticPort);
RestClientBuilder restClientBuilder = RestClient.builder(host);
Optional<String> encodedAuth = getAuthenticationHeader(elasticUser, elasticPassword);
if (encodedAuth.isPresent()) {
Header[] requestHeaders =
new Header[] {new BasicHeader(org.apache.http.HttpHeaders.AUTHORIZATION, encodedAuth.get())};
restClientBuilder.setDefaultHeaders(requestHeaders);
}
return restClientBuilder.build();
}
然后做
final RestClient restClient=buildRestClient(...);
在需要时。 最后,关闭。
示例
new RestHighLevelClient(restClient)