2

SpringBoot3.1.1 整合 Elasticsearch8.7.1

 9 months ago
source link: https://maxqiu.com/article/detail/156
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SpringBoot3.1.1 整合 Elasticsearch8.7.1

2023/07/18  Java  Elasticsearch  SpringBoot

示例代码:


官方教程:Spring Data Elasticsearch - Reference Documentation

PS:本文只是一篇极其简单的连接配置整合教程,不涉及搜索示例,建议认真阅读官方文档

版本对应关系

Spring Boot Spring Data Elasticsearch Elasticsearch
2.4.x 4.1.x 7.9.3
2.5.x 4.2.x 7.12.1
2.6.x 4.3.x 7.15.2
2.7.x 4.4.x 7.17.10
3.0.x 5.0.x 8.5.3
3.1.x 5.1.x 8.7.1

本文以Spring Boot 3.1.x为例

pom.xml

  1. <!--核心依赖-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>
  6. <!-- 自定义配置文件 -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-configuration-processor</artifactId>
  10. <optional>true</optional>
  11. </dependency>

连接信息配置

由于 ES 8.x 开始,服务安装后默认使用 HTTPS 协议访问,所以要自定义配置文件配置CA指纹信息

获取 CA 指纹

Elasticearch 安装完成后,在安装目录下的 config/certs 目录下,有个 http_ca.crt 根证书文件,使用以下命令可以获取指纹信息:

  1. $ openssl x509 -in http_ca.crt -sha256 -fingerprint | grep SHA256 | sed 's/://g'
  2. SHA256 Fingerprint=8ADC8194ADFBC5AA5006B787988F207857CB7B935EE9B5C707C6BAB84759A99C
  • Linux 系统自带 openssl 指令,可以直接执行
  • Windows 系统需要借助 git 安装完成后附带的 Git Bash 工具执行以上指令
  • macOS 系统需要把 SHA256 换成小写 sha256
  • Docker 环境先进入容器再执行,证书文件在 /usr/share/elasticsearch/config/certs/ 目录下

Properties

自定一个配置文件,方便在 yml 配置客户端连接信息

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.stereotype.Component;
  3. import lombok.Getter;
  4. import lombok.NoArgsConstructor;
  5. import lombok.Setter;
  6. import lombok.ToString;
  7. /**
  8. * 客户端连接信息配置
  9. */
  10. @Component
  11. @ConfigurationProperties(prefix = "elasticsearch")
  12. @Getter
  13. @Setter
  14. @NoArgsConstructor
  15. @ToString
  16. public class ElasticSearchProperties {
  17. /**
  18. * 是否启用
  19. */
  20. private String[] hostAndPorts;
  21. /**
  22. * 用户名
  23. */
  24. private String username;
  25. /**
  26. * 密码
  27. */
  28. private String password;
  29. /**
  30. * CA证书指纹
  31. */
  32. private String caFingerprint;
  33. }

application.yml

将连接信息放在 yml 文件中,方便根据运行环境切换

  1. elasticsearch:
  2. hostAndPorts: # elasticsearch 连接地址,可填写多个
  3. - 127.0.0.1:9200
  4. username: elastic
  5. password: t=Nn*1N*GlNqdKS4Z1dm
  6. ca-fingerprint: 8ADC8194ADFBC5AA5006B787988F207857CB7B935EE9B5C707C6BAB84759A99C

Configuration

自定义一个 Bean 对象,配置连接。更多连接信息可以跟进 org.springframework.data.elasticsearch.client.ClientConfigurationBuilder 中可配置的属性进行扩展

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.data.elasticsearch.client.ClientConfiguration;
  3. import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
  4. import com.maxqiu.demo.properties.ElasticSearchProperties;
  5. import jakarta.annotation.Resource;
  6. /**
  7. * Elasticsearch客户端
  8. */
  9. @Configuration
  10. public class ElasticsearchConfig extends ElasticsearchConfiguration {
  11. @Resource
  12. private ElasticSearchProperties properties;
  13. @Override
  14. public ClientConfiguration clientConfiguration() {
  15. // 使用构建器来提供集群地址
  16. return ClientConfiguration.builder()
  17. // 设置连接地址
  18. .connectedTo(properties.getHostAndPorts())
  19. // 启用ssl并配置CA指纹
  20. .usingSsl(properties.getCaFingerprint())
  21. // 设置用户名密码
  22. .withBasicAuth(properties.getUsername(), properties.getPassword())
  23. // 创建连接信息
  24. .build();
  25. }
  26. }

用的不多,不是很熟悉,不写了。。。。。。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK