一、报错信息
DEBUG org.apache.zookeeper.Client***xnSocketNIO - Ignoring exception during shutdown input
java.***.SocketException: Socket is not connected
at sun.nio.ch.***.translateToSocketException(***.java:123)
at sun.nio.ch.***.translateException(***.java:157)
at sun.nio.ch.***.translateException(***.java:163)
at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401)
at org.apache.zookeeper.Client***xnSocketNIO.cleanup(Client***xnSocketNIO.java:200)
二、问题描述
在使用java连接zookeeper客户端时,发现连接是成功的,但是操作数据时报错
连接zookeeper客户端代码
public class Test {
//此处是机器ip:端口号。多个ip用,隔开
private static String connectString = "机器ip:2181";
int sessionTimeout = 5000;
@org.junit.Test
public void test() throws IOException {
ZooKeeper zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 监听事件
* @param event
*/
@Override
public void process(WatchedEvent event) {
System.out.println("监听事件"+event);
}
});
System.out.println(zooKeeper);
}
}
打印信息——连接成功
State:CONNECTING sessionid:0x0 local:null remoteserver:null lastZxid:0 xid:1 sent:0 recv:0 queuedpkts:0 pendingresp:0 queuedevents:0
操作zookeeper代码
public class Test1 {
String connectString = "192.168.75.130:2181,192.168.75.131:2181,192.168.75.132:2181";
int sessionTimeout = 5000;
ZooKeeper zooKeeper = null;
@Before
public void test() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 监听事件
* @param event
*/
@Override
public void process(WatchedEvent event) {
System.out.println("监听事件" + event);
}
});
// System.out.println(zooKeeper);
}
/**
* 创建节点
*/
@Test
public void createNode() throws Exception {
String path = zooKeeper.create("/apptest" // 节点路径
, "HelloZookeeper".getBytes() // 节点的数据
, ZooDefs.Ids.OPEN_ACL_UNSAFE // 权限
, CreateMode.PERSISTENT // 节点类型
);
System.out.println(path);
}
}
打印信息——报错
DEBUG org.apache.zookeeper.Client***xnSocketNIO - Ignoring exception during shutdown input
java.***.SocketException: Socket is not connected
at sun.nio.ch.***.translateToSocketException(***.java:123)
at sun.nio.ch.***.translateException(***.java:157)
at sun.nio.ch.***.translateException(***.java:163)
at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401)
at org.apache.zookeeper.Client***xnSocketNIO.cleanup(Client***xnSocketNIO.java:200)
三、原因分析:
服务器zookeeper版本和pom依赖版本不一致导致的
四、解决方案:
将pom依赖版本服务器zookeeper版本
服务器版本查看3.8.2版本:
[root@zookeeper01 lib]# pwd
/opt/zookeeper/lib
[root@zookeeper01 lib]#
[root@zookeeper01 lib]# ll -r
总用量 12192
-rw-r--r--. 1 root root 23744 8月 31 00:20 zookeeper-prometheus-metrics-3.8.2.jar
-rw-r--r--. 1 root root 256137 8月 31 00:20 zookeeper-jute-3.8.2.jar
-rw-r--r--. 1 root root 1328066 8月 31 00:20 zookeeper-3.8.2.jar
-rw-r--r--. 1 root root 11358 8月 31 00:20 snappy-java-1.1.10.1.jar_LICENSE.txt
更改pom依赖版本为3.8.2
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>***.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>