elasticsearch的使用(三)——SpringBoot2.x整合elasticsearch5.x

介绍

自己在整合的过程中因为版本的不一致差点嗝屁,试了好久才可以使用,本次项目里面使用的SpringBoot版本是2.0x的,因为ES的依赖是强依赖的,所以各个依赖的版本也需要一并更新。

环境配置:

系统配置

Centos7

环境配置

elasticsearch5.5,

springboot2.0

 

SpringDataElasticsearch以及SpringBoot集成

解决依赖的问题

springboot的父依赖如下


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

点击去,看到 版本为5.6.8,但是我们的搜索服务是5.5.0,所以要设置为5.5.0

5.6.8

依赖选择

es的依赖

        
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        

        
        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
        

        
            org.elasticsearch.client
            transport
            ${elasticsearch.version}
        
        

 

示例代码

pom.xml



    4.0.0

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

    com.wjx
    springboot-es-5x-example
    0.0.1-SNAPSHOT
    springboot-es-5x-example
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8

        
        5.5.0
        2.6.2
        1.2.31
        3.4
    

    
        
            spring-releases
            https://repo.spring.io/libs-release
        
    

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        

        
        
            org.elasticsearch
            elasticsearch
            ${elasticsearch.version}
        

        
            org.elasticsearch.client
            transport
            ${elasticsearch.version}
        
        

        
        
            com.alibaba
            fastjson
            ${fastjson.version}
        
        

        
            org.apache.commons
            commons-lang3
            ${commons.lang3.version}
        

        
            junit
            junit
            4.12
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


 

es配置类

package com.wjx.config;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.util.CollectionUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: dingguo
 * @Date: 2019/6/27 上午9:48
 */
@Configuration
@EnableElasticsearchRepositories("com.wjx")
public class ESConfig {
    private static final Logger logger = LoggerFactory.getLogger(ESConfig.class);
    /**
     * 端口号
     */
    @Value("${elasticsearch.port}")
    private int esPort;
    /**
     * 集群名称
     */
    @Value("${elasticsearch.clustername}")
    private String esClusterName;
    /**
     * 集群的地址
     */
    @Value("#{'${elasticsearch.hosts:localhost}'.split(',')}")
    private List hosts = new ArrayList<>();

    /**
     * 连接池
     */
    @Value("${elasticsearch.pool}")
    private String poolSize;

    /**
     * 初始化配置
     *
     * @return
     */
    private Settings settings() {
        Settings settings = Settings.builder()
                .put("cluster.name", esClusterName)
                .put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5
                .put("client.transport.sniff", false).build();
        return settings;
    }

    @Bean
    protected Client buildClient() {
        TransportClient transportClient = new PreBuiltTransportClient(settings());

        if (!CollectionUtils.isEmpty(hosts)) {
            hosts.stream().forEach(h -> {
                try {
                    transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(h), esPort));

                } catch (UnknownHostException e) {
                    logger.error("Error addTransportAddress,with host:{}.", h);
                }
            });
        }
        return transportClient;
    }
    
    @Bean
    public ElasticsearchOperations elasticsearchOperations() {
        return new ElasticsearchTemplate(buildClient());
    }

}

示例代码

springboot-elasticsearch-5x-example

 

 

 

你可能感兴趣的:(环境搭建)