ZooKeeper-3.2 建立ZooKeeper会话- 高飞网

3.2 建立ZooKeeper会话

2018-05-29 11:43:29.0

尝试封装一个zk客户端,先上代码

public class ZKClient {
    private final static String connectString = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
    private final static int sessionTimeout = 1500;
    private ZooKeeper zk = null;
    private static ZKClient zkClient = null;

    private ZKClient() {
        try {
            this.zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println(watchedEvent);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static synchronized ZKClient instance() {
        if (zkClient == null) {
            zkClient = new ZKClient();
        }
        return zkClient;
    }
    public void close() {
        if (zk != null) {
            try {
                zk.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

解释:

ZooKeeper的构造方法中,connectString:连接字符串,ip:port,ip:port....
sessionTimeout:会话超时时间,一般情况下,是5-10秒
Watcher接口:会话通过Watcher接口来监视会话的健康状况。

运行一下,可以看到了第一个服务端节点:

2018-05-29 11:42:14:178 INFO [main] org.apache.zookeeper.ZooKeeper | Initiating client connection, connectString=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 sessionTimeout=1500 watcher=com.zk.ZKClient$1@1e6d1014
2018-05-29 11:42:14:257 INFO [main-SendThread(127.0.0.1:2181)] 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)
2018-05-29 11:42:14:405 INFO [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn | Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2018-05-29 11:42:14:429 INFO [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn | Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x163a577a6d30003, negotiated timeout = 4000
WatchedEvent state:SyncConnected type:None path:null
2018-05-29 11:43:54:266 INFO [main] org.apache.zookeeper.ZooKeeper | Session: 0x163a577a6d30003 closed

如果此时关闭连接的服务端(这里就是第一个节点),会显示下面的日志:

2018-05-29 15:38:23:106 INFO [main-SendThread(127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn | Unable to read additional data from server sessionid 0x163aad377240000, likely server has closed socket, closing socket connection and attempting reconnect
WatchedEvent state:Disconnected type:None path:null
2018-05-29 15:38:24:032 INFO [main-SendThread(127.0.0.1:2182)] 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)
2018-05-29 15:38:24:036 INFO [main-SendThread(127.0.0.1:2182)] org.apache.zookeeper.ClientCnxn | Socket connection established to 127.0.0.1/127.0.0.1:2182, initiating session
2018-05-29 15:38:24:068 INFO [main-SendThread(127.0.0.1:2182)] org.apache.zookeeper.ClientCnxn | Session establishment complete on server 127.0.0.1/127.0.0.1:2182, sessionid = 0x163aad377240000, negotiated timeout = 4000
WatchedEvent state:SyncConnected type:None path:null

可见会话自动连接到了第二个节点。

注意:ZooKeeper管理连接

请不要自己尝试去管理ZooKeeper客户端连接。ZooKeeper客户端库会监控与服务端之间的连接,客户端库不仅告诉我们连接发生的问题,还会主动尝试重新建立连接。而且这个时间很短。所以一般不要关闭会话后再启动一个新会话,这样会增加系统负载,并导致更长时间的中断。

使用stat命令,查看服务端状态

节点1:

xuyanhuadeMacBook-Air:zookeeper-3.4.10 xuyanhua$ telnet 127.0.0.1 2181
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stat
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Clients:
 /127.0.0.1:55195[1](queued=0,recved=4,sent=4)
 /127.0.0.1:55201[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/3
Received: 6
Sent: 5
Connections: 2
Outstanding: 0
Zxid: 0x400000006
Mode: follower
Node count: 5
Connection closed by foreign host.

节点2:

xuyanhuadeMacBook-Air:zookeeper-3.4.10 xuyanhua$ telnet 127.0.0.1 2182
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stat
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Clients:
 /127.0.0.1:55226[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/4
Received: 64
Sent: 63
Connections: 1
Outstanding: 0
Zxid: 0x400000006
Mode: follower
Node count: 5
Connection closed by foreign host.

节点3:

xuyanhuadeMacBook-Air:zookeeper-3.4.10 xuyanhua$ telnet 127.0.0.1 2183
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stat
Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
Clients:
 /127.0.0.1:55245[0](queued=0,recved=1,sent=0)

Latency min/avg/max: 0/0/3
Received: 53
Sent: 52
Connections: 1
Outstanding: 0
Zxid: 0x400000006
Mode: leader
Node count: 5
Connection closed by foreign host.

可见,第3个节点是群首,1、2都是追随者。


上一篇:2.2 ZooKeeper架构
下一篇:3.3 获取管理权