def __init__(self, name, register_watch_cb=None, register_node_cb=None, *args, **kwargs): self._name = name self.register_watch_cb = register_watch_cb self.register_node_cb = register_node_cb self._args = args self._kwargs = kwargs self.ip = utils.getip() self._handler = None self.connect(True) self.i = 0
def runserver(hostport=None): if hostport is None: hostport = (utils.getip(), 8821) utils.log("[服务端]正在监听%s:%d端口..." % hostport) #多线程socket连接 serv = socketserver.ThreadingTCPServer(hostport, FileServer) #保持服务监听线程持续 serv.serve_forever()
def runserver(host=None, port=None): #监听本地的IP和端口元组,多线程socket连接 if host is None: host = utils.getip() if port is None: port = 8820 hostport = (host, port) utils.log("[控制台]鉴权服务正在监听%s:%d端口..." % hostport) #多线程socket连接 auth = socketserver.ThreadingTCPServer(hostport, AuthServer) #保持服务监听线程持续 auth.serve_forever() return None
def register_service(self, service, port=0): path = "" if service == "master": path = ZKConf.ZK_PATH_TEMP_MASTER elif service == "stats": path = ZKConf.ZK_PATH_TEMP_STATS elif service == "proxy": path = ZKConf.ZK_PATH_TEMP_PROXY if not path: raise Exception("service type '%s' doesn't exist" % (service, )) node = zookeeper.exists(self._handler, path, None) if not node: if not zookeeper.exists(self._handler, ZKConf.ZK_PATH_ROOT, None): zookeeper.create(self._handler, ZKConf.ZK_PATH_ROOT, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) zookeeper.create(self._handler, path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) ip = utils.getip() mepath = "".join([path, "/", ip, ":", str(port), "-"]) if not port: mepath = "".join([path, "/", ip, "-"]) childs = zookeeper.get_children(self._handler, path, None) is_created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): is_created = child.split("-")[-1] break if is_created: meflag = mepath + is_created utils.log(utils.cur(), "%s none-callback %s" % ("*" * 10, "*" * 10), meflag) else: utils.log(utils.cur(), "%s %s %s" % ("*" * 10, self.register_node_cb, "*" * 10), mepath) meflag = zookeeper.create(self._handler, mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE | zookeeper.EPHEMERAL) utils.log(utils.cur(), "-" * 10, meflag)
def register_service(self, service, port=0): path = "" if service == "master": path = ZKConf.ZK_PATH_TEMP_MASTER elif service == "stats": path = ZKConf.ZK_PATH_TEMP_STATS elif service == "proxy": path = ZKConf.ZK_PATH_TEMP_PROXY if not path: raise Exception("service type '%s' doesn't exist" % (service,)) node = zookeeper.exists(self._handler, path, None) if not node: if not zookeeper.exists(self._handler, ZKConf.ZK_PATH_ROOT, None): zookeeper.create(self._handler, ZKConf.ZK_PATH_ROOT, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) zookeeper.create(self._handler, path, "", [ZKConf.ZOO_CREATOR_ALL_ACL], 0) ip = utils.getip() mepath = "".join([path, "/", ip, ":", str(port), "-"]) if not port: mepath = "".join([path, "/", ip, "-"]) childs = zookeeper.get_children(self._handler, path, None) is_created = False pathme = mepath.split("/")[-1] for child in childs: if child.startswith(pathme): is_created = child.split("-")[-1] break if is_created: meflag = mepath + is_created utils.log(utils.cur(), "%s none-callback %s" % ("*"*10, "*"*10), meflag) else: utils.log(utils.cur(), "%s %s %s" % ("*"*10, self.register_node_cb, "*"*10), mepath) meflag = zookeeper.create(self._handler, mepath, "", [ZKConf.ZOO_CREATOR_ALL_ACL], zookeeper.SEQUENCE|zookeeper.EPHEMERAL) utils.log(utils.cur(), "-"*10, meflag)