• elasticsearch获取Client客户端工具类


    /*
        @author Lize.Ma
        @date 2019-11-09 14:20
        @description ES连接池
    */
    @Component
    public class ClientHelper {
        private Logger logger = Logger.getLogger(ClientHelper.class);
        @Value("${spring.data.elasticsearch.ip}")
        private String host; // 服务器地址
        @Value("${spring.data.elasticsearch.port}")
        private int port; // 端口
        @Value("${spring.data.elasticsearch.clustername}")
        private String clusterName;
    //    @Value("${spring.data.elasticsearch.username}")
    //    private String username;
    //    @Value("${spring.data.elasticsearch.password}")
    //    private String password;
        private String keypath;
    
        private Settings setting;
    
        private ConcurrentHashMap<String, Client> clientMap = new ConcurrentHashMap<String, Client>();
    
        // hostname port
        private Map<String, Integer> ips = new HashMap<String, Integer>();
    
        //单例模式 获取实例对象
        public static final ClientHelper getInstance() {
            return ClientHolder.INSTANCE;
        }
    
        private static class ClientHolder {
            private static final ClientHelper INSTANCE = new ClientHelper();
        }
    
        /**
         * 初始化默认的client
         */
        public void init() {
            logger.info("host:" + host);
            logger.info("port:" + port);
            logger.info("clusterName:" + clusterName);
            //集群处理
            if (host.contains(",")) {
                String[] split = host.split(",");
                for (String ip : split) {
                    ips.put(ip, port);
                }
            } else {
                ips.put(host, port);
            }
    //读取检验文件
    //        keypath=ResourceFileUtil.getFilePath("elastic-certificates.p12");
    //        if (!CommonUtil.isNotNullEmpty(keypath)){
    //            logger.error("not find elastic-certificates.p12 path");
    //            return;
    //        }
    
            setting = Settings.builder()
                    .put("cluster.name", clusterName)
    //                .put("xpack.security.user", username + ":" + password)
    //                .put("xpack.security.transport.ssl.enabled", true)
    //                .put("xpack.security.transport.ssl.verification_mode", "certificate")
    //                .put("xpack.security.transport.ssl.keystore.path", keypath)
    //                .put("xpack.security.transport.ssl.truststore.path", keypath)
                    .put("client.transport.sniff", true)
                    .put("thread_pool.search.size", 10).build();
            addClient(setting, ips);
        }
    
    
        public Client getClient() {
            if (!CommonUtil.isNotMapNull(clientMap) || !CommonUtil.isNotMapNull(ips)) {
                init();
            }
            return getClient(clusterName);
        }
    
        public Client getClient(String clusterName) {
            if (!CommonUtil.isNotMapNull(clientMap) || !CommonUtil.isNotMapNull(ips)) {
                init();
            }
            return clientMap.get(clusterName);
        }
    
        /**
         *  添加到map
         * @param setting
         * @param ips
         */
        public void addClient(Settings setting, Map<String, Integer> ips) {
            TransportClient client = new PreBuiltTransportClient(setting);
    
            for (String ip : ips.keySet()) {
                try {
                    client = client.addTransportAddresses(new TransportAddress(InetAddress.getByName(ip), Integer.valueOf(ips.get(ip).toString())));
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
            if (client != null) {
                clientMap.put(setting.get("cluster.name"), client);
            } else {
                logger.error("TransportClient is null ,can not build cennection for es");
            }
    
        }
    
        /**
         * 获得所有的地址端口
         * @return
         */
        public List<TransportAddress> getAllAddress(Map<String, Integer> ips) {
            List<TransportAddress> addressList = new ArrayList<TransportAddress>();
            for (String ip : ips.keySet()) {
                try {
                    addressList.add(new TransportAddress(InetAddress.getByName(ip), Integer.valueOf(ips.get(ip).toString())));
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
            return addressList;
        }
    
    }

    感悟:es主要的耗时地方是:连接es,获取client上

    连接池,就是第一次连接的时候,把获取的连接对象存到map里了,你以后再用直接从map里拿,相当于从内存拿,所以非常快

    <!--es 依赖包 -->
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>6.7.2</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>transport</artifactId>
                <version>6.7.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.lucene</groupId>
                <artifactId>lucene-core</artifactId>
                <version>7.7.0</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>x-pack-transport</artifactId>
                <version>5.6.0</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.7.2</version>
            </dependency>
            <!--日志包-->
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.8.2</version>
            </dependency>
            <!-- fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.58</version>
            </dependency>
  • 相关阅读:
    Rx 键值观察KVO的使用
    js总结(6.1)获取DOM的各个属性 补充部分
    js总结(8)轮播小总结
    js总结 (7)事件
    js总结 (书本)《JavaScript DOM编程艺术 第2版》笔记
    js总结 (6) DOM操作 节点元素 遍历树
    css 补充之 checked应用 制作选中效果,以及z-index和position-fixed transfrom 之间层级关系
    js总结 (4)JavaScript高级程序设计
    js总结 (5)原型 原型链,,,继承的问题
    Css权威指南总结
  • 原文地址:https://www.cnblogs.com/mlzdev/p/11826214.html
Copyright © 2020-2023  润新知