WSL环境下Apache Zookeeper 3.8客户端连接被拒绝求助
WSL中连接ZooKeeper 3.8客户端出现Connection refused错误的解决方案
问题描述
我正尝试在WSL(Windows子系统)中连接Apache Zookeeper 3.8客户端,但运行bin/zkCli.sh时出现Connection refused错误。以下是输出信息:
Welcome to ZooKeeper! 2022-08-21 09:42:24,372 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1171] - 正在打开与服务器localhost/127.0.0.1:2181的套接字连接。 2022-08-21 09:42:24,373 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1173] - SASL配置状态:不会尝试使用SASL进行身份验证(未知错误) 2022-08-21 09:42:24,408 [myid:localhost:2181] - WARN [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1292] - 服务器localhost/127.0.0.1:2181的会话0x0,正在关闭套接字连接。尝试重新连接,除非是SessionExpiredException。 java.net.ConnectException: Connection refused at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:777) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:344) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1282) JLine support is enabled [zk: localhost:2181(CONNECTING) 0] 2022-08-21 09:42:25,512 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1171] - 正在打开与服务器localhost/127.0.0.1:2181的套接字连接。 2022-08-21 09:42:25,512 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1173] - SASL配置状态:不会尝试使用SASL进行身份验证(未知错误) 2022-08-21 09:42:25,515 [myid:localhost:2181] - WARN [main-SendThread(localhost:2181):o.a.z.ClientCnxn$SendThread@1292] - 服务器localhost/127.0.0.1:2181的会话0x0,正在关闭套接字连接。尝试重新连接,除非是SessionExpiredException。 java.net.ConnectException: Connection refused at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:777) at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:344) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1282)
解决方案
确认ZooKeeper服务状态
在WSL终端执行bin/zkServer.sh status查看服务运行状态。若显示"ZooKeeper is not running",先启动服务:bin/zkServer.sh start。启动失败的话,检查conf/zoo.cfg配置文件,确保dataDir指定的路径存在,且配置格式无误。检查端口2181占用情况
用netstat -tulpn | grep 2181或lsof -i :2181命令查看端口是否被其他进程占用。如果被占用,可停止对应进程,或者修改conf/zoo.cfg中的clientPort为其他未被占用的端口,之后重启ZooKeeper服务。区分ZooKeeper运行环境(WSL内部/Windows主机)
如果ZooKeeper安装在Windows主机而非WSL内:- 确保Windows防火墙允许2181端口的入站连接;
- 连接时使用Windows主机的实际IP地址,而非localhost,命令示例:
bin/zkCli.sh -server 192.168.x.x:2181(替换为你的Windows本地IP)。
调整ZooKeeper监听地址
打开conf/zoo.cfg,若配置了listenAddress且值为特定IP,WSL的localhost无法访问,需将其改为0.0.0.0以允许所有地址监听,修改后重启ZooKeeper服务。测试端口连通性
在WSL中执行telnet localhost 2181测试端口是否可达,若连接失败,说明服务未启动或端口未开放,回到前面步骤排查。
内容的提问来源于stack exchange,提问作者Renato Bueno
相关产品推荐
相关产品推荐

