首页 归档 关于 文件 Github
×

SpringBoot整合ElasticSearch-v6.8.1的封装 - 1.配置类

2022-03-04 17:32:32
ElasticSearch
  • SpringBoot
本文总阅读量(次):
本文字数统计(字):1.4k
本文阅读时长(分):7

本文是基于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
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
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
<spring-boot.version>2.4.2</spring-boot.version>
<elasticsearch-client.version>6.8.1</elasticsearch-client.version>
</properties>

<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch-client.version}</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch-client.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch-client.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client-sniffer</artifactId>
<version>${elasticsearch-client.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>

</dependencies>

配置

  • 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
    37
    import 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
    88
    import 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
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
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.protocol.HttpContext;
import java.util.concurrent.TimeUnit;
/**
* author :demon
* date :2021 2021/9/12 23:40
* desc :连接策略
*/
public class CustomConnectionKeepAliveStrategy extends DefaultConnectionKeepAliveStrategy {
private static CustomConnectionKeepAliveStrategy INSTANCE;
private static long maxKeepAliveMinutes;
// 带参数构造器
private CustomConnectionKeepAliveStrategy(long maxKeepAliveMinutes) {
super();
CustomConnectionKeepAliveStrategy.maxKeepAliveMinutes = maxKeepAliveMinutes;
}
// 为了传参数进来写了个单例
public static CustomConnectionKeepAliveStrategy getInstance(long maxKeepAliveMinutes) {
if (INSTANCE == null) {
synchronized (CustomConnectionKeepAliveStrategy.class) {
if (INSTANCE == null) {
INSTANCE = new CustomConnectionKeepAliveStrategy(maxKeepAliveMinutes);
}
}
}
return INSTANCE;
}
/**
* 最大keep alive的时间(分钟)
* 这里默认为10分钟,可以根据实际情况设置。
* 可以观察客户端机器状态为TIME_WAIT的TCP连接数,如果太多,可以增大此值。
* 小于0为无限期keepalive,将无限期替换成一个默认的时间
*/
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long keepAliveDuration = super.getKeepAliveDuration(response, context);
if (keepAliveDuration < 0) {
return TimeUnit.MINUTES.toMillis(maxKeepAliveMinutes);
} else {
return keepAliveDuration;
}
}
}
完
SpringBoot整合ElasticSearch-v6.8.1的封装 - 2.接口类
ElasticSearch的数据类型

本文标题:SpringBoot整合ElasticSearch-v6.8.1的封装 - 1.配置类

文章作者:十二

发布时间:2022-03-04 17:32:32

最后更新:2022-03-24 15:14:25

原始链接:https://www.zhuqiaolun.com/2022/03/1646386352631/1646386352631/

许可协议:署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

头像

十二

我想起那天夕阳下的奔跑,那是我逝去的青春。

分类

  • Blog4
  • ElasticSearch13
  • Freemarker2
  • Git2
  • Go-FastDfs2
  • IDEA2
  • J-Package6
  • J-Tools21
  • Java2
  • JavaFx3
  • Kafka4
  • Linux2
  • Logger5
  • Maven5
  • MyBatis6
  • MyCat3
  • MySql2
  • Nginx5
  • OceanBase1
  • RabbitMq4
  • Redis6
  • SVN1
  • SpringBoot11
  • Tomcat6
  • WebService2
  • Windows2
  • kubernetes10

归档

  • 二月 20251
  • 十二月 20244
  • 八月 202416
  • 六月 20241
  • 九月 20231
  • 八月 20231
  • 七月 20232
  • 八月 20222
  • 三月 202214
  • 二月 20224
  • 十一月 20211
  • 七月 20215
  • 六月 20213
  • 五月 20213
  • 四月 20211
  • 三月 202116
  • 二月 20212
  • 一月 20211
  • 十一月 202014
  • 十月 20201
  • 九月 202014
  • 八月 20205
  • 七月 20204
  • 六月 20208
  • 五月 20208

作品

我的微信 我的文件

网站信息

本站运行时间统计: 载入中...
本站文章字数统计:96.9k
本站文章数量统计:132
© 2025 十二  |  鄂ICP备18019781号-1  |  鄂公网安备42118202000044号
驱动于 Hexo  | 主题 antiquity  |  不蒜子告之 阁下是第个访客
首页 归档 关于 文件 Github