本文是基于SpringBoot2.4.2的版本对ElasticSearch6.8.1的版本进行封装使用
SpringBoot整合ElasticSearch-v6.8.1的封装 - 2.接口类
SpringBoot整合ElasticSearch-v6.8.1的封装 - 3.支持类
SpringBoot整合ElasticSearch-v6.8.1的使用 - 4.索引的创建与删除
SpringBoot整合ElasticSearch-v6.8.1的使用 - 5.索引的数据(增删改)
SpringBoot整合ElasticSearch-v6.8.1的使用 - 6.索引的数据(查询)
依赖
1 | <properties> |
配置
ElasticSearchDataConfig
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
37import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
/**
* author :demon
* date :2022 2022/3/18 15:44
* desc :ES的bean
*/
@Data
@Component(value = "elasticSearchDataConfig")
@ConditionalOnMissingClass(value = "elasticSearchDataConfig")
public class ElasticSearchDataConfig {
// 服务器地址
private List<String> httpHost = Collections.singletonList("http://127.0.0.1:9200");
// ES用户名
private String username;
// ES密码
private String password;
// 设置连接超时时间,单位毫秒。指的是连接一个url的连接等待时间,单位毫秒。
private Integer connectTimeout = 5000;
// 请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用,单位毫秒。
private Integer socketTimeout = 60000;
// 设置从connect Manager获取Connection 超时时间,单位毫秒。
private Integer connectionRequestTimeout = 60000;
// httpclient最大连接数
private Integer maxConnTotal = 30;
// httpclient最大路由连接数
private Integer maxConnPerRoute = 10;
// httpclient保活策略
private Integer keepAliveMinutes = 10;
}ElasticSearchConfig
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
88import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
/**
* author :demon
* date :2021 2021/7/20 18:33
* desc :ES 配置项
* https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.8/java-rest-high-document-index.html#java-rest-high-document-index
*/
@Slf4j
@Configuration
public class ElasticSearchConfig {
@Resource(name = "elasticSearchDataConfig")
private ElasticSearchDataConfig elasticSearchDataConfig;
@Bean(destroyMethod = "close", name = "restHighLevelClient")
public RestHighLevelClient restHighLevelClient() {
// 创建链接
RestClientBuilder builder;
List<String> httpHostList = elasticSearchDataConfig.getHttpHost();
if (httpHostList.size() > 1) {
HttpHost[] httpHosts = new HttpHost[httpHostList.size()];
for (int i = 0; i < httpHostList.size(); i++) {
httpHosts[i] = HttpHost.create(httpHostList.get(i));
}
log.info("ES模式:集群({})", Collections.singletonList(httpHostList).toArray());
// 集群创建
builder = RestClient.builder(httpHosts);
} else {
String host = httpHostList.get(0);
log.info("ES模式:单机({})", Collections.singletonList(host).toArray());
// 单机创建
builder = RestClient.builder(HttpHost.create(host));
}
// 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
// 设置连接超时时间,单位毫秒。指的是连接一个url的连接等待时间
.setConnectTimeout(elasticSearchDataConfig.getConnectTimeout())
// 请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
.setSocketTimeout(elasticSearchDataConfig.getSocketTimeout())
// 设置从connect Manager获取Connection 超时时间,单位毫秒。
.setConnectionRequestTimeout(elasticSearchDataConfig.getConnectionRequestTimeout())
)
// 使用异步httpclient时设置并发连接数
.setHttpClientConfigCallback(httpClientBuilder -> {
// httpclient连接数配置
httpClientBuilder
// 最大连接数
.setMaxConnTotal(elasticSearchDataConfig.getMaxConnTotal())
// 最大路由连接数
.setMaxConnPerRoute(elasticSearchDataConfig.getMaxConnPerRoute())
// httpclient保活策略
.setKeepAliveStrategy(CustomConnectionKeepAliveStrategy.getInstance(elasticSearchDataConfig.getKeepAliveMinutes()))
// 没有密码可以不用这一个set
.disableAuthCaching().setDefaultCredentialsProvider(getCredentialsProvider())
;
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
private CredentialsProvider getCredentialsProvider() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//如果没配置密码就可以不用下面这行
String username = elasticSearchDataConfig.getUsername();
String password = elasticSearchDataConfig.getPassword();
if (username != null && password != null) {
System.out.println("★★★★★ restHighLevelClient 用户验证★★★★★");
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
return credentialsProvider;
}
}
连接策略
1 | import org.apache.http.HttpResponse; |