栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Elasticsearch开发

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Elasticsearch开发

注意事项:

       在使用SpringBoot+Elasticsearch开发时,首要要安装Elasticsarch服务器。如果没有安装的可用查阅一下我前面发布的一篇"Elasticsearch7.3 的安装"的文章

1、首先使用IDEA创建一个maven项目。首先选择创建一个空的maven工程

然后点击下一步,添写maven工程的名称,maven工程的存放地址以及版本号

点击finish完成创建maven工程,随后可用把maven源缓存自己的,打开IDEA编辑器file>>settings>>build,execution,deployment>>build tools>>maven

 2、在pom.xml中引入spring-boot、spring-boot-web和elasticsearc的依赖



    4.0.0

    cn.mshangqs
    springboot-es
    1.0

    
        8
        8
    

    
        spring-boot-starter-parent
        org.springframework.boot
        2.5.10
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.3.0
            
                
                    org.elasticsearch
                    elasticsearch
                
            
        
        
            org.elasticsearch
            elasticsearch
            7.3.0
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            junit
            junit
            test
        
    

3、在application.properties文件中添加elasticsearch的配置

server.port=8088

# 配置elasticsearch服务器地址 127.0.0.1:9200,127.0.0.1:9300(多个可利用逗号进行分割)
chengbkj.elasticsearch.hosts = 127.0.0.1:9200

4、利用SpringBoot的自动管理进行连接

@Configuration
public class ElasticsearchConfig {

    @Value("${chengbkj.elasticsearch.hosts}")
    public String hostList;

    @Bean
    public RestHighLevelClient restHighLevelClient(){

        String[] hostArr = hostList.split(",");
        //1、创建一个httpHosts数组
        HttpHost[] httpHosts = new HttpHost[hostArr.length];

        for (int i = 0; i < hostArr.length; i++) {
            String item = hostArr[i];
            httpHosts[i] = new HttpHost(item.split(":")[0],Integer.parseInt(item.split(":")[1]),"http");
        }

        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }


}

4、创建一个测试文件,并进行测试是否可用进行连接

package cn.mshangqs;


import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@SpringBootTest(classes = EsApplication.class)
@RunWith(SpringRunner.class)
public class ElApplicationTest{

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Test
    public void testClient() throws IOException {
        //1 构建请求
        GetRequest getRequest = new GetRequest("book", "1");
        //2 执行
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        //3 获取请求结果
        System.out.println(getResponse.getId());
        System.out.println(getResponse.getVersion());
        System.out.println(getResponse.getSourceAsString());
    }

}

查看控制台打印的结果:

 对比elasticsearch服务器中的数据是否一致

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921244.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号