史上最简单的elasticsearch整合教程

昨天给大家介绍了一下ElasticSearch代码结构设计,今天给大家介绍一下ElasticSearch怎么集成到Spring boot中,不知道大家记不记得以前我也写过它们的整合,但今天要说的模式和以前的不一样(具体区别大家可以对照的看看,这边就不详细介绍了)。

1.pom依赖

我们首先引入ElasticSearch的相关依赖,pom文件的依赖如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- elasticsearch  client依赖 -->

<dependency>

<groupId>org.elasticsearch.client</groupId>

<artifactId>transport</artifactId>

<version>6.2.2</version>

</dependency>

<!-- elasticsearch依赖 -->

<dependency>

<groupId>org.elasticsearch</groupId>

<artifactId>elasticsearch</artifactId>

<version>6.2.2</version>

</dependency>

2.yml配置

然后再配置文件中配置ElasticSearch的相关配置,yml配置文件如下所示:

1
2
3
4
5
6
7
elasticsearch:

cluster:

name: my-application

address: 192.168.11.24:9300

3.配置类

创建一个配置类(@Configuration),然后添加ElasticSearch相关的连接信息,配置类如下所示:

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.infun.platform.es.config;

import org.apache.commons.lang.StringUtils;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.TransportAddress;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.util.ArrayList;

import java.util.List;

import java.util.ResourceBundle;

/**

* Elasticsearch配置类

* @author linzhiqiang

* @date 2018/5/26

* @since 1.0

*/

@Configuration

public class ElasticsearchConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);

@Value("${elasticsearch.cluster.name}")

private String clusterName;

@Value("${elasticsearch.address}")

private String address;

@Bean

public TransportClient getTransportClient() {

try {

// 设置集群名称,并自动嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中

// settings

Settings settings = Settings.builder().put("cluster.name", clusterName)

.build();

//地址列表

List<TransportAddress> transportAddressList = new ArrayList<>();

if (StringUtils.isNotBlank(address)) {

String[] addresses = address.split(",");

String[] hostAndPort = null;

//组装地址

for (String str : addresses) {

hostAndPort = str.split(":");

if (hostAndPort != null && hostAndPort.length > 1) {

transportAddressList.add(new TransportAddress(InetAddress.getByName(hostAndPort[0].trim()), Integer.valueOf(hostAndPort[1].trim())));

} else {

transportAddressList.add(new TransportAddress(InetAddress.getByName(hostAndPort[0].trim()), 9300));

}

}

}

//转换

TransportAddress[] transportAddresses = transportAddressList.toArray(new TransportAddress[transportAddressList.size()]);

//返回连接

return new PreBuiltTransportClient(settings).addTransportAddresses(transportAddresses);

} catch (UnknownHostException e) {

e.printStackTrace();

LOGGER.error(e.getMessage());

}

return null;

}

}

4.注入TransportClient

配置类搞定,我们就可以在service中注入TransportClient 的连接了,使用方式如下所示

1
2
3
4
5
6
7
8
9
/**

* TransportClient连接

*/

@Autowired

private TransportClient getTransportClient;

总结

过程就是这样,这样的整合方式和之前的整合方式最大的区别就是,现在这种的只是将elasticsearch的配置信息放在yml配置文件中,elasticsearch代码风格还是一样的,但是spring-data-elasticsearch这种的就不一样了,它的代码风格就像JPA这种模式了,虽然封装的很好,但是很不灵活。两者之间各自有各自的优缺点吧,看具体应用场景适合什么。

更多内容敬请关注:“林老师带你学编程”

林老师带你学编程 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!