报错记录ZooKeeper报java.net.SocketException: Socket is not connected无法连接上

Posted DCTANT

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了报错记录ZooKeeper报java.net.SocketException: Socket is not connected无法连接上相关的知识,希望对你有一定的参考价值。

ZooKeeper版本:3.5.7
CentOS版本:7.9

由于我在学习ZooKeeper的阶段中,ZooKeeper都部署在虚拟机里了,心想着我就不配hosts了吧,反正虚拟机早晚要删掉。然后用java连接ZooKeeper就报错了,报:

ava.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:198)
	at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1338)
	at org.apache.zookeeper.ClientCnxn$SendThread.cleanAndNotifyState(ClientCnxn.java:1276)
	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1254)
Caused by: java.nio.channels.NotYetConnectedException: null
	at sun.nio.ch.SocketChannelImpl.shutdownInput(SocketChannelImpl.java:782)
	at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:399)
	... 4 common frames omitted
15:58:53.824 [main-SendThread(192.168.5.6:2181)] DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown output
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.shutdownOutput(SocketAdaptor.java:409)
	at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:205)
	at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1338)
	at org.apache.zookeeper.ClientCnxn$SendThread.cleanAndNotifyState(ClientCnxn.java:1276)
	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1254)
Caused by: java.nio.channels.NotYetConnectedException: null
	at sun.nio.ch.SocketChannelImpl.shutdownOutput(SocketChannelImpl.java:799)
	at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:407)
	... 4 common frames omitted
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /dct
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:102)
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)
	at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:1538)
	at com.itdct.zoo.zk.ZkClient.create(ZkClient.java:38)
	at com.itdct.zoo.zk.ZkClient.main(ZkClient.java:50)

解决方法是:

用Notepad++打开

C:\\Windows\\System32\\drivers\\etc\\hosts文件,添加ZooKeeper部署的服务器ip,然后保存。

 如果发现hosts文件无法修改,按什么都没反应,大概率是hosts文件的权限是只读,只需要右键属性,去除其只读属性就行了。

 保存的时候可能会提示是否用管理员权限重新打开,点击确定就行了。

如果还是报连接失败,可以按照以下步骤:

1、ping ZooKeeper服务器的ip地址,看看是否能够连通服务器

2、查看ZooKeeper服务器的防火墙,是否客户端端口被防火墙拦截了(可使用:service firewalld stop彻底关闭CentOS的防火墙)

3、确定ZooKeeper客户端的连接端口,是否是端口写错了,具体查看zoo.cfg中的clientPort=2181

4、查看ZooKeeper服务器的状态,查看其是否正常运行,查看方法:
{ZooKeeper安装路径}/bin/zkServer.sh status
如果出现Mode:{foller/leader/standalone}说明ZooKeeper启动是正常的,若出现报错,或者找不到服务,说明ZooKeeper服务端就没有启动成功

附连接代码:
 

package com.itdct.zoo.zk;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class ZkClient {
    public String connectString = "ZK1:2181,ZK2:2181,ZK3:2181";
    private ZooKeeper zooKeeper;

    /**
     * 初始化连接
     */
    public void init() {
        try {
            zooKeeper = new ZooKeeper(connectString, 2000, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println("process: " + watchedEvent.getType());
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建节点
     */
    public void create() {
        try {
            String nodeCreate = zooKeeper.create("/dct", "dct.txt".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("nodeCreate is " + nodeCreate);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ZkClient zkClient = new ZkClient();
        zkClient.init();
        zkClient.create();
    }
}

以上是关于报错记录ZooKeeper报java.net.SocketException: Socket is not connected无法连接上的主要内容,如果未能解决你的问题,请参考以下文章

zookeeper 链接报错 KeeperErrorCode = NodeExists for

zookeeper_exporter启动报错

zookeeper_exporter启动报错

zookeeper启动报错(数据目录权限不对)

zookeeper启动报错:java.io.EOFException

zookeeper集群启动报错:Cannot open channel to * at election address /ip:3888