SpringBoot整合Elasticsearch7实战(一)

前言

本文主要介绍java客户端SpringBoot整合Elasticsearch,参照Elasticsearch的官方文档,将一些基本的常见的API封装,供自己后面项目中es能够开箱即用。

java客户端采用的RestHighLevelClient来实现es基础查询;

官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

中文文档:https://www.kaifaxueyuan.com/server/elasticsearch7/elasticsearch-index-api.html

 

正文

maven依赖:


        org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASE
    

    
        
        7.4.2
        2.12.1
    


    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
        

        
            org.elasticsearch.client
            elasticsearch-rest-client
            ${elasticsearch.version}
        

        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            ${elasticsearch.version}
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.apache.logging.log4j
            log4j-api
            ${log4j2.version}
        
        
            org.apache.logging.log4j
            log4j-core
            ${log4j2.version}
        
        
            org.projectlombok
            lombok
            1.16.18
            true
        
        
            org.apache.commons
            commons-lang3
            3.6
        
        
            com.alibaba
            fastjson
            1.2.47
        

    

配置类

此处可以用properties配置文件管理,我为了方便,直接在配置类写死了

package com.ruofei.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: srf
 * @Date: 2020/12/9 20:27
 * @description:
 */
@Configuration
public class ESConfig implements DisposableBean {

    private RestHighLevelClient restHighLevelClient;

    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestClientBuilder restClientBuilder = RestClient.builder(
                new HttpHost("127.0.0.1", 9200, "http"));

        //配置超时时间(非必须)
        restClientBuilder.setRequestConfigCallback(requestConfigBuilder->{
            //连接超时时间
            requestConfigBuilder.setConnectTimeout(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS);
            //socket超时时间
            requestConfigBuilder.setSocketTimeout(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS);
            //请求超时时间
            requestConfigBuilder.setConnectionRequestTimeout(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS);
            return requestConfigBuilder;
        });

        //异步 httpclient 连接参数配置
        restClientBuilder.setHttpClientConfigCallback(httpClientBuilder->{
            //最大连接数
            httpClientBuilder.setMaxConnTotal(RestClientBuilder.DEFAULT_MAX_CONN_TOTAL);
            //单主机并发最大数
            httpClientBuilder.setMaxConnPerRoute(RestClientBuilder.DEFAULT_MAX_CONN_PER_ROUTE);
            return httpClientBuilder;
        });

        // 鉴权设置,如果需要账号密码用下面逻辑
/*        if (StringUtils.isNotBlank(elasticsearchProperties.getUsername()) && StringUtils
                .isNotBlank(elasticsearchProperties.getPassword()))
        {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider
                    .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticsearchProperties
                            .getUsername(), elasticsearchProperties.getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }*/

        restHighLevelClient = new RestHighLevelClient(restClientBuilder);

        return restHighLevelClient;
    }

    @Override
    public void destroy() throws Exception {
        restHighLevelClient.close();
    }
}

API相关操作简单封装

主要对索引,文档,查询相关的api进行了封装。具体实现就一一写了,代码会上传github:https://github.com/RuofeiSun/springboot-es.git

SpringBoot整合Elasticsearch7实战(一)_第1张图片

 

这次主要是对常用的基本api封装了下,下一步打算做一个实际应用的demo。包括数据同步,查询等

你可能感兴趣的:(springboot,elasticsearch,java后端,java,elasticsearch)