上一篇已经讲解了如何安装zookeeper的python客户端,接下来是我在网上搜到的例子,举例应用环境是:

1.当有两个或者多个服务运行,并且同意时间只有一个服务接受请求(工作),其他服务待命。

2.当接受请求(工作)的服务异常挂掉时,会从剩下的待命服务中选举出一个服务来接受请求(工作)。

下面直接上例子,有两个文件组成1.zkclient.py   2.zktest.py

# coding: utf-8
# modfied from https://github.com/phunt/zk-smoketest/blob/master/zkclient.py
# zkclient.py import zookeeper, time, threading
from collections import namedtuple DEFAULT_TIMEOUT = 30000
VERBOSE = True ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} # Mapping of connection state values to human strings.
STATE_NAME_MAPPING = {
zookeeper.ASSOCIATING_STATE: "associating",
zookeeper.AUTH_FAILED_STATE: "auth-failed",
zookeeper.CONNECTED_STATE: "connected",
zookeeper.CONNECTING_STATE: "connecting",
zookeeper.EXPIRED_SESSION_STATE: "expired",
} # Mapping of event type to human string.
TYPE_NAME_MAPPING = {
zookeeper.NOTWATCHING_EVENT: "not-watching",
zookeeper.SESSION_EVENT: "session",
zookeeper.CREATED_EVENT: "created",
zookeeper.DELETED_EVENT: "deleted",
zookeeper.CHANGED_EVENT: "changed",
zookeeper.CHILD_EVENT: "child",
} class ZKClientError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value) class ClientEvent(namedtuple("ClientEvent", 'type, connection_state, path')):
"""
A client event is returned when a watch deferred fires. It denotes
some event on the zookeeper client that the watch was requested on.
""" @property
def type_name(self):
return TYPE_NAME_MAPPING[self.type] @property
def state_name(self):
return STATE_NAME_MAPPING[self.connection_state] def __repr__(self):
return "<ClientEvent %s at %r state: %s>" % (
self.type_name, self.path, self.state_name) def watchmethod(func):
def decorated(handle, atype, state, path):
event = ClientEvent(atype, state, path)
return func(event)
return decorated class ZKClient(object):
def __init__(self, servers, timeout=DEFAULT_TIMEOUT):
self.timeout = timeout
self.connected = False
self.conn_cv = threading.Condition( )
self.handle = -1 self.conn_cv.acquire()
if VERBOSE: print("Connecting to %s" % (servers))
start = time.time()
self.handle = zookeeper.init(servers, self.connection_watcher, timeout)
self.conn_cv.wait(timeout/1000)
self.conn_cv.release() if not self.connected:
raise ZKClientError("Unable to connect to %s" % (servers)) if VERBOSE:
print("Connected in %d ms, handle is %d"
% (int((time.time() - start) * 1000), self.handle)) def connection_watcher(self, h, type, state, path):
self.handle = h
self.conn_cv.acquire()
self.connected = True
self.conn_cv.notifyAll()
self.conn_cv.release() def close(self):
return zookeeper.close(self.handle) def create(self, path, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]):
start = time.time()
result = zookeeper.create(self.handle, path, data, acl, flags)
if VERBOSE:
print("Node %s created in %d ms"
% (path, int((time.time() - start) * 1000)))
return result def delete(self, path, version=-1):
start = time.time()
result = zookeeper.delete(self.handle, path, version)
if VERBOSE:
print("Node %s deleted in %d ms"
% (path, int((time.time() - start) * 1000)))
return result def get(self, path, watcher=None):
return zookeeper.get(self.handle, path, watcher) def exists(self, path, watcher=None):
return zookeeper.exists(self.handle, path, watcher) def set(self, path, data="", version=-1):
return zookeeper.set(self.handle, path, data, version) def set2(self, path, data="", version=-1):
return zookeeper.set2(self.handle, path, data, version) def get_children(self, path, watcher=None):
return zookeeper.get_children(self.handle, path, watcher) def async(self, path = "/"):
return zookeeper.async(self.handle, path) def acreate(self, path, callback, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]):
result = zookeeper.acreate(self.handle, path, data, acl, flags, callback)
return result def adelete(self, path, callback, version=-1):
return zookeeper.adelete(self.handle, path, version, callback) def aget(self, path, callback, watcher=None):
return zookeeper.aget(self.handle, path, watcher, callback) def aexists(self, path, callback, watcher=None):
return zookeeper.aexists(self.handle, path, watcher, callback) def aset(self, path, callback, data="", version=-1):
return zookeeper.aset(self.handle, path, data, version, callback) watch_count = 0 """Callable watcher that counts the number of notifications"""
class CountingWatcher(object):
def __init__(self):
self.count = 0
global watch_count
self.id = watch_count
watch_count += 1 def waitForExpected(self, count, maxwait):
"""Wait up to maxwait for the specified count,
return the count whether or not maxwait reached. Arguments:
- `count`: expected count
- `maxwait`: max milliseconds to wait
"""
waited = 0
while (waited < maxwait):
if self.count >= count:
return self.count
time.sleep(1.0);
waited += 1000
return self.count def __call__(self, handle, typ, state, path):
self.count += 1
if VERBOSE:
print("handle %d got watch for %s in watcher %d, count %d" %
(handle, path, self.id, self.count)) """Callable watcher that counts the number of notifications
and verifies that the paths are sequential"""
class SequentialCountingWatcher(CountingWatcher):
def __init__(self, child_path):
CountingWatcher.__init__(self)
self.child_path = child_path def __call__(self, handle, typ, state, path):
if not self.child_path(self.count) == path:
raise ZKClientError("handle %d invalid path order %s" % (handle, path))
CountingWatcher.__call__(self, handle, typ, state, path) class Callback(object):
def __init__(self):
self.cv = threading.Condition()
self.callback_flag = False
self.rc = -1 def callback(self, handle, rc, handler):
self.cv.acquire()
self.callback_flag = True
self.handle = handle
self.rc = rc
handler()
self.cv.notify()
self.cv.release() def waitForSuccess(self):
while not self.callback_flag:
self.cv.wait()
self.cv.release() if not self.callback_flag == True:
raise ZKClientError("asynchronous operation timed out on handle %d" %
(self.handle))
if not self.rc == zookeeper.OK:
raise ZKClientError(
"asynchronous operation failed on handle %d with rc %d" %
(self.handle, self.rc)) class GetCallback(Callback):
def __init__(self):
Callback.__init__(self) def __call__(self, handle, rc, value, stat):
def handler():
self.value = value
self.stat = stat
self.callback(handle, rc, handler) class SetCallback(Callback):
def __init__(self):
Callback.__init__(self) def __call__(self, handle, rc, stat):
def handler():
self.stat = stat
self.callback(handle, rc, handler) class ExistsCallback(SetCallback):
pass class CreateCallback(Callback):
def __init__(self):
Callback.__init__(self) def __call__(self, handle, rc, path):
def handler():
self.path = path
self.callback(handle, rc, handler) class DeleteCallback(Callback):
def __init__(self):
Callback.__init__(self) def __call__(self, handle, rc):
def handler():
pass
self.callback(handle, rc, handler)

上面的文件是别人封装好的zookeeper接口,下面是测试代码,需要依赖上面的包:

# coding: utf-8
# zktest.py import logging
from os.path import basename, join from zkclient import ZKClient, zookeeper, watchmethod logging.basicConfig(
level = logging.DEBUG,
format = "[%(asctime)s] %(levelname)-8s %(message)s"
) log = logging class GJZookeeper(object): ZK_HOST = "localhost:2181"
ROOT = "/app"
WORKERS_PATH = join(ROOT, "workers")
MASTERS_NUM = 1
TIMEOUT = 10000 def __init__(self, verbose = True):
self.VERBOSE = verbose
self.masters = []
self.is_master = False
self.path = None self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
self.say("login ok!")
# init
self.__init_zk()
# register
self.register() def __init_zk(self):
"""
create the zookeeper node if not exist
"""
nodes = (self.ROOT, self.WORKERS_PATH)
for node in nodes:
if not self.zk.exists(node):
try:
self.zk.create(node, "")
except:
pass @property
def is_slave(self):
return not self.is_master def register(self):
"""
register a node for this worker
"""
self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
self.path = basename(self.path)
self.say("register ok! I'm %s" % self.path)
# check who is the master
self.get_master() def get_master(self):
"""
get children, and check who is the smallest child
"""
@watchmethod
def watcher(event):
self.say("child changed, try to get master again.")
self.get_master() children = self.zk.get_children(self.WORKERS_PATH, watcher)
children.sort()
self.say("%s's children: %s" % (self.WORKERS_PATH, children)) # check if I'm master
self.masters = children[:self.MASTERS_NUM]
if self.path in self.masters:
self.is_master = True
self.say("I've become master!")
else:
self.say("%s is masters, I'm slave" % self.masters) def say(self, msg):
"""
print messages to screen
"""
if self.VERBOSE:
if self.path:
if self.is_master:
log.info("[ %s(%s) ] %s" % (self.path, "master" , msg))
else:
log.info("[ %s(%s) ] %s" % (self.path, "slave", msg))
else:
log.info(msg) def main():
gj_zookeeper = GJZookeeper() if __name__ == "__main__":
main()
import time
time.sleep(1000)

下面在两台机器(也可以在同一台)上运行上面的脚本可以看到如下信息:

[2013-07-03 14:26:12,192] INFO     login ok!
Node /app/workers/worker created in 1 ms
[2013-07-03 14:26:12,195] INFO [ worker0000000016(slave) ] register ok! I'm worker0000000016
[2013-07-03 14:26:12,196] INFO [ worker0000000016(slave) ] /app/workers's children: ['worker0000000016']
[2013-07-03 14:26:12,196] INFO [ worker0000000016(master) ] I've become master!

[2013-07-03 14:26:58,277] INFO     login ok!
Node /app/workers/worker created in 2 ms
[2013-07-03 14:26:58,281] INFO [ worker0000000017(slave) ] register ok! I'm worker0000000017
[2013-07-03 14:26:58,282] INFO [ worker0000000017(slave) ] /app/workers's children: ['worker0000000016', 'worker0000000017']
[2013-07-03 14:26:58,282] INFO [ worker0000000017(slave) ] ['worker0000000016'] is masters, I'm slave

先然第一台机器做了master。

下面我们关掉第一个进程,第二台在timeout(10s)时间后,会出现如下信息:

[2013-07-03 14:28:02,204] INFO     [ worker0000000017(slave) ] child changed, try to get master again.
[2013-07-03 14:28:02,205] INFO [ worker0000000017(slave) ] /app/workers's children: ['worker0000000017']
[2013-07-03 14:28:02,206] INFO [ worker0000000017(master) ] I've become master!

显然,第二台机器上的程序变成了master,这就是我们想要实现的功能。

当然开启多个进程也是可以的,并且可以在程序中选择master的数量,同时本人建议timeout时间可以取小一点,我们先上环境用的是2s。

之后会更新更多zookeeper的其他应用环境的例子。

zookeeper集群的python代码测试的更多相关文章

  1. Centos6下zookeeper集群部署记录

    ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. Zookeeper设计目的 最终一致性:client不论 ...

  2. k8s 上使用 StatefulSet 部署 zookeeper 集群

    目录 StatefulSet 部署 zookeeper 集群 创建pv StatefulSet 测试 StatefulSet 部署 zookeeper 集群 参考 k8s官网zookeeper集群的部 ...

  3. zookeeper与Kafka集群搭建及python代码测试

    Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我想对用户 ...

  4. kafka集群和zookeeper集群的部署,kafka的java代码示例

    来自:http://doc.okbase.net/QING____/archive/19447.html 也可参考: http://blog.csdn.net/21aspnet/article/det ...

  5. 消息中间件kafka+zookeeper集群部署、测试与应用

    业务系统中,通常会遇到这些场景:A系统向B系统主动推送一个处理请求:A系统向B系统发送一个业务处理请求,因为某些原因(断电.宕机..),B业务系统挂机了,A系统发起的请求处理失败:前端应用并发量过大, ...

  6. Zookeeper集群搭建以及python操作zk

    一.Zookeeper原理简介 ZooKeeper是一个开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等. Zookeeper设计目 ...

  7. 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例

    centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...

  8. ZooKeeper集群的安装、配置、高可用测试

    Dubbo注册中心集群Zookeeper-3.4.6 Dubbo建议使用Zookeeper作为服务的注册中心. Zookeeper集群中只要有过半的节点是正常的情况下,那么整个集群对外就是可用的.正是 ...

  9. Dubbo入门到精通学习笔记(十三):ZooKeeper集群的安装、配置、高可用测试、升级、迁移

    文章目录 ZooKeeper集群的安装.配置.高可用测试 ZooKeeper 与 Dubbo 服务集群架构图 1. 修改操作系统的/etc/hosts 文件,添加 IP 与主机名映射: 2. 下载或上 ...

随机推荐

  1. Erlang cowboy 处理不规范的client

    Erlang cowboy 处理不规范的client Cowboy 1.0 參考 本章: Dealing with broken clients 存在很多HTTP协议的实现版本号. 很多广泛使用的cl ...

  2. OC对象创建过程

    在利用OC开发应用程序中,须要大量创建对象,那么它的过程是什么呢? 比方:NSArray *array = [[NSArrayalloc] init]; 在说明之前,先把OC的Class描写叙述一下: ...

  3. oracle事务(转)

    今天温习oracle事务,记录如下: 事务定义            事务是保持数据的一致性,它由相关的DDL或者DML语句做为载体,这组语句执行的结果要么一起成功,要么一起失败.        我们 ...

  4. OCA读书笔记(13) - 性能管理

    使用EM监控性能使用自动内存管理(AMM)使用Memory Advisor分配内存查看性能相关动态视图诊断无效的和不可用的对象 创建问题SQLsqlplus / as sysdbaconn scott ...

  5. Codeforces Round #296 (Div. 1) E. Triangles 3000

    http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...

  6. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  7. 具体解释java定时任务

    在我们编程过程中假设须要运行一些简单的定时任务,无须做复杂的控制.我们能够考虑使用JDK中的Timer定时任务来实现. 以下LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器 ...

  8. Windows Server时间服务器配置方法

    1 时间服务器经常会碰到客户端机器需要和服务器在时间上保持同步,否则会出现各种问题,特别是有时间相关的触发功能的时候. 为解决各设备间时间统一的问题,我们可在网络中设置一台服务器使其作为基准时间,其它 ...

  9. 自己定义 ViewGroup 支持无限循环翻页之三(响应回调事件)

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处,再次感谢 ################################ ...

  10. Jersey的RESTful简单案例demo

    REST基础概念: 在REST中的一切都被认为是一种资源. 每个资源由URI标识. 使用统一的接口.处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作. ...