1.前言

以下所有整合,都是基于ElasticSearch8.0进行开发,老版本会有很多不兼容的情况,请自行斟酌

2.Springboot与ElasticSearch版本对照表

参考地址:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories
image.png

3.引入

官方推荐使用RestClient,而RestClient需要我们自行配置。因此无需划分单数据源还是多数据源,初始化的引入模式就是多数据源模式了,嗨嗨嗨!!!!!

项目Github地址

https://github.com/GuardFTC/elasticsearch-test.git

引入依赖

  1. <!--ElasticSearch依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>
  6. <!--ElasticSearch客户端依赖-->
  7. <dependency>
  8. <groupId>co.elastic.clients</groupId>
  9. <artifactId>elasticsearch-java</artifactId>
  10. <version>8.0.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.fasterxml.jackson.core</groupId>
  14. <artifactId>jackson-databind</artifactId>
  15. <version>2.13.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>jakarta.json</groupId>
  19. <artifactId>jakarta.json-api</artifactId>
  20. <version>2.1.0</version>
  21. </dependency>
  22. <!--lombok依赖-->
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <optional>true</optional>
  27. </dependency>
  28. <!--Hutool依赖-->
  29. <dependency>
  30. <groupId>cn.hutool</groupId>
  31. <artifactId>hutool-all</artifactId>
  32. <version>5.8.4</version>
  33. </dependency>

编写配置文件

还有很多配置项,并没有一一写入。可以在后续结合项目,用到哪里再做补充

  1. #elasticsearch配置
  2. elasticsearch:
  3. primary:
  4. host: 120.48.107.224
  5. port: 9201
  6. username: username
  7. password: password
  8. connect-timeout: 6000
  9. socket-timeout: 6000
  10. connection-request-timeout: 2000
  11. secondary:
  12. host: 120.48.107.224
  13. port: 9202
  14. connect-timeout: 6000
  15. socket-timeout: 6000
  16. connection-request-timeout: 2000

编写配置属性类

  1. import lombok.Data;
  2. /**
  3. * @author: 冯铁城 [17615007230@163.com]
  4. * @date: 2022-08-18 11:33:32
  5. * @describe: ElasticSearch数据源配置属性
  6. */
  7. @Data
  8. public class ElasticSearchProperties {
  9. /**
  10. * 主机
  11. */
  12. private String host;
  13. /**
  14. * 端口
  15. */
  16. private Integer port;
  17. /**
  18. * 用户名
  19. */
  20. private String username;
  21. /**
  22. * 密码
  23. */
  24. private String password;
  25. /**
  26. * 链接超时时间
  27. */
  28. private Integer connectTimeout;
  29. /**
  30. * socket链接超时时间
  31. */
  32. private Integer socketTimeout;
  33. /**
  34. * 请求链接超时时间
  35. */
  36. private Integer connectionRequestTimeout;
  37. }

编写配置类

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.context.annotation.Primary;
  5. /**
  6. * @author: 冯铁城 [17615007230@163.com]
  7. * @date: 2022-08-18 16:57:15
  8. * @describe: ElasticSearch配置类
  9. */
  10. @Configuration
  11. public class ElasticSearchConfig {
  12. @Primary
  13. @Bean(name = "primaryProperties")
  14. @ConfigurationProperties(prefix = "elasticsearch.primary")
  15. public ElasticSearchProperties primaryProperties() {
  16. return new ElasticSearchProperties();
  17. }
  18. @Primary
  19. @Bean(name = "secondaryProperties")
  20. @ConfigurationProperties(prefix = "elasticsearch.secondary")
  21. public ElasticSearchProperties secondaryProperties() {
  22. return new ElasticSearchProperties();
  23. }
  24. }

编写客户端配置类

  1. import cn.hutool.core.util.StrUtil;
  2. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  3. import co.elastic.clients.json.jackson.JacksonJsonpMapper;
  4. import co.elastic.clients.transport.ElasticsearchTransport;
  5. import co.elastic.clients.transport.rest_client.RestClientTransport;
  6. import lombok.RequiredArgsConstructor;
  7. import org.apache.http.HttpHost;
  8. import org.apache.http.auth.AuthScope;
  9. import org.apache.http.auth.UsernamePasswordCredentials;
  10. import org.apache.http.client.CredentialsProvider;
  11. import org.apache.http.impl.client.BasicCredentialsProvider;
  12. import org.elasticsearch.client.RestClient;
  13. import org.elasticsearch.client.RestClientBuilder;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.context.annotation.Configuration;
  16. import org.springframework.context.annotation.Primary;
  17. /**
  18. * @author: 冯铁城 [17615007230@163.com]
  19. * @date: 2022-08-18 11:15:40
  20. * @describe: ElasticSearch客户端链接配置
  21. */
  22. @Configuration
  23. @RequiredArgsConstructor
  24. public class ElasticSearchClientConfig {
  25. private final ElasticSearchConfig elasticSearchConfig;
  26. @Primary
  27. @Bean("primaryElasticsearchClient")
  28. public ElasticsearchClient getPrimaryRestClient() {
  29. //1.获取配置
  30. ElasticSearchProperties primaryProperties = elasticSearchConfig.primaryProperties();
  31. //2.生成客户端返回
  32. return getElasticsearchClient(primaryProperties);
  33. }
  34. @Bean("secondaryElasticsearchClient")
  35. public ElasticsearchClient getSecondaryRestClient() {
  36. //1.获取配置
  37. ElasticSearchProperties secondaryProperties = elasticSearchConfig.secondaryProperties();
  38. //2.生成客户端返回
  39. return getElasticsearchClient(secondaryProperties);
  40. }
  41. /**
  42. * 创建ElasticSearch客户端
  43. *
  44. * @param properties ElasticSearch配置
  45. * @return ElasticSearch客户端
  46. */
  47. private ElasticsearchClient getElasticsearchClient(ElasticSearchProperties properties) {
  48. //1.获取主机和端口以及超时配置
  49. String host = properties.getHost();
  50. Integer port = properties.getPort();
  51. Integer connectTimeout = properties.getConnectTimeout();
  52. Integer socketTimeout = properties.getSocketTimeout();
  53. Integer connectionRequestTimeout = properties.getConnectionRequestTimeout();
  54. //2.设置客户端IP端口
  55. RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
  56. //3.设置链接相关参数
  57. RestClientBuilder.RequestConfigCallback requestConfigCallback = requestConfigBuilder -> {
  58. requestConfigBuilder.setConnectTimeout(connectTimeout);
  59. requestConfigBuilder.setSocketTimeout(socketTimeout);
  60. requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
  61. return requestConfigBuilder;
  62. };
  63. //4.设置客户端链接参数
  64. builder.setRequestConfigCallback(requestConfigCallback);
  65. //5.获取用户名密码
  66. String username = properties.getUsername();
  67. String password = properties.getPassword();
  68. //6.用户名密码判空,判定是否需要创建登录凭证
  69. if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(password)) {
  70. final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  71. //7.设置用户名密码
  72. UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
  73. credentialsProvider.setCredentials(AuthScope.ANY, credentials);
  74. //8.封装鉴权参数
  75. builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
  76. }
  77. //9.创建Rest客户端
  78. RestClient restClient = builder.build();
  79. //10.生成Elasticsearch客户端链接
  80. ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
  81. //11.生成Elasticsearch客户端并返回
  82. return new ElasticsearchClient(transport);
  83. }
  84. }

编写单元测试使用

  1. import co.elastic.clients.elasticsearch.ElasticsearchClient;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import javax.annotation.Resource;
  6. import java.io.IOException;
  7. @SpringBootTest
  8. class ElasticSearchClientConfigTest {
  9. @Resource
  10. @Qualifier("primaryElasticsearchClient")
  11. private ElasticsearchClient primaryClient;
  12. @Resource
  13. @Qualifier("secondaryElasticsearchClient")
  14. private ElasticsearchClient secondaryClient;
  15. @Test
  16. void testCreateIndex() throws IOException {
  17. primaryClient.indices().create(c -> c.index("test_java_primary"));
  18. secondaryClient.indices().create(c -> c.index("test_java_secondary"));
  19. }
  20. }

测试结果

image.png
如图,单元测试运行成功
image.png
如图,两个索引都创建成功