ElastaticSearch ---es客户端 TransportClient

TransportClient

TransportClient客户端,官方在es 7.0版本中将弃用TransportClient客户端,且在8.0版本中完全移除它.
es 7.0及以上的版本,请使用 RestHighLevelClient。
如果项目中使用的es版本不高,可以使用 TransportClient。

依赖

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
            <version>5.5.1</version>
        </dependency>

初始化TransportClient 客户端


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 java.net.InetAddress;
import java.net.UnknownHostException;


public class EsTransportClient {

    /**
     * 初始化TransportClient 客户端
     *
     * @param clusterName es集群名称
     * @param ip es的ip地址
     * @param port es的端口
     * @return
     */
    public static TransportClient getClient(String clusterName, String ip, int port) {
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();

        TransportClient transportClient = null;
        try {
            transportClient = new PreBuiltTransportClient(settings);
            transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
            return transportClient;
        } catch (UnknownHostException e) {
            log.error("UnknownHostException exception.");
        }
        return transportClient;
    }


    /**
     * 多节点的es集群
     * @param nodes 格式为:ip:端口,多个节点用逗号隔开  ,比如 10.123.666.1:9300,10.123.666.2:9300
     * @param clusterName 集群名称
     * @return
     */
    public static TransportClient getClientByNodes(String nodes, String clusterName) {
        String[] hosts = nodes.split(",");
        Settings settings = Settings.builder().put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .put("client.transport.ping_timeout", "10s")
                .build();
        TransportClient esClient = null;

        try {
            esClient = new PreBuiltTransportClient(settings);
            for (String host : hosts) {
                String[] hostAndPort = host.split(":");
                esClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndPort[0]),
                            Integer.parseInt(hostAndPort[1])));
            }
        } catch (NumberFormatException | UnknownHostException e) {
            log.error("getClientByNodes exception.", e);
        }
        return esClient;
    }

}

posted on   乐之者v  阅读(484)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示