SpringBoot整合Elasticsearch的Java Rest Client

尽量保持节奏吧

首先明确一点,SpringBoot自带的ES模板,不建议使用,建议使用Rest Client。如果业务简单,且无特殊要求,可以使用SpringBoot的模板ElasticsearchRepository来搞定。这个非常简单,这里不作介绍,有需要看最底下的连接

ElasticsearchRepository

  • 优点: 简单,SpringBoot无缝对接,配置简单
  • 缺点: 基于即将废弃的TransportClient, 不能支持复杂的业务

创建SpringBoot项目

IDEA 创建SpringBoot工程

如果不会创建,可以参考:https://www.jianshu.com/p/2101d176555b
maven 和 gradle都可以,建议使用 JDK使用1.8 因为Elasticsearch 的Java High Level REST Client 对java的版本要求是8。你们可以去官网查证。
【参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started-maven.html】

添加Rest Client依赖,增加配置

maven工程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 <!-- Java Low Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.3.2</version>
</dependency>
 <!-- Java High Level REST Client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>

gradle 工程

1
2
3
4
dependencies {
compile 'org.elasticsearch.client:elasticsearch-rest-client:6.3.2'
compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.3.2'
}

版本号请根据自己安装的ES版本酌情选择,因为ES5和ES6的API有些差别

编写单例Rest Low Level Client 和Rest High Level Client的bean

想用Spring 的IOC管理ES的连接客户端,可分下面几步

  • 配置ES节点
  • 配置Rest Client
  • 配置Rest High Level Client
  • 使用IOC注入

根据我从其他网站上查询的资料,Rest Client是长连接,而且内部有默认的线程池管理,因此一般无需自定义线程池管理连接。如果不对请指正

基于以上结论。先把连接点全部配置到配置文件中.(为了省事,直接一个数组搞定,有心的朋友可以注入成list+对象)

  1. 配置节点

    1
    elasticsearch.ip=127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200,127.0.0.4:9200,127.0.0.5:9200,127.0.0.6:9200
  2. 编写Config类,配置Rest Client和Rest High Level Client

请找一个pack,创建ElasticsearchRestClient 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
* Create By @author zing @date 2018/7/18 17:20
*/
@Slf4j
@Configuration
public class ElasticsearchRestClient {
/**
* 使用冒号隔开ip和端口
*/
@Value("${elasticsearch.ip}")
String[] ipAddress;
private static final int ADDRESS_LENGTH = 2;
private static final String HTTP_SCHEME = "http";
@Bean
public RestClient restClient() {
HttpHost[] hosts = Arrays.stream(ipAddress)
.map(this::makeHttpHost)
.filter(Objects::nonNull)
.toArray(HttpHost[]::new);
log.debug("hosts:{}", Arrays.toString(hosts));
return RestClient
.builder(hosts)
.build();
}
@Bean(name = "highLevelClient")
public RestHighLevelClient highLevelClient(@Autowired RestClient restClient) {
return new RestHighLevelClient(restClient);
}
private HttpHost makeHttpHost(String s) {
assert StringUtils.isNotEmpty(s);
String[] address = s.split(":");
if (address.length == ADDRESS_LENGTH) {
String ip = address[0];
int port = Integer.parseInt(address[1]);
return new HttpHost(ip, port, HTTP_SCHEME);
} else {
return null;
}
}
}

注:@Slf4j注解是lombok的日志注解,可以自行删除,切换成其他日志方式;Stream不会的朋友可以写成for循环,速度大约能快些

业务使用

强烈建议从TransportClient迁移到RestClient,因为业务压测发现TransportClient存在并发瓶颈。
在service里然后使用之前创建的highLevelClient呢?
demo如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Service
public class XXXServiceImpl implements XXXService {
@Autowired
RestHighLevelClient highLevelClient;
@Override
public boolean testEsRestClient(){
SearchRequest searchRequest = new SearchRequest("gdp_tops*");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("city", "北京市"));
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
try {
SearchResponse response = highLevelClient.search(searchRequest);
Arrays.stream(response.getHits().getHits())
.forEach(i -> {
System.out.println(i.getIndex());
System.out.println(i.getSource());
System.out.println(i.getType());
});
System.out.println(response.getHits().totalHits);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}

如果你没有Query DSL的基础,建议先学习ES的基本查询方法,如果有时间我可能会写一些,但是网上已经有N多入门教程。

RestClient其他详细使用方法参考我前面的文章:

Elasticsearch Java Rest Client 上手指南(上)

Elasticsearch Java Rest Client 上手指南(下)

想用自带SpringBoot光环的ElasticsearchRepository的可参考:https://juejin.im/post/5aec0b386fb9a07abb23784d

By: Max Zing
转载请注明出处:https://www.jianshu.com/p/0b4f5e41405e
如有错误,请不吝指正。谢谢
我的博客:https://micorochio.github.io/

转载请勿使用本文章推广任何自媒体!请勿追加任何媒体的二维码!