springboot2.0集成elasticsearch6.3.2

1.maven依赖

    org.springframework.boot
    spring-boot-starter-parent
    2.0.0.RELEASE
dependency>
    org.springframework.boot
    spring-boot-starter-web

    org.elasticsearch
    elasticsearch
    6.3.2


    org.elasticsearch.client
    transport
    6.3.2



    org.elasticsearch.plugin
    transport-netty4-client
    6.3.2
2.配置文件
elasticsearch.cluster.name=elasticsearch
elasticsearch.ip=127.0.0.1
elasticsearch.port=9300
elasticsearch.pool=1
3.配置类
@Bean
public TransportClient init() {

    TransportClient transportClient = null;
    try {
        // 配置信息
        Settings esSetting = Settings.builder()
                .put("cluster.name", clusterName)
                //增加嗅探机制,找到ES集群
                .put("client.transport.sniff", true)
                //增加线程池个数为1
                .put("thread_pool.search.size", Integer.parseInt(poolSize))
                .build();

        transportClient = new PreBuiltTransportClient(esSetting);
        TransportAddress inetSocketTransportAddress = new TransportAddress(InetAddress.getByName(hostName),
                Integer.valueOf(port));
        transportClient.addTransportAddresses(inetSocketTransportAddress);
    } catch (UnknownHostException e) {
        e.printStackTrace();
        logger.info("初始化bean失败");
    }


    return transportClient;
}

集成完毕,使用时注入下TransportClient这个就可以用了

你可能感兴趣的:(springboot)