• elasticsearch单例模式连接 java


    import java.net.InetAddress;

    import org.elasticsearch.client.transport.TransportClient;
    import org.elasticsearch.common.settings.Settings;
    import org.elasticsearch.common.transport.InetSocketTransportAddress;
    import org.elasticsearch.transport.client.PreBuiltTransportClient;

    /**
     * @author zxy
     *es单例连接集群
     */
    public class ESConnect {
        
        public static TransportClient transportClient=null; //句柄
        public static String esClusterName;     //集群名
        public static String esServerIps;       //集群服务ip集合
        public static Integer esServerPort;     //Es集群端口
        
        @SuppressWarnings({ "resource", "unchecked" })
        public static TransportClient getTransPortClient(String esClusterNam,String esServerIp,Integer esServerPor){
            //连接ES transportClient获得句柄
                esClusterName = esClusterNam; // 集群名
                esServerIps = esServerIp; // 集群服务ip
                esServerPort = esServerPor; // Es集群端口
                try {
                    if (transportClient == null) {
                        if (esServerIps == null || "".equals(esServerIps.trim()))
                            return null;
                        Settings esSetting = Settings.builder().put("client.transport.ignore_cluster_name", true)
                                .put("cluster.name", esClusterName) // 集群名
                                .put("client.transport.sniff", true) // 自动嗅探,把机器添加到列表
                                .put("client.transport.ping_timeout", "10s")
                                .build();
                        transportClient = new PreBuiltTransportClient(esSetting).addTransportAddress(
                                new InetSocketTransportAddress(InetAddress.getByName(esServerIps), esServerPort));

                        System.out.println("connectok");
                        return transportClient;
                    } else{
                        System.out.println("hell11");
                        return transportClient;
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (transportClient != null)
                        transportClient.close();
                    System.out.println("hellk");
                    return null;
                }
            }

            public static void clientClose() {
                /* 关闭连接 */
                if (null != transportClient) {
                    try {
                        transportClient.close();
                    } catch (Exception e) {
                    }
                }
            }

    }

  • 相关阅读:
    Python实用笔记 (24)面向对象高级编程——使用@property
    Python实用笔记 (23)面向对象高级编程——使用__slots__
    Python实用笔记 (22)面向对象编程——实例属性和类属性
    Python实用笔记 (21)面向对象编程——获取对象信息
    Python实用笔记 (20)面向对象编程——继承和多态
    Python实用笔记 (19)面向对象编程——访问限制
    Python实用笔记 (18)面向对象编程——类和实例
    Python实用笔记 (17)模块
    Python实用笔记 (16)函数式编程——偏函数
    Python实用笔记 (15)函数式编程——装饰器
  • 原文地址:https://www.cnblogs.com/zengxiaoyi36/p/7679316.html
Copyright © 2020-2023  润新知