问题描述
如何以编程方式检测客户端连接到 ZooKeeper 集合中的哪个服务器?
How to programmatically detect which server in a ZooKeeper ensemble a client is connected to?
我正在使用 Apache Curator API,我正在通过注册 ConnectionStateListener.如果客户端连接的服务器出现故障,我想知道当客户端重新连接时客户端连接到整体中的哪个服务器.
I'm using the Apache Curator API and I am listening for state changes in connection by registering ConnectionStateListener. I would like to know which server in the ensemble a client is connected to when the client reconnects if the server it was connected to goes down.
推荐答案
您可以在 Curator 生成的日志中看到这一点.在下面的示例输出中,CuratorFramework
客户端在它可以连接的 connectionString
中获得了 4 个不同的 ZooKeeper 实例.从日志中可以看出,它选择了第一个:
You can see this in the logs produced by Curator. In the example output below, the CuratorFramework
client has been given 4 different ZooKeeper instances in the connectionString
that it can connect to. As can be seen in the log, it choses the first:
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 服务器崩溃,您还会在日志中看到客户端连接的新服务器:
In case the ZooKeeper server that the client has connected to crashes, you will also see the new server that the client connects to in the logs:
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
这篇关于如何以编程方式检测集成客户端中的哪个服务器连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!