python 访问 zookeeper
zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。zookeeper提供可靠的存储,利用它可以保存当前工作的状态。当使用python在某个客户端服务器访问zookeeper集群,需要在客户端服务器上安装zookeeper(不是集群的zookeeper)c开发环境,不需要配置。
1) 下载zookeeper源码
$ wget http://archive.apache.org/dist/zookeeper/zookeeper-3.4.5/zookeeper-3.4.5.tar.gz
由于python客户端依赖c的客户端所以要先安装c版本的客户端
$ cd zookeeper-3.4.5/src/c $ ./configure $ make $ sudo make install
测试c客户端命令行与zk集群(hacl-node3.pepstack.com:2181是zk集群的其中一台服务)联通情况:
[root@vm-repo c]# ./cli_mt hacl-node3.pepstack.com:2181 Watcher SESSION_EVENT state = CONNECTED_STATE Got a new session id: 0x153eaa3fe840011 ls / time = 5 msec /: rc = 0 hadoop-ha hbase reg admin zookeeper consumers config storm yarn-leader-election brokers controller_epoch time = 6 msec
OK!
2) 下载zkpython
$ wget https://pypi.python.org/packages/source/z/zkpython/zkpython-0.4.2.tar.gz
解压安装:
$ tar -zxf zkpython-0.4.2.tar.gzcd zkpython-0.4.2 $ python2.7 setup.py build $ python2.7 setup.py install
3) 使用python访问zookeeper
$ python2.7 >>> import zookeeper
报错:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/zookeeper.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/zookeeper.py", line 6, in __bootstrap__
ImportError: libzookeeper_mt.so.2: cannot open shared object file: No such file or directory
解决方法任选一种:
(一) 将下面这句加入到 /etc/profile 最后一行(libzookeeper_mt.so在/usr/local/lib下面):
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
然后执行:
$ . /etc/profile
或者(二):
# echo "/usr/local/lib" >> /etc/ld.so.conf # /sbin/ldconfig
这样就加入到默认系统链接库啦!
再测试:
$ python2.7 >>> import zookeeper as zoo # 初始化连接到集群 zk = zoo.init("hacl-node3.pepstack.com:2181,hacl-node4.pepstack.com:2181,hacl-node5.pepstack.com:2181") # 获取所有节点 zoo.get_children(zk, "/", None)
完整的zkpython使用超出了本文的范畴, 参考:
Zookeeper原理与使用(必读):
http://blog.csdn.net/xinguan1267/article/details/38422149
zookeeper c api:
http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html
http://www.cnblogs.com/Xjng/p/3691421.html
http://www.cnblogs.com/lexus/archive/2012/03/11/2390203.html