http://www.cnblogs.com/lexus/archive/2012/03/11/2390203.html
一个简单的demo:
之后让我们来写一个简单的demo吧。(demo中用到的zkclient.py:https://github.com/piglei/zkpython_example/blob/master/zkclient.py)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# coding: utf-8
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:
log.info( "[ %s(%s) ] %s" % ( self .path, "master" if self .is_master else "slave" , msg))
else :
log.info(msg)
def main():
gj_zookeeper = GJZookeeper()
if __name__ = = "__main__" :
main()
import time
time.sleep( 1000 )
这个简单的demo所做的事情,就是通过在zookeeper的/app/workers节点下建立临时的子节点( flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE ),每次create完成之后检查自己是不是在最小的MASTERS_NUM(例子中为1,即单master)里。如果是的话,作为master运行,否则的话,作为slave运行。
这样的话,当我们的master挂掉以后,与zookeeper之间的连接也会中断,过了指定的TIMEOUT以后,master之前在worker下的子节点就会被删除,于是slave节点之前设置的watcher会被触发,再次检查自己是否为master,如果是的话则完成切换。
# 第一个实例
Connected in 20 ms, handle is 0
[2011-09-09 12:40:43,702] INFO login ok!
Node /app/workers/worker created in 4 ms
[2011-09-09 12:40:43,708] INFO [ worker0000000022(slave) ] register ok! I'm worker0000000022
[2011-09-09 12:40:43,709] INFO [ worker0000000022(slave) ] /app/workers's children: ['worker0000000022']
[2011-09-09 12:40:43,709] INFO [ worker0000000022(master) ] I've become master!
# 这时再起第二个实例
Connected in 64 ms, handle is 0
[2011-09-09 12:43:08,334] INFO login ok!
Node /app/workers/worker created in 11 ms
[2011-09-09 12:43:08,346] INFO [ worker0000000023(slave) ] register ok! I'm worker0000000023
[2011-09-09 12:43:08,347] INFO [ worker0000000023(slave) ] /app/workers's children: ['worker0000000022', 'worker0000000023']
[2011-09-09 12:43:08,347] INFO [ worker0000000023(slave) ] ['worker0000000022'] is masters, I'm slave
# 杀掉master,第二个实例发生的变化
[2011-09-09 12:44:06,016] INFO [ worker0000000023(slave) ] child changed, try to get master again.
[2011-09-09 12:44:06,017] INFO [ worker0000000023(slave) ] /app/workers's children: ['worker0000000023']
[2011-09-09 12:44:06,017] INFO [ worker0000000023(master) ] I've become master!
|