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客户端代码

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.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   23744 831 00:20 zookeeper-prometheus-metrics-3.8.2.jar
-rw-r--r--. 1 root root  256137 831 00:20 zookeeper-jute-3.8.2.jar
-rw-r--r--. 1 root root 1328066 831 00:20 zookeeper-3.8.2.jar
-rw-r--r--. 1 root root   11358 831 00:20 snappy-java-1.1.10.1.jar_LICENSE.txt

更改pom依赖版本为3.8.2

    
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.8.2version>
        dependency>
        <dependency>
            <groupId>com.github.sgroschupfgroupId>
            <artifactId>zkclientartifactId>
            <version>0.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
    dependencies>

你可能感兴趣的:(常见问题,zookeeper,zookeeper,分布式,云原生,java)