0


zookeeper连接客户端操作数据时报错Socket is not connected

文章目录


一、报错信息

DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown input
java.net.SocketException: Socket is not connected
    at sun.nio.ch.Net.translateToSocketException(Net.java:123)
    at sun.nio.ch.Net.translateException(Net.java:157)
    at sun.nio.ch.Net.translateException(Net.java:163)
    at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401)
    at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:200)

二、问题描述

在使用java连接zookeeper客户端时,发现连接是成功的,但是操作数据时报错

连接zookeeper客户端代码

publicclassTest{//此处是机器ip:端口号。多个ip用,隔开privatestaticString connectString ="机器ip:2181";int sessionTimeout =5000;@org.junit.Testpublicvoidtest()throwsIOException{ZooKeeper zooKeeper =newZooKeeper(connectString, sessionTimeout,newWatcher(){/**
             * 监听事件
             * @param event
             */@Overridepublicvoidprocess(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代码

publicclassTest1{String connectString ="192.168.75.130:2181,192.168.75.131:2181,192.168.75.132:2181";int sessionTimeout =5000;ZooKeeper zooKeeper =null;@Beforepublicvoidtest()throwsIOException{
        zooKeeper =newZooKeeper(connectString, sessionTimeout,newWatcher(){/**
             * 监听事件
             * @param event
             */@Overridepublicvoidprocess(WatchedEvent event){System.out.println("监听事件"+ event);}});//        System.out.println(zooKeeper);}/**
     * 创建节点
     */@TestpublicvoidcreateNode()throwsException{String path = zooKeeper.create("/apptest"// 节点路径,"HelloZookeeper".getBytes()// 节点的数据,ZooDefs.Ids.OPEN_ACL_UNSAFE// 权限,CreateMode.PERSISTENT// 节点类型);System.out.println(path);}}

打印信息——报错

DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown input
java.net.SocketException: Socket is not connected
    at sun.nio.ch.Net.translateToSocketException(Net.java:123)
    at sun.nio.ch.Net.translateException(Net.java:157)
    at sun.nio.ch.Net.translateException(Net.java:163)
    at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401)
    at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.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   237448月  31 00:20 zookeeper-prometheus-metrics-3.8.2.jar
-rw-r--r--. 1 root root  2561378月  31 00:20 zookeeper-jute-3.8.2.jar
-rw-r--r--. 1 root root 13280668月  31 00:20 zookeeper-3.8.2.jar
-rw-r--r--. 1 root root   113588月  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>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>

本文转载自: https://blog.csdn.net/weixin_45112292/article/details/133896071
版权归原作者 zero 所有, 如有侵权,请联系我们删除。

“zookeeper连接客户端操作数据时报错Socket is not connected”的评论:

还没有评论