org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server with timeout

问题介绍


zookeeper客户端连接到zookeeper节点有20秒的延迟,而且每次都是20秒。自己编写客户端进行测试,通过日志看到在某个环节上会出现阻塞,导致延迟

报错内容


org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server 10.3.139.117:2181 with timeout 5000ms

Opening socket connection to server 10.3.139.117/10.3.139.117:2181. Will not attempt to authenticate using SASL (unknown error)

15:49:12.424 [main] INFO  org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=x.x.x.x:2181 sessionTimeout=5000 watcher=zk.ZKTest$1@393671df
15:49:12.427 [main] INFO  org.apache.zookeeper.common.X509Util - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
15:49:12.431 [main] INFO  org.apache.zookeeper.ClientCnxnSocket - jute.maxbuffer value is 1048575 Bytes
15:49:12.436 [main] INFO  org.apache.zookeeper.ClientCnxn - zookeeper.request.timeout value is 0. feature enabled=false

15:49:32.465 [main-SendThread(x.x.x.x:2181)] INFO  org.apache.zookeeper.ClientCnxn - Opening socket connection to server x.x.x.x/x.x.x.x:2181.
15:49:32.465 [main-SendThread(x.x.x.x:2181)] INFO  org.apache.zookeeper.ClientCnxn - SASL config status: Will not attempt to authenticate using SASL (unknown error)
15:49:32.477 [main-SendThread(x.x.x.x:2181)] INFO  org.apache.zookeeper.ClientCnxn - Socket connection established, initiating session, client: /172.20.140.23:56244, server: x.x.x.x/x.x.x.x:2181
15:49:32.498 [main-SendThread(x.x.x.x:2181)] INFO  org.apache.zookeeper.ClientCnxn - Session establishment complete on server x.x.x.x/x.x.x.x:2181, session id = 0x201249468f70009, negotiated timeout = 5000

问题修复


在上报错内容中可以看到服务连接zookeeper超时

org.I0Itec.zkclient.exception.ZkTimeoutException: Unable to connect to zookeeper server 10.3.139.117:2181 with timeout 5000ms

通过上述过程,可以确定是zookeeper默认使用了ZooKeeperSaslClient,而这个过程中调用了getHostName方法,从而造成程序阻塞。

所以可以通过在hosts文件添加上zookeeper的ip地址,使得getHostName可以直接使用hosts里面的dns结果。

vim /etc/hosts

10.3.141.115	zk1
10.3.141.116	zk2
10.3.139.117	zk3

总结


以后一定谨慎使用InetSocketAddress的getHostName方法,这个方法会对传进去的字符串进行域名解析,即使是一个ip地址也会进行解析,而dns解析的快慢和服务器配置有关系

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/1103.html

发表评论

登录后才能评论