如何以编程方式检测客户端连接到 ZooKeeper 集成中的哪个服务器?
我正在使用Apache Curator API,并通过注册ConnectionStateListener来监听连接状态的变化。如果客户端连接的服务器出现故障,我想知道当客户端重新连接时客户端连接到集合中的哪个服务器。
如何以编程方式检测客户端连接到 ZooKeeper 集成中的哪个服务器?
我正在使用Apache Curator API,并通过注册ConnectionStateListener来监听连接状态的变化。如果客户端连接的服务器出现故障,我想知道当客户端重新连接时客户端连接到集合中的哪个服务器。
您可以在 Curator 生成的日志中看到这一点。在下面的示例输出中,CuratorFramework
客户端已获得 4 个connectionString
可以连接的不同 ZooKeeper 实例。从日志中可以看出,它选择了第一个:
21:13:45.384 [main] INFO org.apache.curator.framework.imps.CuratorFrameworkImpl - Starting
21:13:45.386 [main] INFO org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@2876f0c
21:13:45.388 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
21:13:45.388 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
21:13:45.392 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x14aac461eb70004, negotiated timeout = 40000
如果客户端连接的 ZooKeeper 服务器崩溃,您还将在日志中看到客户端连接的新服务器:
21:23:03.675 [main-SendThread(127.0.0.1:2182)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
21:23:03.677 [main-SendThread(127.0.0.1:2182)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to 127.0.0.1/127.0.0.1:2182, initiating session
21:23:03.697 [main-SendThread(127.0.0.1:2182)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server 127.0.0.1/127.0.0.1:2182, sessionid = 0x14aac461eb70004, negotiated timeout = 40000
21:23:03.697 [main-EventThread] INFO org.apache.curator.framework.state.ConnectionStateManager - State change: RECONNECTED