标签:des style blog http io color os ar for
问题导读:
1.如何利用zookeeper保证集群Master可用性和唯一性?
2.zookeeper竞选Master包含哪些过程?
3.zookeeper竞选Master机制利用了zk哪些特性?
在zookeeper应用场景中提出了对于Master节点管理的问题,如何保证集群中Master可用性和唯一性,下面就利用zookeeper来实现。
在确保zookeeper集群节点安装配置的前提下,假设zk已经对外提供了正常的服务,通过下面的步骤来实现Master竞选
上面的机制主要利用了zk的几个特性
下图是上面的逻辑图
<ignore_js_op>
下面是实现的代码,默认是连接本地的zk服务,端口是2181,zkclient模块位于zookeeper python接口只需要运行多个下面的脚本就会能实现Master的竞选。
PS:上面步骤3里面一个客户端关闭后经历了一段时间znode才会删除,原因是这段时间内zk的session还没有被清除,因为关闭是通过ctrl+c关闭的。但是加了一个客户端,znode里面创建,就会通知其余注册了watcher的客户端
#!/usr/bin/env python2.7 # -*- 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 = "/Roles" 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 |-Roles |-workers """ 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,znode type : EPHEMERAL | SEQUENCE |-Roles |-workers |-worker000000000x ==>>master |-worker000000000x+1 ==>>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() try : children = self.zk.get_children(self.WORKERS_PATH, watcher) except zookeeper.ConnectionLossException: print "losing connection with zookeeper..." return False except : return False 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)
文章转自:http://www.aboutyun.com/forum.php?mod=viewthread&tid=9277&ctid=16
标签:des style blog http io color os ar for
原文地址:http://www.cnblogs.com/likehua/p/4060301.html