一.问题描述
在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;
}