簡介
zookeeper為分布式系統調度框架, 用于解決分布式應用中數據管理問題,比如同步鎖,分布式應用配置管理等
安裝&配置
-
修改配置
在conf目錄將zoo_sample.cfg拷貝為zoo.cfg, 該文件為zookeeper啟動的默認配置文件
zoo.cfg文件內容:tickTime=2000 #zookeeper服務器或客戶端與服務器之間發送心跳的時間間隔(2s) initLimit=10 #zookeeper集群中連接到leader的fllower的時間不能超過多少個tickTime時間長度,若超過則連接失敗 syncLimit=5 #zookeeper集群中leader和fllower之間發送消息,請求和響應的時間不能超過多少個tickTime時間長度 dataDir=/tmp/zookeeper #zookeeper數據存放位置 clientPort=2181 #zookeeper客戶端連接的端口號 #maxClientCnxns=60 #最大連接數量 #autopurge.snapRetainCount=3 #autopurge.purgeInterval=1 server.A=ipA:portA1:portA2 #表示集群中服務器地址, A:服務器標識, ipA:服務器ip地址, portA1:該服務器與leader通信的端口號, portA2: 當leader掛掉,用來選舉使用的端口號 server.B=ipB:portB1:portB2
在單機部署時至需要配置tickTime,dataDir,clientPort配置即可
在集群部署時需要再設置initLimit,syncLimit,server.*等配置, 同時需要在dataDir目錄下新建myid文件, 內容為服務器的標識
啟動服務器
cd ./bin && start zkServer.cmd
啟動客戶端
cd ./bin && start zkCli.cmd -server localhost:2181
集群部署
1.將zookeeper文件夾拷貝多份,修改zoo.cfg中的clientPort和dataDir
2.修改dataDir目錄下myid文件內容為該服務器的標識
3.依次啟動zookeeper服務器
實例
使用python實現分布式共享鎖
#!/usr/bin/env python
#encoding: utf-8
import logging
import threading
import time
from kazoo.client import KazooClient
_logger = logging.getLogger('zlock')
class ZKClient(KazooClient):
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(ZKClient, cls).__new__(cls)
return cls._instance
def __init__(self, *args, **kwargs):
super(ZKClient, self).__init__(*args, **kwargs)
self.start()
class Zlock(object):
def __init__(self):
self._zkclient = ZKClient(hosts='localhost:2281')
self._lock_nameservice = '/uk/lock'
self._lock_prefix = 'lock_id_'
self._lock_id = ''
self._event = threading.Event()
def acquire(self, wait=True):
if self._lock_id == '':
self._lock_id = self._zkclient.create('%s/%s' % (self._lock_nameservice, self._lock_prefix), str(time.time()), ephemeral=True, sequence=True, makepath=True)
if not self.locked():
return True
elif wait:
self._event.wait()
else:
return False
def _watch(self, *args, **kwargs):
if not self.locked():
self._event.set()
def release(self):
self._zkclient.exists(self._lock_id) and self._zkclient.delete(self._lock_id)
self._lock_id = ''
self._event.clear()
def locked(self):
if self._zkclient.exists(self._lock_nameservice):
_children = self._zkclient.get_children(self._lock_nameservice, self._watch)
if self._lock_id != '':
_children.sort()
return '%s/%s' % (self._lock_nameservice, _children[0]) != self._lock_id
else:
return len(_children) != 0
else:
return False
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)s %(levelname)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
_lock = Zlock()
for i in xrange(10):
print _lock.acquire(True)
_lock.release()
pass