其他分享
首页 > 其他分享> > zookeeper05-使用ZooKeeper

zookeeper05-使用ZooKeeper

作者:互联网

3、开始使用ZooKeeper

3.2、第一个ZooKeeper会话

]# zkCli.sh    --
...
--客户端启动程序来建立一个会话。
2021-12-22 09:37:37,464 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@73a8dfcc
2021-12-22 09:37:37,476 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-12-22 09:37:37,492 [myid:] - INFO  [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2021-12-22 09:37:37,505 [myid:] - INFO  [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
--客户端尝试连接到localhost/127.0.0.1:2181
2021-12-22 09:37:37,518 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
--客户端连接成功,服务器开始初始化这个新会话。
2021-12-22 09:37:37,675 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:35332, server: localhost/127.0.0.1:2181
--会话初始化成功完成。会话ID(sessionid)是0x20000103b190002
2021-12-22 09:37:37,710 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x20000103b190002, negotiated timeout = 30000

WATCHER::

----服务器向客户端发送一个SyncConnected事件。
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 
--列出根(root)下的所有znode。此刻出/zookeeper之外,znode树为没有其他znode
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
--创建一个名为/workers的znode
[zk: localhost:2181(CONNECTED) 1] create /workers ""
Created /workers
--查看/workers
[zk: localhost:2181(CONNECTED) 2] ls /
[workers, zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /workers 
[]
--删除znode
[zk: localhost:2181(CONNECTED) 4] delete /workers 
[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper]
--退出会话
[zk: localhost:2181(CONNECTED) 6] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-12-22 09:56:02,382 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x20000103b190002 closed
2021-12-22 09:56:02,390 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x20000103b190002

4、一个主-从模式例子的实现

4.1、主节点角色

--开启第一个会话(zkCli.sh),将作为主节点会话

--创建名为/master的znode,以便获得管理权。使用-e标志来表示创建的znode为临时性的。  --添加主机信息并不是必需的,这样做仅仅是为了说明我们可以在需要时添加数据。
[zk: localhost:2181(CONNECTED) 0] create -e /master "master1.example.com:2223"
Created /master
--列出ZooKeeper树的根。
[zk: localhost:2181(CONNECTED) 1] ls /
[master, zookeeper]
--获取/master的数据。
[zk: localhost:2181(CONNECTED) 2] get /master
master1.example.com:2223
--开启第二个会话(zkCli.sh),将作为从节点会话
[zk: localhost:2181(CONNECTED) 0] create -e /master "master2.example.com:2223"
Node already exists: /master
--第二个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 1] stat -w /master
cZxid = 0x100000028
ctime = Wed Dec 22 23:06:11 CST 2021
mZxid = 0x100000028
mtime = Wed Dec 22 23:06:11 CST 2021
pZxid = 0x100000028
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x100004099d30008
dataLength = 24
numChildren = 0
--关闭第一会话,观察第二个会话
[zk: localhost:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeDeleted path:/master
--第二个会话(将成为主节点会话)
[zk: localhost:2181(CONNECTED) 2] create -e /master "master2.example.com:2223"
Created /master

4.2、从节点,任务和任务分配

--开启一个会话创建持久znode,创建后关闭该会话
[zk: localhost:2181(CONNECTED) 0] create /workers ""
Created /workers
[zk: localhost:2181(CONNECTED) 1] create /tasks ""
Created /tesks
[zk: localhost:2181(CONNECTED) 2] create /assign ""
Created /assign
--第二个会话(目前是主节点会话)

--监控/workers的变化
[zk: localhost:2181(CONNECTED) 3] ls -w /workers 
[]
--监控/tesks的变化
[zk: localhost:2181(CONNECTED) 4] ls -w /tasks
[]

4.3、从节点角色 

--重新开启第一个会话(zkCli.sh),将作为从节点会话
[zk: localhost:2181(CONNECTED) 0] create -e /workers/worker1.example.com "worker1.example.com:2223"
Created /workers/worker1.example.com
--第二个会话(目前是主节点会话)
[zk: localhost:2181(CONNECTED) 5] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/workers
--第一个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 1] create /assign/worker1.example.com ""
Created /assign/worker1.example.com
[zk: localhost:2181(CONNECTED) 2] ls -w /assign/worker1.example.com
[]

4.4、客户端角色

--开启第三个会话(zkCli.sh),将作为客户端会话
[zk: localhost:2181(CONNECTED) 0] create -s /tasks/task- "cmd"
Created /tasks/task-0000000000
--第三个会话(目前是客户端会话)
[zk: localhost:2181(CONNECTED) 1] ls -w /tasks/task-0000000000
[]
--第二个会话(目前是主节点会话)
[zk: localhost:2181(CONNECTED) 5] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/tasks
--第二个会话(目前是主节点会话)
[zk: localhost:2181(CONNECTED) 5] ls /tasks 
[task-0000000000]
[zk: localhost:2181(CONNECTED) 6] ls /workers 
[worker1.example.com]
[zk: localhost:2181(CONNECTED) 7] create /assign/worker1.example.com/task-0000000000 ""
Created /assign/worker1.example.com/task-0000000000
--第一个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 3] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/assign/worker1.example.com
--第一个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 3] ls /assign/worker1.example.com
[task-0000000000]
--第一个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 4] create /tasks/task-0000000000/status "done"
Created /tasks/task-0000000000/status
--第三个会话(目前是客户端会话)
[zk: localhost:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/tasks/task-0000000000

[zk: localhost:2181(CONNECTED) 2] get /tasks/task-0000000000
cmd
[zk: localhost:2181(CONNECTED) 3] get /tasks/task-0000000000/status 
done

4.5、三个会话

4.5.3、第一个会话

--第一个会话(目前是主节点会话)
[zk: localhost:2181(CONNECTED) 0] create -e /master "master1.example.com:2223"
Created /master
[zk: localhost:2181(CONNECTED) 1] ls /
[master, zookeeper]
[zk: localhost:2181(CONNECTED) 2] get /master
master1.example.com:2223

--第一个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 0] create -e /workers/worker1.example.com "worker1.example.com:2223"
Created /workers/worker1.example.com
[zk: localhost:2181(CONNECTED) 1] create /assign/worker1.example.com ""
Created /assign/worker1.example.com
[zk: localhost:2181(CONNECTED) 2] ls -w /assign/worker1.example.com
[]
[zk: localhost:2181(CONNECTED) 3] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/assign/worker1.example.com

[zk: localhost:2181(CONNECTED) 3] ls /assign/worker1.example.com
[task-0000000000]
[zk: localhost:2181(CONNECTED) 4] create /tasks/task-0000000000/status "done"
Created /tasks/task-0000000000/status

4.5.2、第二个会话

--第二个会话(目前是从节点会话)
[zk: localhost:2181(CONNECTED) 0] create -e /master "master2.example.com:2223"
Node already exists: /master
[zk: localhost:2181(CONNECTED) 1] stat -w /master
cZxid = 0x100000004
ctime = Thu Dec 23 10:51:28 CST 2021
mZxid = 0x100000004
mtime = Thu Dec 23 10:51:28 CST 2021
pZxid = 0x100000004
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x1000011abdf0000
dataLength = 24
numChildren = 0

--第二个会话(目前是主节点会话)
[zk: localhost:2181(CONNECTED) 2] create -e /master "master2.example.com:2223"
Created /master
[zk: localhost:2181(CONNECTED) 3] ls -w /workers 
[]
[zk: localhost:2181(CONNECTED) 4] ls -w /tasks
[]
[zk: localhost:2181(CONNECTED) 5] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/workers

[zk: localhost:2181(CONNECTED) 5] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/tasks

[zk: localhost:2181(CONNECTED) 5] ls /tasks 
[task-0000000000]
[zk: localhost:2181(CONNECTED) 6] ls /workers 
[worker1.example.com]
[zk: localhost:2181(CONNECTED) 7] create /assign/worker1.example.com/task-0000000000 ""
Created /assign/worker1.example.com/task-0000000000

4.5.3、第三个会话

--第三个会话(目前是客户端会话)
[zk: localhost:2181(CONNECTED) 0] create -s /tasks/task- "cmd"
Created /tasks/task-0000000000
[zk: localhost:2181(CONNECTED) 1] ls -w /tasks/task-0000000000
[]
[zk: localhost:2181(CONNECTED) 2] 
WATCHER::

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/tasks/task-0000000000

[zk: localhost:2181(CONNECTED) 2] get /tasks/task-0000000000
cmd
[zk: localhost:2181(CONNECTED) 3] get /tasks/task-0000000000/status 
done

1

3、开始使用ZooKeeper

3.2、第一个ZooKeeper会话

]# zkCli.sh    --
...
--客户端启动程序来建立一个会话。
2021-12-22 09:37:37,464 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@73a8dfcc
2021-12-22 09:37:37,476 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-12-22 09:37:37,492 [myid:] - INFO  [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2021-12-22 09:37:37,505 [myid:] - INFO  [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
--客户端尝试连接到localhost/127.0.0.1:2181
2021-12-22 09:37:37,518 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
--客户端连接成功,服务器开始初始化这个新会话。
2021-12-22 09:37:37,675 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:35332, server: localhost/127.0.0.1:2181
--会话初始化成功完成。会话ID(sessionid)是0x20000103b190002
2021-12-22 09:37:37,710 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x20000103b190002, negotiated timeout = 30000

WATCHER::

----服务器向客户端发送一个SyncConnected事件。
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] 
--列出根(root)下的所有znode。此刻出/zookeeper之外,znode树为没有其他znode
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
--创建一个名为/workers的znode
[zk: localhost:2181(CONNECTED) 1] create /workers ""
Created /workers
--查看/workers
[zk: localhost:2181(CONNECTED) 2] ls /
[workers, zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /workers 
[]
--删除znode
[zk: localhost:2181(CONNECTED) 4] delete /workers 
[zk: localhost:2181(CONNECTED) 5] ls /
[zookeeper]
--退出会话
[zk: localhost:2181(CONNECTED) 6] quit

WATCHER::

WatchedEvent state:Closed type:None path:null
2021-12-22 09:56:02,382 [myid:] - INFO  [main:ZooKeeper@1422] - Session: 0x20000103b190002 closed
2021-12-22 09:56:02,390 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x20000103b190002

 

标签:zk,--,ZooKeeper,会话,2181,CONNECTED,使用,zookeeper05,localhost
来源: https://www.cnblogs.com/maiblogs/p/15835567.html