SpringBoot3整合Elasticsearch8,面试建议

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注大数据)
img

正文

账号密码连接ES
  1. 设置:ES Elasticsearch.ymlxpack.security.enabled属性使用默认值+ xpack.security.http.ssl.enabled设置为false

注意:若xpack.security.enabled属性为false,则xpack.security.http.ssl.enabled属性不生效,即相当于设置为false;所有以xpack开头的属性都不会生效

ES Elasticsearch.ymlxpack.security.http.ssl.enabled
● 默认true:必须使用https://localhost:9200/访问ES服务+启动Kibana服务会成功+需要使用账号连接+必须使用HTTPS连接
● 若为false:必须使用http://localhost:9200/访问ES服务+启动Kibana服务会失败+需要使用账号连接,但必须使用HTTP连接

  1. 配置:dev目录application-dal中添加下列配置
# elasticsearch配置
elasticsearch:
  userName:  #自己的账号名
  password:  #自己的密码

  1. 添加:ElasticSearchTest类中添加下列代码---索引名必须小写+不能有空格
  2. 运行:设置跳过测试--->手动运行/不跳过--->直接install,但不运行测试
@Resource(name = "accountConnectionESClient")
ElasticsearchClient accountConnectionESClient;


@Test
public void accountConnectionTest() throws IOException {

if (open.equals("true")) {
    //创建索引
    CreateIndexResponse response = accountConnectionESClient.indices().create(c -> c.index("account\_connection\_index"));
    log.info(response.toString());
}
else{
    log.info("es is closed");
}
}

证书账号连接ES
  1. 设置:ES Elasticsearch.ymlxpack.security.enabledxpack.security.http.ssl.enabled配置项使用默认值

设置为true后,ES就走https,若schemehttp,则报Unrecognized SSL message错误

  1. 配置:将dev目录application-dalelasticsearch.scheme配置项改成https
  2. 证书添加:终端输入keytool -importcert -alias es_https_ca -keystore "D:\computelTool\Java\JDK\JDK21\lib\security\cacerts" -file "D:\computelTool\database\elasticsearch8111\config\certs\http_ca.crt"

keytool -delete -alias es_https_ca -keystore "D:\computelTool\Java\JDK\JDK21\lib\security\cacerts" ---与上面的命令相反

  1. 拷贝:将ESconfig目录下certs目录下的http_ca.crt文件拷贝到web模块resource目录
  2. 添加:ElasticSearchConfig类添加下列方法
/\*\*
\* @function: 创建用于安全连接(证书 + 账号)ES服务器的客户端
\* 如果@Bean没有指定bean的名称,那么这个bean的名称就是方法名
\*/
@Bean(name = "accountAndCertificateConnectionESClient")
public ElasticsearchClient accountAndCertificateConnectionESClient() {


    RestClientBuilder builder = creatBaseConfBuilder( (scheme == "https")?"https":"https");

    // 1.账号密码的配置
    //CredentialsProvider: 用于提供 HTTP 身份验证凭据的接口; 允许你配置用户名和密码,以便在与服务器建立连接时进行身份验证
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));

    // 2.设置自签证书,并且还包含了账号密码
    builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder
                                        .setSSLContext(buildSSLContext())
                                        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                        .setDefaultCredentialsProvider(credentialsProvider));

    RestClientTransport transport = new RestClientTransport(builder.build(), new JacksonJsonpMapper());

    //And create the API client
    ElasticsearchClient esClient = new ElasticsearchClient(transport);

    return esClient;
}

private static SSLContext buildSSLContext() {

    // 读取http\_ca.crt证书
    ClassPathResource resource = new ClassPathResource("http\_ca.crt");
    SSLContext sslContext = null;
    try {
        // 证书工厂
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        Certificate trustedCa;
        try (InputStream is = resource.getInputStream()) {
            trustedCa = factory.generateCertificate(is);
        }
        // 密钥库
        KeyStore trustStore = KeyStore.getInstance("pkcs12");
        trustStore.load(null, "liuxiansheng".toCharArray());
        trustStore.setCertificateEntry("ca", trustedCa);
        SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                 .loadTrustMaterial(trustStore, null);
        sslContext = sslContextBuilder.build();
    } catch (CertificateException | IOException | KeyStoreException | NoSuchAlgorithmException |
                 KeyManagementException e) {
        log.error("ES连接认证失败", e);
    }
    return sslContext;
}


  1. 测试:ElasticSearchTest类添加
@Resource(name = "accountAndCertificateConnectionESClient")
ElasticsearchClient accountAndCertificateConnectionESClient;

@Test
public void  accountAndCertificateConnectionTest() throws IOException {

if (open.equals("true")) {
    //创建索引
    CreateIndexResponse response =  accountAndCertificateConnectionESClient.indices().create(c -> c.index("account\_and\_certificate\_connection\_index"));
    log.info(response.toString());
    System.out.println(response.toString());
}
else{
    log.info("es is closed");
}
}

Spring Data ES

公共配置
  1. 依赖:web模块引入该依赖---但其版本必须与你下载的ES的版本一致

版本:点击https://spring.io/projects/spring-data-elasticsearch#learn,点击GA版本的Reference Doc,点击version查看Spring Data ESES版本的支持关系

参考:https://www.yuque.com/itwanger/vn4p17/wslq2t/https://blog.csdn.net/qq_40885085/article/details/105023026

<!-- 若存在Spring Data ES的某个版本支持你下的ES版本,则使用 -->
<!-- Spring官方在ES官方提供的JAVA环境使用的依赖的基础上做了封装 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<!-- ESTestEntity用到 -->
<dependency>
	<groupId>jakarta.servlet</groupId>
	<artifactId>jakarta.servlet-api</artifactId>
	<version>6.0.0</version>
	<scope>provided</scope>
</dependency>

<!-- ESTestEntity用到 -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <scope>provided</scope>
  <!--provided:指定该依赖项在编译时是必需的,才会生效, 但在运行时不需要,也不会生效-->
  <!--这样 Lombok 会在编译期静悄悄地将带 Lombok 注解的源码文件正确编译为完整的 class 文件 -->
</dependency>

  1. 新建:web模块TestEntity目录新建ESTestEntity
@Data
@EqualsAndHashCode(callSuper = false)
@Document(indexName = "test")
public class ESTestEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Field(type = FieldType.Text, analyzer = "ik\_max\_word")
    private String content;

    private String title;

    private String excerpt;
}

  1. 新建:web模块TestRepository包下新建ESTestRepository 接口
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ESTestRepository extends ElasticsearchRepository<ESTestEntity, Long> {
}

直接连接ES
  1. 配置:ES Elasticsearch.ymlxpack.security.enabled属性设置为false

xpack.security.enabled
● 默认true:必须使用账号连接ES
● 若为false:必须使用http://localhost:9200/访问ES服务+启动Kibana服务会失败+不需要使用账号连接,但必须使用HTTP连接

  1. 配置:web模块dev目录application-dal添加
spring:
  elasticsearch:
    uris:
      - http://127.0.0.1:9200

  1. 新建:web模块test目录新建ElasticsearchTemplateTest
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;

@SpringBootTest
public class ElasticsearchTemplateTest {

    @Autowired
    ESTestRepository esTestRepository;

    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;

    @Test
    void save() {
        ESTestEntity esTestEntity = new ESTestEntity();
        esTestEntity.setId(1L);
        esTestEntity.setContent("不安全连接");
        esTestEntity.setTitle("world");
        esTestEntity.setExcerpt("test");
        System.out.println(elasticsearchTemplate.save(esTestEntity));
    }

    @Test
    void insert() {
        ESTestEntity esTestEntity = new ESTestEntity();
        esTestEntity.setId(2L);
        esTestEntity.setContent("不安全连接");
        esTestEntity.setTitle("world");
        esTestEntity.setExcerpt("test");
        System.out.println(esTestRepository.save(esTestEntity));
    }
}

  1. 访问:点击http://localhost:9200/test/_search ---查询索引库test
    请添加图片描述
HTTP连接ES
  1. 配置:ES Elasticsearch.ymlxpack.security.enabled属性使用默认值+ xpack.security.http.ssl.enabled设置为false

ES Elasticsearch.ymlxpack.security.http.ssl.enabled
● 默认true:必须使用https://localhost:9200/访问ES服务+启动Kibana服务会成功+需要使用账号连接+必须使用HTTPS连接
● 若为false:必须使用http://localhost:9200/访问ES服务+启动Kibana服务会失败+需要使用账号连接,但必须使用HTTP连接

  1. 配置:web模块dev目录application-dal添加
spring:
  elasticsearch:
    uris:
      - http://127.0.0.1:9200
    username:  # 账号用户名
    password:  #账号密码

  1. 修改:web模块test目录下ElasticsearchTemplateTest修改成这样,其他参见直接连接
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;

@SpringBootTest
public class ElasticsearchTemplateTest {

    @Autowired
    ESTestRepository esTestRepository;

    @Autowired
    ElasticsearchTemplate elasticsearchTemplate;

    @Test
    void save() {
        ESTestEntity esTestEntity = new ESTestEntity();
        esTestEntity.setId(1L);
        esTestEntity.setContent("不安全连接");
        esTestEntity.setTitle("world");
        esTestEntity.setExcerpt("test");
        System.out.println(elasticsearchTemplate.save(esTestEntity));
    }

    @Test
    void insert() {
        ESTestEntity esTestEntity = new ESTestEntity();
        esTestEntity.setId(2L);
        esTestEntity.setContent("不安全连接");
        esTestEntity.setTitle("world");
        esTestEntity.setExcerpt("test");
        System.out.println(esTestRepository.save(esTestEntity));
    }
}

  1. 访问:点击http://localhost:9200/test/_search ---查询索引库test
HTTPS连接ES
  1. 配置:ES Elasticsearch.ymlxpack.security.enabledxpack.security.http.ssl.enabled属性使用默认值
  2. 配置:web模块dev目录application-dal添加
spring:
  elasticsearch:
    uris:
      - https://127.0.0.1:9200
    username:  # 账号用户名
    password:  #账号密码

  1. 修改:web模块test目录下ElasticsearchTemplateTest修改成这样,其他参见直接连接
import cn.bytewisehub.pai.web.TestEntity.ESTestEntity;
import cn.bytewisehub.pai.web.TestRepository.ESTestRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;

@SpringBootTest


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
![img](https://img-blog.csdnimg.cn/img_convert/54ef62d709a62b36afdbb3f3f6c27562.png)

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

t org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;

@SpringBootTest


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注大数据)**
[外链图片转存中...(img-2YLpOzbt-1713154785841)]

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值