城市天际线,ElasticSearch入门笔记(6)--Java LowLevelRestClient,象山天气预报

之前几篇介绍了ES入门级其他常识,这一篇介绍一下运用Java和ES通讯。

ElasticSearch有一种TransportClient和两种Java Rest Client:LowLevelRestClient和HighLevelRestClient,本篇先介绍一下Client:LowLevelRestClient。

LowLevelRestClient

LowLevelRestClient直接经过Http的办法拜访ElasticSearch集群,将恳求的编列和呼应的反编列留给用户自行输出,并且兼容一切版别的ElasticSearch。

LowLevelRestClient的特色

JLowLevelRestClient首要包含以下这些特色

  • 最小化依靠
  • 负载均衡
  • 节点毛病和特定呼应的毛病搬运
  • 衔接失利赏罚
  • 耐久化衔接
  • 恳求和相应的盯梢日志
  • 可选的集群节点发现

Java中运用LowLevelRestClient

增加LowLevelRestClient的包,POM.XML里边参加一下依靠:


org.elasticsearch.client
elasticsearch-rest-client
6.4.2

为了避免版别抵触的问题,还需求运用maven-shade-plugin插件:




org.apache.maven.plugins
maven-shade-plugin
3.1.0


package
shade



org.apache.http
hidden.org.apache.http


org.apache.logging
hidden.org.apache.logging


org.apache.commons.codec
hidden.org.apache.commons.codec


org.apache.commons.logging
hidden.org.apache.commons.logging








plugins和dependencies相同,能够运用manager进行一致版别办理,这儿因为代码篇幅太长就不写了。

实例化一个RestClient

先来简略检查一下RestClient这个类

RestClient经过`RestClientBuilder.build()`办法来进行实例化。

RestClientBuilder的结构办法是RestClientBuilder(List nodes),前面没有润饰也就是说是`default`的,`经过`RestClient.builder(Node... nodes)`或许RestClient.builder(HttpHost... hosts)这两个静态办法进行实例化。

RestClient需求调用RestClient.close()开释HttpClient。

RestClientBuilder能够设定下面这些参数

  • defaultHeaders 默许的Header
  • failureListener 恳求失利的Listener
  • maxRetryTimeout 最大重试的Timeout
  • pathPrefix 恳求的前缀
  • nodeSelector 用来挑选恳求节点
  • httpClientConfigCallback 答应用户定制HttpClient
  • requestConfigCallback 答应用户定制Request

下面是在SpringBoot中注入RestClient的一个比如

@Bean
public RestClient restClient() {
RestClientBuilder restClientBuilder = restClientBuilder();
return restClientBuilder == null ? null : restClientBuilder.build();
}
private RestClientBuilder restClientBuilder() {
HttpHost[] httpHosts = buildHttpHosts();
if (httpHosts == null || httpHosts.length == 0 ) {
return null;
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
restClientBuilder.setDefaultHeaders(buildHeaders());
restClientBuilder.setFailureListener(buildFailureListener());
restClientBuilder.setMaxRetryTimeoutMillis(10000);
restClientBuilder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
// 暂时不需求
// restClientBuilder.setPathPrefix("");
// restClientBuilder.setHttpClientConfigCallback(buildHttpClientConfigCallback());
// restClientBuilder.setRequestConfigCallback(buildRequestConfigCallback());
return restClientBuilder;
}
private HttpHost[] buildHttpHosts() {
List esNodes = null;
if (elasticSearchInfo == null || (esNodes = elasticSearchInfo.getNodes()) == null || esNodes.isEmpty()) {
log.warn("没有可用的ElasticSearch节点。");
return null;
}
log.info("ElasticSearch节点: "+esNodes);
HttpHost[] httpHosts = new HttpHost[esNodes.size()];
httpHosts = esNodes.stream()
.map(node -> new HttpHost(node.getHostname(), node.getPort(), node.getSchema()))
.collect(Collectors.toList()).toArray(httpHosts);
return httpHosts;
}
private Header[] buildHeaders() {
Header[] defaultHeaders = new Header[]{new BasicHeader("Content-Type", "application/json")};
return defaultHeaders;
}
private FailureListener buildFailureListener() {
FailureListener failueListener = new FailureListener() {
@Override
public void onFailure(Node node) {
log.info(String.format("ElasticSearch通讯失利: nodename=%s, host=%s, port=%d, version=%s", node.getName(),
node.getHost().getHostName(), node.getHost().getPort(), node.getVersion()));
super.onFailure(node);
}

};
return failueListener;
}
private HttpClientConfigCallback buildHttpClientConfigCallback() {
return new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder;
}
};
}
private RequestConfigCallback buildRequestConfigCallback() {
return new RequestConfigCallback() {
@Override
public Builder customizeRequestConfig(Builder requestConfigBuilder) {
return requestConfigBuilder;
}
};
}
@Bean(destroyMethod="destroy")
public ElasticSearchRepository elasticSearchRepository(RestClient restClient) {
return ElasticSearchRepositoryImpl.of(restClient);
}

最终注入的ElasticSearchRepository指定了destroy办法,在bean毁掉时调用destroy(),destroy()中调用RestClient.close()

ElasticSearchRepositoryImpl中的代码:

private RestClient restClient;
private ElasticSearchRepositoryImpl(RestClient restClient) {
this.restClient = restClient;
}
public static ElasticSearchRepository of(RestClient restClient) {
Objects.requireNonNull(restClient, "restClient不能是NULL");
return new ElasticSearchRepositoryImpl(restClient);
}
public void destroy() {
log.warn("正在封闭restClient");
if (restClient == null) {
return;
}
try {
restClient.close();
} catch (IOException e) {
log.warn("无法封闭restClient");
}
}

最终

欢迎感兴趣的童鞋们点赞,重视,转发,保藏,一同学习Elasticsearch。