1. 环境
服务器:CentOS Linux release 7.9.2009 (Core)
zookeeper:3.6.3
java:1.8.0_231
2. 问题
zookeeper客户端连接到zookeeper节点有20秒的延迟,而且每次都是20秒。自己编写客户端进行测试,通过日志看到在某个环节上会出现阻塞,导致延迟。
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
3. 解决过程
3.1 编写测试客户端,客户端只是连接到zookeeper
package zk;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.client.ConnectStringParser;
import org.apache.zookeeper.client.StaticHostProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* @author cc
* @function
* @date 2021/7/22 15:14
*/
public class ZKTest {
private static final Logger LOG = LoggerFactory.getLogger(ZKTest.class);
public static void main(String[] args) throws Exception {
ZKTest main = new ZKTest();
main.run(args[0]);
}
String date() {
return new SimpleDateFormat("HH:mm:ss.SSS").format(System.currentTimeMillis());
}
void run(String zkIps) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
LOG.info("running...");
StaticHostProvider providers = new StaticHostProvider(new ConnectStringParser

本文详述了一起关于Zookeeper客户端连接延迟20秒的问题,经过日志分析和源代码审查,发现原因是ZookeeperSaslClient在尝试创建连接时调用getHostName方法进行DNS解析导致的阻塞。解决方案是在hosts文件中添加Zookeeper服务器的IP地址记录,避免DNS解析延迟。文章强调了在使用InetSocketAddress.getHostName方法时可能引发的潜在问题,并分享了问题排查和修复的思路。

2万+

被折叠的 条评论
为什么被折叠?



