python实操

基础命令

pip isntall kazoo
服务器搭建zookeeper集群
python实操zookeeper1
python实操zookeeper2

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
import sys
from kazoo.client import KazooClient, KazooState
import logging

logging.basicConfig(
level=logging.DEBUG,
stream=sys.stdout,
format='%(asctime)s %(pathname)s %(funcName)s%(lineno)d %(levelname)s: %(message)s')

zk = KazooClient(
hosts='10.167.218.158:2181',
timeout=10.0,
logger=logging
)
# 开始心跳
zk.start()

# 获取根节点数据和状态
data, stat = zk.get('/')
print("data:", data)
print("stat:", stat)

# 获取子节点
children = zk.get_children("/")
print(children)

# 创建子节点
# zk.create('/sanguo/shuguo', b'caoChao', ephemeral=False)
if not zk.exists('/sanguo/weiguo'):
zk.create('/sanguo/weiguo', b'caoChao', ephemeral=False)


children = zk.get_children("/sanguo")
print(children)
data, stat = zk.get('/sanguo/weiguo')
print("data:", data)
print("stat:", stat)


def monitor(event):
print(type(event))
print(event)
print(zk.get_children('/sanguo'))


import time

children = zk.get_children('/sanguo', watch=monitor)
print(children)

node = zk.create('/b')
print(node, 'aaaaaaaaaaaaaaaaaaaaa')
print(zk.exists('/sanguo'), 'fafafafafaa')

zookeeper实现分布式锁

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
import time
import sys
from kazoo.client import KazooClient
import logging

class ZkLock:
def __init__(self, root='/Lock', node=None):
self.lock = False
self.root = root
self.node = node
logging.basicConfig(
level=logging.DEBUG,
stream=sys.stdout,
format='%(asctime)s %(pathname)s %(funcName)s%(lineno)d %(levelname)s: %(message)s')
self.zk = KazooClient(
hosts='10.167.218.158:2181',
timeout=10.0,
logger=logging
)
self.zk.start()

def acquire(self):
if not self.zk.exists(self.root):
self.zk.create(self.root)
if not self.node:
self.node = self.zk.create(self.root+'/lock', ephemeral=True, sequence=True)
nodes = self.zk.get_children(self.root)
if nodes[0] == self.node:
print('Acquire lock successfully')
self.lock = True
else:
print('Acquire lock failed start watching')

@self.zk.ChildrenWatch(self.root)
def watch_func(data):
print("Data is %s and node is %s and root.is %s" % (data, self.node, self.root))
print(self.node, self.root + '/' + data[-1], self.node == self.root + '/' + data[-1])
if self.node == self.root + '/' + data[-1]:
self.lock = True

def release(self):
self.zk.stop()


if __name__ == '__main__':
t = ZkLock()
while not t.lock:
t.acquire()
print('执行获取锁后的操作')
time.sleep(10)
print('执行操作结束释放锁')
t.release()
  • 优点:ZK可以不需要关心锁超时时间,实现起来有现成的第三方包,比较方便,并且支持读写锁,
    ZK获取锁会按照加锁的顺序,所以其是公平锁。对于高可用利用ZK集群进行保证。
  • 缺点:ZK需要额外维护,增加维护成本,性能和Mysql相差不大,依然比较差。并且需要开发人员了解ZK是什么。
分享到