我正在运行一个简单的演示,使用java zookeeper客户端连接zookeeper服务器。在windows上连接成功需要很长时间(至少10秒),但在linux上连接成功非常快。原因是什么?如何处理?非常感谢。
软件版本:
OS:win11有两个虚拟机,一个是ubuntu服务器,另一个是ubuntu桌面,
Java:开放式JDK11,
zookeeper服务器:版本3.7.0,部署在ubuntu虚拟机上
演示代码:
public static void main(String[] args) throws Exception {
log.info("begin connect to zookeeper ================================>>");
ZooKeeper zooKeeper = new ZooKeeper("192.168.1.16", 30000, watchedEvent -> {
});
log.info("connect zookeeper success =================================>>");
zooKeeper.close();
log.info("zookeeper is closed...");
}
结果和日志:
1.在Win11上运行演示代码:
[04-04-22 11:57:32:013 CST] main INFO provider.ProviderStartup: begin connect to zookeeper ================================>>
[04-04-22 11:57:41:087 CST] main INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
[04-04-22 11:57:41:087 CST] main INFO zookeeper.ZooKeeper: Client environment:java.version=11
[04-04-22 11:57:41:088 CST] main INFO zookeeper.ZooKeeper: Client environment:java.home=D:\jdk-11
[04-04-22 11:57:41:088 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 10
[04-04-22 11:57:41:088 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[04-04-22 11:57:41:088 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=10.0
[04-04-22 11:57:41:096 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.1.16 sessionTimeout=30000 watcher=com.gonnaup.dubbo.provider.ProviderStartup$$Lambda$29/0x00000008000dc840@799d4f69
[04-04-22 11:57:41:102 CST] main INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
[04-04-22 11:57:41:384 CST] main INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes
[04-04-22 11:57:41:393 CST] main INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false
[04-04-22 11:57:41:393 CST] main INFO provider.ProviderStartup: connect zookeeper success =================================>>
[04-04-22 11:57:59:472 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.1.16/192.168.1.16:2181.
[04-04-22 11:57:59:473 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)
[04-04-22 11:57:59:476 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: Socket connection established, initiating session, client: /192.168.1.8:2474, server: 192.168.1.16/192.168.1.16:2181
[04-04-22 11:57:59:596 CST] main INFO provider.ProviderStartup: zookeeper is closed...
[04-04-22 11:57:59:596 CST] main-EventThread INFO zookeeper.ClientCnxn: EventThread shut down for session: 0x100001dd5b40058
我省略了一些无用的日志,以减少字数,从日志记录时间可以看出,从连接到结束需要很长时间(参见'====〉〉'标志)。
1.在ubuntu桌面虚拟机上运行演示代码:
[04-04-22 11:57:03:739 CST] main INFO simple.ZookeeperCli: begin connect to zookeeper ================================>>
[04-04-22 11:57:03:785 CST] main INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
[04-04-22 11:57:03:786 CST] main INFO zookeeper.ZooKeeper: Client environment:java.version=11.0.14.1
[04-04-22 11:57:03:788 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
[04-04-22 11:57:03:788 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[04-04-22 11:57:03:788 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=5.13.0-39-generic
[04-04-22 11:57:03:800 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.1.16 sessionTimeout=30000 watcher=cn.gonnaup.simple.ZookeeperCli$$Lambda$31/0x0000000840083040@47f37ef1
[04-04-22 11:57:03:808 CST] main INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
[04-04-22 11:57:03:818 CST] main INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes
[04-04-22 11:57:03:838 CST] main INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false
[04-04-22 11:57:03:849 CST] main INFO simple.ZookeeperCli: connect zookeeper success =================================>>
[04-04-22 11:57:03:941 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.1.16/192.168.1.16:2181.
[04-04-22 11:57:03:943 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)
[04-04-22 11:57:03:964 CST] main-SendThread(192.168.1.16:2181) INFO zookeeper.ClientCnxn: Socket connection established, initiating session, client: /10.0.2.15:56596, server: 192.168.1.16/192.168.1.16:2181
[04-04-22 11:57:04:109 CST] main INFO zookeeper.ZooKeeper: Session: 0x100001dd5b40057 closed
[04-04-22 11:57:04:109 CST] main INFO simple.ZookeeperCli: zookeeper is closed...
[04-04-22 11:57:04:109 CST] main-EventThread INFO zookeeper.ClientCnxn: EventThread shut down for session: 0x100001dd5b40057
可以看到连接Zookeeper和关闭连接是立即完成。
2条答案
按热度按时间1tuwyuhd1#
我不能说这是你的问题,但你有某种类型的防病毒软件运行在Windows机器上,扫描jar文件的访问?我遇到过这个问题在过去的一个服务器上,其中一个防病毒更新导致所有Zookeeper连接从不到一秒的连接到大约60秒,而按访问扫描程序正在检查所有的jar文件被接触/加载,包括Zookeeper和JVM库。如果你有防病毒软件,你可以在运行连接测试时暂时禁用它,至少排除这种可能性。
7cwmlq892#
通过调试Zookeeper的源代码,我终于发现了这个问题。在JVM示例化Zookeeper对象之前,它执行了Zookeeper.class中的静态块,加载了您的环境。有一个方法
InetAddress.getLocalHost().getCanonicalHostName()
使代码运行缓慢。我在getcanonicalhostname-is-very-slow中找到了解决方法。这就是Npcap Loopback Adapter
导致执行缓慢的原因。