频道栏目
首页 > 资讯 > 云计算 > 正文

ElasticSearch集群连接方法Java

18-07-11        来源:[db:作者]  
收藏   我要投稿
package com.ctpsp.custominfo.api.configuration;


import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.UnknownHostException;

@Configuration
public class ElasticSearchConfiguration {
    @Value("${elastic.esServerIps}")
    private String esServerIps;
    @Value("${elastic.port}")
    private int port;

    private static TransportClient  transPort = null;

    @Bean
    public TransportClient client() throws UnknownHostException {

        try {

            Settings settings = Settings.builder()
                    .put("cluster.name", "leon")
                    .build();

            transPort = new PreBuiltTransportClient(settings);

            String esIps[] = esServerIps.split(",");
            for (String esIp : esIps) {//添加集群IP列表
                TransportAddress transportAddress =  new InetSocketTransportAddress(
                        InetAddresses.forString(esIp),
                        9300);
                transPort.addTransportAddresses(transportAddress);
            }

            return transPort;
        }catch (Exception e){
            e.printStackTrace();
            if (transPort != null){
                transPort.close();
            }
        }
        return null;
    }

}

properties

elastic.esServerIps=192.168.1.21,192.168.1.21
elastic.port= 9300
相关TAG标签
上一篇:线程简介
下一篇:给php添加扩展的方法示例讲解
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站