es 2.3.3 向es添加数据报NoNodeAvailableException[None of the configured nodes are available

一.问题描述

    在windows中搭建了一个单机版的es2.3.3 ,定时向es中添加数据,第一次添加没有问题,第二次提示NoNodeAvailableException[None of the configured nodes are available,奶奶的,经过一上午的调试排查,终于解决了。

用了一个公共的客户端连接,报错,初始化新的客户端,通过debug查看,还是同样的hashcode值,也就是说还是同一个,

原来在这里:

public class ESUtils {
    public static Client client=null;
    private static int hostPort=32016;
    private static String  hostClusterNode1="192.168.168.160";
    private static String  hostClusterNode2="192.168.168.161";
    private static String  hostClusterNode3="192.168.168.162";

    /**
     * 方法描述:    公共获取连接
     * @param:
     * @return:
     **/
    public static Client getESClientConnection(String indexName,String typeName,String clusterName,String nodes) {
        if (client == null) {
            System.err.println("-------连接es----------------------------");
            System.setProperty("es.set.netty.runtime.available.processors", "false");
            try {
                //设置集群名称
                Settings settings = Settings.builder().put("cluster.name", clusterName)
                        .put("client.transport.sniff", true)
                        .put("client.transport.ping_timeout", "600s").build();

                   String esNodes[]=nodes.split(",");
                //创建client
                String host=esNodes[0].split(":")[0];
                int port=Integer.parseInt(esNodes[0].split(":")[1]);
                client = TransportClient.builder().settings(settings).build()
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
                       // .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode1), hostPort))
                       // .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode2), hostPort))
                        //.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode3), hostPort));
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
                if (client != null) {
                    client.close();
                }
            }
        }
        return client;
    }

二.解决思路:

将初始化链接es的客户端,公共客户端,改为单次客户端,执行完一次,关闭掉,下次使用,再次初始化。

如果报异常,捕获到,重新初始化客户端。

public class ESUtils {
    //public static Client client=null;
    private static int hostPort=32016;
    private static String  hostClusterNode1="192.168.168.160";
    private static String  hostClusterNode2="192.168.168.161";
    private static String  hostClusterNode3="192.168.168.162";

    /**
     * 方法描述:    公共获取连接
     * @auther:    ypy
     * @data:      2019/3/4 17:44
     * @param:
     * @return:
     **/
    public static Client getESClientConnection(String indexName,String typeName,String clusterName,String nodes) {
        Client    singleEsClient=null;
        if (singleEsClient == null) {
            System.err.println("-------连接es----------------------------");
            System.setProperty("es.set.netty.runtime.available.processors", "false");
            try {
                //设置集群名称
                Settings settings = Settings.builder().put("cluster.name", clusterName)
                        .put("client.transport.sniff", true)
                        .put("client.transport.ping_timeout", "600s").build();

                   String esNodes[]=nodes.split(",");
                //创建client
                String host=esNodes[0].split(":")[0];
                int port=Integer.parseInt(esNodes[0].split(":")[1]);
                singleEsClient = TransportClient.builder().settings(settings).build()
                        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
                       // .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode1), hostPort))
                       // .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode2), hostPort))
                        //.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostClusterNode3), hostPort));
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.getMessage());
                if (singleEsClient != null) {
                    singleEsClient.close();
                }
            }
        }
        return singleEsClient;
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值