1、客户端连接ZK出现下面的异常信息
2017-11-02 15:36:42,489 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@873] - Refusing session request for client /127.0.0.1:49912 as it has seen zxid 0x2a1 our last zxid is 0x0 client must try another server
2017-11-02 15:36:42,489 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1008] - Closed socket connection for client /127.0.0.1:49912 (no session established for client)
2017-11-02 15:36:42,752 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /10.18.104.47:49915
2017-11-02 15:36:42,753 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@873] - Refusing session request for client /10.18.104.47:49915 as it has seen zxid 0x609 our last zxid is 0x0 client must try another server
2017-11-02 15:36:42,754 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1008] - Closed socket connection for client /10.18.104.47:49915 (no session established for client)
2017-11-02 15:36:44,324 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /10.18.104.47:49924
2017-11-02 15:36:44,325 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@873] - Refusing session request for client /10.18.104.47:49924 as it has seen zxid 0x609 our last zxid is 0x0 client must try another server
2017-11-02 15:36:44,325 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1008] - Closed socket connection for client /10.18.104.47:49924 (no session established for client)
2017-11-02 15:36:44,555 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /127.0.0.1:49926
2017-11-02 15:36:44,555 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@873] - Refusing session request for client /127.0.0.1:49926 as it has seen zxid 0x2a1 our last zxid is 0x0 client must try another server
可以看出客户端一直在重连!使用连接不上。
2、为什么会这样呢?
那是因为我在重启ZK的时候,曾经删除过数据目录,这样造成zkid存在问题。客户端记录的是某个id,而服务端重启之后并且删除数据目录之后,id编号从0x0开始。
Zookeeper的zxid会由于状态的变更主键递增1,为了保证事务的顺序一致性,zookeeper采用了递增的事务id号(zxid)来标识事务。这在选举的时候启动至关重要。
所以一旦客户端的id比服务端大,服务端是拒绝连接的。