我正在运行一个简单的演示,它使用Java zookeeper客户端连接zookeeper服务器。在windows上连接成功需要很长时间(至少10秒钟),但在Linux上连接成功非常快。原因是什么,如何处理?
软件版本:
操作系统:Windows 11有两个虚拟机,一个是Ubuntu服务器,另一个是Ubuntu桌面,
Java:OpenJDK 11,
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条答案
按热度按时间xxls0lw81#
我不能说这是你的问题,但你有一些类型的防病毒软件运行在Windows框是扫描jar文件的访问?我过去在一台服务器上遇到过这个问题,其中防病毒更新导致所有Zookeeper连接从不到一秒连接到大约60秒,而访问扫描程序正在检查所有被触摸/加载的jar文件,包括Zookeeper和JVM库。如果你有防病毒软件,你可以尝试在运行连接测试时暂时禁用它,至少排除这种可能性。
eivgtgni2#
通过调试Zookeeper的源代码,我终于发现了这个问题。在JVM示例化Zookeeper对象之前,它执行Zookeeper.class中的静态块,加载您的环境。有一个方法
InetAddress.getLocalHost().getCanonicalHostName()
使代码运行缓慢。我在getcanonicalhostname-is-very-slow中找到了解决方案。这是Npcap Loopback Adapter
导致执行缓慢的原因。