java连接elasticsearch多台服务创建方法

动态创建连接,数组里可以存放elasticsearch服务的ip地址

Client client = null;
        try {
            String[] urllist = URL.split(",");
            List<InetSocketTransportAddress> list = new ArrayList<InetSocketTransportAddress>();
            for(String str : urllist){
                logger.info(str);
                InetSocketTransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName(str),9300);
                list.add(transportAddress);
            }
            logger.info("创建Elasticsearch Client 开始");
            client = TransportClient.builder().build()
                    .addTransportAddresses(list.toArray(new InetSocketTransportAddress[list.size()]));
            logger.info("创建Elasticsearch Client 结束");
        } catch (Exception e) {
            logger.error("创建Elasticsearch Client 异常"+URL);
        }

 

posted @ 2018-01-07 15:23  Nice&&Day  阅读(641)  评论(0)    收藏  举报