def set_config(self): """ 初始化节点服务配置 :return: """ config = Config().config ser_cfg = config.get("servers", {}).get(self.servername) if not ser_cfg: raise ValueError mem_cfg = config.get("cache") master_cfg = config.get("master") db_cfg = config.get("db") GlobalObject().json_config = ser_cfg netport = ser_cfg.get('netport') # 客户端连接 webport = ser_cfg.get('webport') # http连接 rootport = ser_cfg.get('rootport') # root节点配置 wsport = ser_cfg.get("wsport") # WebSocket端口 self.remoteportlist = ser_cfg.get('remoteport', []) # remote节点配置列表 logpath = ser_cfg.get('log') # 日志 hasdb = ser_cfg.get('db') # 数据库连接 hasmem = ser_cfg.get('mem') # memcached连接 app = ser_cfg.get('app') # 入口模块名称 cpuid = ser_cfg.get('cpu') # 绑定cpu mreload = ser_cfg.get('reload') # 重新加载模块名称 if master_cfg: masterport = master_cfg.get('rootport') masterhost = master_cfg.get('roothost') self.master_remote = RemoteObject(self.servername) addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) if wsport: self.ws = WsFactory(wsport) wsservice = services.CommandService("wsservice") self.ws.addServiceChannel(wsservice) reactor.listenTCP(wsport, self.ws) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername) if hasdb and db_cfg: log.msg(str(db_cfg)) dbpool.initPool(**db_cfg) if hasmem and mem_cfg: urls = mem_cfg.get('urls') hostname = str(mem_cfg.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) GlobalObject().server = self if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import admin
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config netport = config.get('netport') #客户端连接 webport = config.get('webport') #http连接 rootport = config.get('rootport') #root节点配置 self.remoteportlist = config.get('remoteport', []) #remote节点配置列表 if not servername: servername = config.get('name') #服务器名称 logpath = config.get('log') #日志 hasdb = config.get('db') #数据库连接 hasmem = config.get('mem') #memcached连接 app = config.get('app') #入口模块名称 cpuid = config.get('cpu') #绑定cpu mreload = config.get('reload') #重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername, "master") addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername, rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import admin
class FFServer: def __init__(self): ''' ''' self.netfactory = None #net前端 self.root = None #分布式root节点 self.remote = {} #remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None def config(self, config, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' netport = config.get('netport') #客户端连接 rootport = config.get('rootport') #root节点配置 remoteportlist = config.get('remoteport', []) #remote节点配置列表 servername = config.get('name') #服务器名称 logpath = config.get('log') #日志 hasdb = config.get('db') #数据库连接 hasmem = config.get('mem') #memcached连接 app = config.get('app') #入口模块名称 cpuid = config.get('cpu') #绑定cpu mreload = config.get('reload') #重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get('rootport') self.master_remote = RemoteObject(servername) addr = ('localhost', masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in remoteportlist: rname = cnf.get('rootname') rport = cnf.get('rootport') self.remote[rname] = RemoteObject(servername) addr = ('localhost', rport) self.remote[rname].connect(addr) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: GlobalObject().reloadmodule = __import__(mreload) import admin def start(self): '''启动服务器 ''' log.msg('%s start...' % self.servername) log.msg('%s pid: %s' % (self.servername, os.getpid())) reactor.run()
class FFServer: def __init__(self): self.netfactory = None # net前端 self.root = None # 分布式root节点 self.webroot = None # http服务 self.ws = None self.remote = {} # remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None self.remoteportlist = [] def set_name(self, name): """ 设置当前节点服务器名称 :param name: :return: """ self.servername = name def set_config(self): """ 初始化节点服务配置 :return: """ config = Config().config ser_cfg = config.get("servers", {}).get(self.servername) if not ser_cfg: raise ValueError mem_cfg = config.get("cache") master_cfg = config.get("master") db_cfg = config.get("db") GlobalObject().json_config = ser_cfg netport = ser_cfg.get('netport') # 客户端连接 webport = ser_cfg.get('webport') # http连接 rootport = ser_cfg.get('rootport') # root节点配置 wsport = ser_cfg.get("wsport") # WebSocket端口 self.remoteportlist = ser_cfg.get('remoteport', []) # remote节点配置列表 logpath = ser_cfg.get('log') # 日志 hasdb = ser_cfg.get('db') # 数据库连接 hasmem = ser_cfg.get('mem') # memcached连接 app = ser_cfg.get('app') # 入口模块名称 cpuid = ser_cfg.get('cpu') # 绑定cpu mreload = ser_cfg.get('reload') # 重新加载模块名称 if master_cfg: masterport = master_cfg.get('rootport') masterhost = master_cfg.get('roothost') self.master_remote = RemoteObject(self.servername) addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) if wsport: self.ws = WsFactory(wsport) wsservice = services.CommandService("wsservice") self.ws.addServiceChannel(wsservice) reactor.listenTCP(wsport, self.ws) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername) if hasdb and db_cfg: log.msg(str(db_cfg)) dbpool.initPool(**db_cfg) if hasmem and mem_cfg: urls = mem_cfg.get('urls') hostname = str(mem_cfg.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath)) #日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) GlobalObject().server = self if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import admin def remote_connect(self, rname, rhost): """ 连接远程RRoot节点 :param rname: :param rhost: :return: """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost', rport) else: addr = (rhost, rport) self.remote[rname].connect(addr) break def startAfter(self): """ 启动之后 :return: """ log.msg('%s has started, the pid is %s ...' % (self.servername, os.getpid())) def stopBefore(self): """ 停止之前 :return: """ log.msg('[%s] server is stopped ...' % self.servername) if GlobalObject().stophandler: GlobalObject().stophandler() signal.alarm(1) def start(self): """ 启动服务器 :return: """ reactor.addSystemEventTrigger('after', 'startup', self.startAfter) reactor.addSystemEventTrigger('before', 'shutdown', self.stopBefore) reactor.run()
Created on 2011-10-17 @author: lan (www.9miao.com) ''' from firefly.utils import services from firefly.distributed.node import RemoteObject from twisted.internet import reactor from twisted.python import util, log import sys log.startLogging(sys.stdout) reactor = reactor addr = ('localhost', 10001) #目标主机的地址 remote = RemoteObject('test_node') #实例化远程调用对象 service = services.Service('reference', services.Service.SINGLE_STYLE) #实例化一条服务对象 remote.addServiceChannel(service) def serviceHandle(target): '''服务处理 @param target: func Object ''' service.mapTarget(target) @serviceHandle def printOK(data):
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = None#remote节点 self.master_remote = None self.db = None self.servername = None self.remoteportlist = [] def config(self, server_id, version, config, servername=None, dbconfig=None, cacheconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().server_id = server_id GlobalObject().version = version GlobalObject().json_config = config netport = config.get('netport')#客户端连接 webport = config.get('webport')#http连接 rootport = config.get('rootport')#root节点配置 self.remoteportlist = config.get('remoteport',[])#remote节点配置列表 if not servername: servername = config.get('name')#服务器名称 logpath = config.get('log')#日志 hasdb = config.get('db')#数据库连接 app = config.get('app')#入口模块名称 cpuid = config.get('cpu')#绑定cpu mreload = config.get('reload')#重新加载模块名称 self.servername = servername self.remote = RemoteManager(self.servername) if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport,self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote.addRemote(rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if cacheconfig: GlobalObject().cache_config = cacheconfig if logpath: log.addObserver(loogoo(logpath))#日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory = self.netfactory, root=self.root, remote = self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload,fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote import admin def remote_connect(self, rname, rhost): """ """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost',rport) else: addr = (rhost,rport) self.remote.connectRemote(rname, addr) break def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): """配置服务器 """ GlobalObject().json_config = config netport = config.get("netport") # 客户端连接 webport = config.get("webport") # http连接 rootport = config.get("rootport") # root节点配置 self.remoteportlist = config.get("remoteport", []) # remote节点配置列表 if not servername: servername = config.get("name") # 服务器名称 logpath = config.get("log") # 日志 hasdb = config.get("db") # 数据库连接 hasmem = config.get("mem") # memcached连接 app = config.get("app") # 入口模块名称 cpuid = config.get("cpu") # 绑定cpu mreload = config.get("reload") # 重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get("rootport") masterhost = masterconf.get("roothost") self.master_remote = RemoteObject(servername, "master") addr = ("localhost", masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get("rootname") self.remote[rname] = RemoteObject(self.servername, rname) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if logpath: log.addObserver(loogoo(logpath)) # 日志处理 log.startLogging(sys.stdout) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect from . import admin
class FFServer: def __init__(self): ''' ''' self.netfactory = None#net前端 self.root = None#分布式root节点 self.webroot = None#http服务 self.remote = {}#remote节点 self.master_remote = None self.db = None self.mem = None self.servername = None self.remoteportlist = [] self.isssl = False; self.sslkey = ''; self.sslcrt = ''; self.sslcontext = None; def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config netport = config.get('netport')#客户端连接 webport = config.get('webport')#http连接 rootport = config.get('rootport')#root节点配置 self.remoteportlist = config.get('remoteport',[])#remote节点配置列表 if not servername: servername = config.get('name')#服务器名称 logpath = config.get('log')#日志 hasdb = config.get('db')#数据库连接 hasmem = config.get('mem')#memcached连接 app = config.get('app')#入口模块名称 cpuid = config.get('cpu')#绑定cpu mreload = config.get('reload')#重新加载模块名称 self.isssl = config.get('ssl'); self.sslkey = config.get('sslkey'); self.sslcrt = config.get('sslcrt'); if self.isssl: self.sslcontext = ssl.DefaultOpenSSLContextFactory(self.sslkey,self.sslcrt); self.servername = servername if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport,self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot if self.isssl: reactor.listenSSL(webport, DelaySite(self.webroot), self.sslcontext) else: reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername) if hasdb and dbconfig: log.msg(str(dbconfig)) dbpool.initPool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) if logpath: log.addObserver(loogoo(logpath))#日志处理 log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory = self.netfactory, root=self.root, remote = self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload,fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote import admin def remote_connect(self, rname, rhost): """ """ for cnf in self.remoteportlist: _rname = cnf.get('rootname') if rname == _rname: rport = cnf.get('rootport') if not rhost: addr = ('localhost',rport) else: addr = (rhost,rport) self.remote[rname].connect(addr) break def start(self): '''启动服务器 ''' log.msg('%s start...'%self.servername) log.msg('%s pid: %s'%(self.servername,os.getpid())) reactor.run()
class FFGServer(FFServer): def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): GlobalObject().json_config = config netport = config.get('netport') # 客户端连接 webport = config.get('webport') # http连接 rootport = config.get('rootport') # root节点配置 self.remoteportlist = config.get('remoteport', []) # remote节点配置列表 if not servername: servername = config.get('name') # 服务器名称 logpath = config.get('log') # 日志 hasdb = config.get('db') # 数据库连接 hasmem = config.get('mem') # memcached连接 app = config.get('app') # 入口模块名称 cpuid = config.get('cpu') # 绑定cpu mreload = config.get('reload') # 重新加载模块名称 self.servername = servername if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) GlobalObject().masterremote = self.master_remote if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = vhost.NameVirtualHost() GlobalObject().webroot = self.webroot reactor.listenTCP(webport, DelaySite(self.webroot)) if rootport: self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) reactor.listenTCP(rootport, BilateralFactory(self.root)) for cnf in self.remoteportlist: rname = cnf.get('rootname') self.remote[rname] = RemoteObject(self.servername) if hasdb and dbconfig: log.msg(str(dbconfig)) db_pool.init_db_pool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) log_filename = "%s%s" % (self.servername, ".log") log.startLogging(DailyLogFile(log_filename, 'logs/')) #log.startLogging(DailyLogFile('master.log', 'logs/')) # log.startLogging(sys.stdout) if cpuid: affinity.set_process_affinity_mask(os.getpid(), cpuid) GlobalObject().config(netfactory=self.netfactory, root=self.root, remote=self.remote) if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload, fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect import firefly.server.admin def start_after(self): func.log_info('{} has started, the pid is {}.'.format( self.servername, os.getpid())) def stop_before(self): func.log_info('{} is stoped.'.format(self.servername)) if GlobalObject().stophandler: GlobalObject().stophandler() signal.alarm(1) def start(self): reactor.addSystemEventTrigger('after', 'startup', self.start_after) reactor.addSystemEventTrigger('before', 'shutdown', self.stop_before) GlobalObject().server = self FFServer.start(self)