java连接不上安装在vmware上的linux上的zk

java连接不上安装在vmware上的linux上的zk

代码如下:

package
com.imooc;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
* 描述:连接到ZK服务端,打印连接状态
*/
public class ZKConnect implements Watcher {
public static final String SERVER_PATH="192.168.136.137:2181";
public static final Integer TIMOUT = 5000;//毫秒

   public static void main(String[] args) throws IOException, InterruptedException {
/**
        * 客户端和服务端他们是异步连接,连接成功之后客户端会受到watcher通知。
        * connecString:服务器的IP+端口,比如127.0.0.1:2181
        * sessionTimeout:超时时间
        * watcher:通知事件
        */
       ZooKeeper zk = new ZooKeeper(SERVER_PATH,TIMOUT,new ZKConnect());
System.out.println("客户端开始连接ZK服务器了");
System.out.println(zk.getState());
Thread.sleep(2000);
System.out.println(zk.getState());
}


@Override
   public void process(WatchedEvent event) {
System.out.println("收到了通知"+event);
}
}

各种尝试,连接不上zookeeper:

​"C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" "-javaagent:E:\IDEA\IntelliJ IDEA Community Edition 2021.1.3\lib\idea_rt.jar=49383:E:\IDEA\IntelliJ IDEA Community Edition 2021.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;E:\com.imooc\zk-prace\target\classes;C:\Users\lenovo\.m2\repository\org\apache\zookeeper\zookeeper\3.6.0\zookeeper-3.6.0.jar;C:\Users\lenovo\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\lenovo\.m2\repository\org\apache\zookeeper\zookeeper-jute\3.6.0\zookeeper-jute-3.6.0.jar;C:\Users\lenovo\.m2\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-handler\4.1.45.Final\netty-handler-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-common\4.1.45.Final\netty-common-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-buffer\4.1.45.Final\netty-buffer-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport\4.1.45.Final\netty-transport-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-resolver\4.1.45.Final\netty-resolver-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-codec\4.1.45.Final\netty-codec-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport-native-epoll\4.1.45.Final\netty-transport-native-epoll-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport-native-unix-common\4.1.45.Final\netty-transport-native-unix-common-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\lenovo\.m2\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;C:\Users\lenovo\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.imooc.ZKConnect
10:29:28 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.6.0--b4c89dc7f6083829e18fae6e446907ae0b1f22d7, built on 02/25/2020 14:38 GMT
10:29:28 INFO zookeeper.ZooKeeper: Client environment:host.name=DESKTOP-772UAQU
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.version=1.8.0_162
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.home=C:\Program Files\Java\jdk1.8.0_162\jre
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.class.path=C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;E:\com.imooc\zk-prace\target\classes;C:\Users\lenovo\.m2\repository\org\apache\zookeeper\zookeeper\3.6.0\zookeeper-3.6.0.jar;C:\Users\lenovo\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\lenovo\.m2\repository\org\apache\zookeeper\zookeeper-jute\3.6.0\zookeeper-jute-3.6.0.jar;C:\Users\lenovo\.m2\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-handler\4.1.45.Final\netty-handler-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-common\4.1.45.Final\netty-common-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-buffer\4.1.45.Final\netty-buffer-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport\4.1.45.Final\netty-transport-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-resolver\4.1.45.Final\netty-resolver-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-codec\4.1.45.Final\netty-codec-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport-native-epoll\4.1.45.Final\netty-transport-native-epoll-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\io\netty\netty-transport-native-unix-common\4.1.45.Final\netty-transport-native-unix-common-4.1.45.Final.jar;C:\Users\lenovo\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\lenovo\.m2\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;C:\Users\lenovo\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;E:\IDEA\IntelliJ IDEA Community Edition 2021.1.3\lib\idea_rt.jar
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.library.path=C:\Program Files\Java\jdk1.8.0_162\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\MySQL\MySQL Server 8.0\bin;E:\apache-maven-3.8.1\bin;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;E:\java\bin;;E:\IDEA\IntelliJ IDEA Community Edition 2021.1.3\bin;;.
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\lenovo\AppData\Local\Temp\
10:29:28 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 10
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.version=10.0
10:29:28 INFO zookeeper.ZooKeeper: Client environment:user.name=lenovo
10:29:28 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\lenovo
10:29:28 INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\com.imooc\zk-prace
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.memory.free=226MB
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.memory.max=3518MB
10:29:28 INFO zookeeper.ZooKeeper: Client environment:os.memory.total=238MB
10:29:28 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.136.137:2181 sessionTimeout=5000 watcher=com.imooc.ZKConnect@28c97a5
10:29:28 INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
10:29:33 INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes
10:29:33 INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false
客户端开始连接ZK服务器了
CONNECTING
CONNECTING

Process finished with exit code 0

刚开始怀疑虚拟机给内存太低了,调整到8G,还是有问题:
linux登录zk正常:

​-rwxr-xr-x. 1 1000 1000 238 2月 20 2014 README.txt
-rwxr-xr-x. 1 1000 1000 1937 2月 20 2014 zkCleanup.sh
-rwxr-xr-x. 1 1000 1000 1049 2月 20 2014 zkCli.cmd
-rwxr-xr-x. 1 1000 1000 1534 2月 20 2014 zkCli.sh
-rwxr-xr-x. 1 1000 1000 1333 2月 20 2014 zkEnv.cmd
-rwxr-xr-x. 1 1000 1000 2696 2月 20 2014 zkEnv.sh
-rwxr-xr-x. 1 1000 1000 1084 2月 20 2014 zkServer.cmd
-rwxr-xr-x. 1 1000 1000 5742 2月 20 2014 zkServer.sh
-rw-r--r--. 1 root root 6187 10月 2 18:44 zookeeper.out
[root@localhost bin]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:0c:29:e3:29:e2 brd ff:ff:ff:ff:ff:ff
inet 192.168.136.137/24 brd 192.168.136.255 scope global eth0
inet6 fe80::20c:29ff:fee3:29e2/64 scope link
valid_lft forever preferred_lft forever
[root@localhost bin]# sh zkServer.sh status
JMX enabled by default
Using config: /root/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
[root@localhost bin]#


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小尤 2021-10-06 14:08:00

同学你好,可能是因防火墙导致无法进行连接,则建议同学关闭服务器防火墙,然后重新连接试一下。

./bin/zkServer.sh start

​祝学习愉快!

  • 提问者 张守祝 #1

    解决了,linux防火墙关闭了,windows防火墙没有关闭,把windows防火墙开放权限就可以了

    2021-10-06 14:30:03
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师