使用kazoo模块,用python3来操作zookeeper
#新建znode节点
from kazoo import client as kz_client
zk = kz_client.KazooClient(hosts='10.47.199.136:2015')
zk.start(timeout=5)
try:
zk.create('/yy')
except Exception as e:
print(e)
for i in range(1000,9999999):
try:
zk.create('/yy/e%s'%i)
print(i,end='')
except Exception as e:
print(e)
zk.stop()
#删除znode
from kazoo import client as kz_client
zk = kz_client.KazooClient(hosts='10.47.199.136:2015,10.47.199.133:2015,10.47.199.132:2015')
zk.start(timeout=5)
for i in range(4399285,9999999):
if zk.exists('/yy/e%s'%i):
try:
zk.delete('/yy/e%s'%i)
print(i,end=' ')
except Exception as e:
pass
zk.stop()
########################
from kazoo import client as kz_client
zk = kz_client.KazooClient(hosts='10.47.199.136:2015')
zk.start(timeout=5)
def my_func(event):
print("*************")
children = zk.get_children("/test", watch=my_func)
def connection_listener(state):
if state == "LOST":
# Register somewhere that the session was lost
pass
elif state == "SUSPENDED":
# Handle being disconnected from Zookeeper
pass
else:
# Handle being connected/reconnected to Zookeeper
pass
#zk.add_listener(connection_listener)
create -s
/NODE_NAME
DATA
# -e参数为创建临时节点,如果不带参数则创建持久节点
create -s -e
/NODE_NAME
DATA ##创建临时顺序节点
zk集群确保存在leader状态服务,在leader中执行
echo mntr |nc localhost 2015
返回:
zk_version 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
zk_avg_latency 0
zk_max_latency 0
zk_min_latency 0
zk_packets_received 4
zk_packets_sent 3
zk_num_alive_connections 1
zk_outstanding_requests 0
zk_server_state leader
zk_znode_count 1013
zk_watch_count 0
zk_ephemerals_count 1
zk_approximate_data_size 8188
zk_open_file_descriptor_count 33
zk_max_file_descriptor_count 102400
zk_followers 2 ###zk_followers数量
zk_synced_followers 2 ###zk_followers同步数量
zk_pending_syncs 0