美文网首页Elaticsearch进阶
ElasticSearch6.X集成SpringBoot2.0

ElasticSearch6.X集成SpringBoot2.0

作者: 一个菜鸟JAVA | 来源:发表于2018-07-24 16:52 被阅读28次
版本说明

目前,SpringBoot提供的ES集成版本暂时不支持6.X的版本.如果是6.X以下的是可以支持的.
SpringBoot与ES对应版本说明

集成

maven依赖

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>6.2.3</version>
        </dependency>

application.properties配置.多个使用,分隔

spring.data.elasticsearch.cluster-nodes=http://localhost:9200

客户端配置类

package com.zc.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URL;

/**
 * Created by  on 2018/7/24.
 */
@Configuration
public class ElasticsearchConfig implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchConfig.class);
    private RestHighLevelClient restHighLevelClient;

    @Override
    public void destroy() throws Exception {
        try {
            if (restHighLevelClient!=null){
                restHighLevelClient.close();
            }
        }catch (IOException e){
            LOG.error("Error closing ElasticSearch client: ", e);
        }finally {

        }
    }

    @Override
    public RestHighLevelClient getObject() throws Exception {
        return restHighLevelClient;
    }

    @Override
    public Class<?> getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] urlList = clusterNodes.split(",");
        HttpHost[] nodes = new HttpHost[urlList.length];
        for (int i = 0; i < urlList.length; i++) {
            URL url = new URL(urlList[i]);
            HttpHost node = new HttpHost(url.getHost(),url.getPort(),url.getProtocol());
            nodes[i] = node;
        }
        RestClientBuilder builder = RestClient.builder(nodes);
        restHighLevelClient = new RestHighLevelClient(builder);
    }
}

我这里是按照我自己的逻辑写的,当然具体怎么做,可以根据自己情况调整,主要就是注入client客户端

测试
package com.zc.config;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zengchao on 2018/7/24.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RestClient {
    @Autowired
    private RestHighLevelClient client;

    @Test
    public void test() throws IOException {
        IndexRequest request = new IndexRequest("buydeem","order","1");
        Map<String,Object> order =new HashMap<>();
        order.put("orderNo","201807240001");
        order.put("created",new Date());
        order.put("amount", BigDecimal.TEN);
        request.source(order);
        IndexResponse response = client.index(request);
        System.out.println(response.toString());
    }
}

结果:

IndexResponse[index=buydeem,type=order,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}]

kibana查询结果

相关文章

网友评论

本文标题:ElasticSearch6.X集成SpringBoot2.0

本文链接:https://www.haomeiwen.com/subject/gukymftx.html