python操作zookeeper

有时候对于一些注册到zookeeper的java服务,因开发无法给出相应的端口做监控,所以想通过监控工具对其进行监控比较困难,这里我们可以通过python获取zookeeper的节点来判断。
安装zk:

 wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz
 tar zxf  zookeeper-3.3.6.tar.gz 
 安装zookeeper的c客户端
 cd zookeeper-3.3.6/src/c
 make && make install
 启动zk,并且测试是否生效:
 ./cli_mt localhost:2181
Watcher SESSION_EVENT state = CONNECTED_STATE
Got a new session id: 0x23f9d77d3fe0001

安装python客户端

wget  https://pypi.python.org/packages/14/38/a761465ab0a154405c11f7e5d6e81edf6e84584e114e152fddd340f7d6d3/zkpython-0.4.2.tar.gz
 cd zkpython-0.4.2
 python3 setup.py  install
 
 
 [root@centos3 zkpython-0.4.2]# python3
Python 3.4.7 (default, Sep  5 2017, 17:40:43) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zookeeper
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
出现这个错误请在/etc/profile里添加如下变量: export LD_LIBRARY_PATH=/usr/local/lib

[root@centos3 liujiangbu]# python3
Python 3.4.7 (default, Sep  5 2017, 17:40:43) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>from kazoo.client import KazooClient

vim zk.py

#!/usr/bin/python3
import logging
from time import sleep
from kazoo.client import KazooClient
from kazoo.client import KazooState

zk = KazooClient('127.0.0.1:2181')
zk.start()

#判断zk客户端是否与server连接
def my_listener():

    if zk.state == "LOST":
        print("1111")# Register somewhere that the session was lost
    elif zk.state == "SUSPENDED":
        print("222222")
        # Handle being disconnected from Zookeeper
    else:
        # Handle being connected/reconnected to Zookeeper
        print("6666")

# print log to console
#logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

#zk = KazooClient('127.0.0.1:2181')
#zk.start()

def children_callback(children):
    print('****',children)

children = zk.get_children('/zookeeper', children_callback)
my_listener()
zk.create('/zookeeper/goodboy12364567777')
#zk.delete('/zookeeper/555555')
while True: sleep(1)    

节点监视功能解析:

>>> def children_callback(children):
...      print('****',children)
... 
>>> children = zk.get_children('/zookeeper/goodboy', children_callback)
>>> zk.create('/zookeeper/goodboy/12364567777333')
**** WatchedEvent(type='CHILD', state='CONNECTED', path='/zookeeper/goodboy')
**** WatchedEvent(type='CHILD', state='CONNECTED', path='/zookeeper/goodboy')
'/zookeeper/goodboy/12364567777333'

不过上面的 方法是一次性调用的,适用于节点长期稳定不怎么变化的,对于节点经常变更的使用于下面的方法:

>>> '/zookeeper/goodboy/12364567777333'
'/zookeeper/goodboy/12364567777333'
>>> @zk.ChildrenWatch("/zookeeper/goodboy")
... def watch_children(children):
...     print("Children are now: %s" % children)
... 
Children are now: ['12364567777333']

这个是动态手机节点变化的,当我在这个节点下再创建一个node,它会立马返回/zookeeper/goodboy下面的节点列表:

>>> @zk.ChildrenWatch("/zookeeper/goodboy")Children are now: ['wwww12364567777', '12364567777333']

使用

from kazoo.client import KazooClient
zk = KazooClient(hosts=’127.0.0.1:2181′)
zk.start() //没有异常的话就意味着连接上zookeeper
children = zk.get_children(‘/’)
zk.stop()

获取节点信息

zk.get_children(“/zookeeper”)
[‘goodboy’, ‘goodboy123’, ‘goodboy1’, ‘goodboy12’, ‘quota’]
zk.get(‘/zookeeper/goodboy’)
(b”, ZnodeStat(czxid=14, mzxid=14, ctime=1505749749490, mtime=1505749749490, version=0, cversion=0, aversion=0, ephemeralOwner=0, dataLength=0, numChildren=0, pzxid=14))
zk.state
‘CONNECTED’

https://kazoo.readthedocs.io/en/latest/basic_usage.html

    原文作者:老夫刘某
    原文地址: https://www.jianshu.com/p/11199dd5487e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞