def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog)) #日志处理 log.startLogging(sys.stdout) import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root))
def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log_init(masterlog) # 日志处理 import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root))
def masterapp(self): """ """ config = self.__loadconfig() GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog))#日志处理 log.startLogging(sys.stdout) import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root))
#coding:utf8 ''' Created on 2011-10-17 @author: lan (www.9miao.com) ''' from gfirefly.utils import services from gfirefly.distributed.root import PBRoot,BilateralFactory from gtwisted.core import reactor from gtwisted.utils import log import sys reactor = reactor log.startLogging(sys.stdout) root = PBRoot() ser = services.Service('test') root.addServiceChannel(ser) def serviceHandle(target): '''服务处理 @param target: func Object ''' ser.mapTarget(target) @serviceHandle def printData1(data,data1): print data,data1 print "############################" d = root.callChild("test_node",1,u'Root测试') return data
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config GlobalObject().remote_connect = self.remote_connect 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 netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = Flask("servername") GlobalObject().webroot = self.webroot reactor.listenWSGI(webport, 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: if dbconfig.has_key("user") and dbconfig.has_key( "host") and dbconfig.has_key("host"): dbpool.initPool({"default": dbconfig}) else: dbpool.initPool(dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') # hostname = str(memconfig.get('hostname')) memcached_connect(urls) from gfirefly.dbentrust.util import M2DB_PORT, M2DB_HOST, ToDBAddress ToDBAddress().setToDBHost(memconfig.get("pubhost", M2DB_HOST)) ToDBAddress().setToDBPort(memconfig.get("pubport", M2DB_PORT)) 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]) if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) GlobalObject().masterremote = self.master_remote import admin addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr)
class Master: def __init__(self): self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self, configpath, mainpath): """ """ self.configpath = configpath self.mainpath = mainpath def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log_init(masterlog) # 日志处理 import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): '''baidu ''' sys_args = sys.argv if len(sys_args) > 2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = json.load(open(self.configpath, 'r')) sersconf = config.get('servers') for sername in sersconf.keys(): # cmds = "kernprof -o app/logs/%s.lprof -l -b %s %s %s " % (sername, self.mainpath, sername, self.configpath) cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) # cmds = 'python -m profile %s %s %s' % (self.mainpath, sername, self.configpath) # print 'mutiple', cmds subprocess.Popen(cmds, shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: self.masterapp() # config = json.load(open(self.configpath, 'r')) sername = server_name cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) # print 'single', cmds subprocess.Popen(cmds, shell=True) reactor.run() else: self.masterapp() reactor.run()
#coding:utf8 ''' Created on 2011-10-17 @author: lan (www.9miao.com) ''' from gfirefly.utils import services from gfirefly.distributed.root import PBRoot, BilateralFactory from gtwisted.core import reactor from gtwisted.utils import log import sys reactor = reactor log.startLogging(sys.stdout) root = PBRoot() ser = services.Service('test') root.addServiceChannel(ser) def serviceHandle(target): '''服务处理 @param target: func Object ''' ser.mapTarget(target) @serviceHandle def printData1(data, data1): print data, data1 print "############################" d = root.callChild("test_node", 1, u'Root测试')
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 = [] def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config GlobalObject().remote_connect = self.remote_connect #websocket port wsport = config.get('wsport') #ws 端口 wsurls = config.get('wsurls') #ws 端口 #websocket port 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 netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if wsport: #是否需要支持wss keyfile = config.get('keyfile', None) certfile = config.get('certfile', None) if keyfile: wsapp = MyWebSocketServer(wsport, wsurls, MyApplication, keyfile=keyfile, certfile=certfile) else: wsapp = MyWebSocketServer(wsport, wsurls, MyApplication) wsapp.addServiceChan(services.CommandService("netservice")) GlobalObject().wsapp = wsapp wsapp.start() if webport: self.webroot = Flask("servername") GlobalObject().webroot = self.webroot reactor.listenWSGI(webport, 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: if dbconfig.has_key("user") and dbconfig.has_key( "host") and dbconfig.has_key("host"): dbpool.initPool({"default": dbconfig}) else: dbpool.initPool(dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') # hostname = str(memconfig.get('hostname')) memcached_connect(urls) from gfirefly.dbentrust.util import M2DB_PORT, M2DB_HOST, ToDBAddress ToDBAddress().setToDBHost(memconfig.get("pubhost", M2DB_HOST)) ToDBAddress().setToDBPort(memconfig.get("pubport", M2DB_PORT)) 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]) if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) GlobalObject().masterremote = self.master_remote from gfirefly.server import admin addr = ('localhost', masterport) if not masterhost else (masterhost, masterport) self.master_remote.connect(addr) def remote_connect(self, rname, rhost): """进行rpc的连接 """ 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] started...' % self.servername) log.msg('[%s] pid: %s' % (self.servername, os.getpid())) reactor.run()
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self,configpath,mainpath): """ """ self.configpath = configpath self.mainpath = mainpath def masterapp(self): """ """ config = self.__loadconfig() GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog))#日志处理 log.startLogging(sys.stdout) import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' sys_args = sys.argv if len(sys_args)>2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = self.__loadconfig() sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: config = self.__loadconfig() sername = server_name cmds = 'python %s %s %s'%(self.mainpath,sername,self.configpath) subprocess.Popen(cmds,shell=True) else: self.masterapp() reactor.run() def __loadconfig(self): return __import__(self.configpath).config_dict
def config(self, config, servername=None, dbconfig=None, memconfig=None, masterconf=None): '''配置服务器 ''' GlobalObject().json_config = config GlobalObject().remote_connect = self.remote_connect 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 netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport,self.netfactory) if webport: self.webroot = Flask("servername") GlobalObject().webroot = self.webroot reactor.listenWSGI(webport, 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: if dbconfig.has_key("user") and dbconfig.has_key("host") and dbconfig.has_key("host"): dbpool.initPool({"default":dbconfig}) else: dbpool.initPool(dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') # hostname = str(memconfig.get('hostname')) memcached_connect(urls) from gfirefly.dbentrust.util import M2DB_PORT,M2DB_HOST,ToDBAddress ToDBAddress().setToDBHost(memconfig.get("pubhost",M2DB_HOST)) ToDBAddress().setToDBPort(memconfig.get("pubport",M2DB_PORT)) 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]) if masterconf: masterport = masterconf.get('rootport') masterhost = masterconf.get('roothost') self.master_remote = RemoteObject(servername) GlobalObject().masterremote = self.master_remote import admin addr = ('localhost',masterport) if not masterhost else (masterhost,masterport) self.master_remote.connect(addr)
class Master: """ """ def __init__(self): """ """ self.configpath = None self.mainpath = None self.root = None self.webroot = None def config(self, configpath, mainpath): """ """ self.configpath = configpath self.mainpath = mainpath def masterapp(self): """ """ config = json.load(open(self.configpath, 'r')) GlobalObject().json_config = config mastercnf = config.get('master') rootport = mastercnf.get('rootport') webport = mastercnf.get('webport') masterlog = mastercnf.get('log') self.root = PBRoot() rootservice = services.Service("rootservice") self.root.addServiceChannel(rootservice) self.webroot = Flask("master") GlobalObject().root = self.root GlobalObject().webroot = self.webroot if masterlog: log.addObserver(loogoo(masterlog)) #日志处理 log.startLogging(sys.stdout) import webapp import rootapp reactor.listenWSGI(webport, self.webroot) reactor.listenTCP(rootport, BilateralFactory(self.root)) def start(self): ''' ''' sys_args = sys.argv if len(sys_args) > 2 and sys_args[1] == "single": server_name = sys_args[2] if server_name == "master": mode = MASTER_SERVER_MODE else: mode = SINGLE_SERVER_MODE else: mode = MULTI_SERVER_MODE server_name = "" if mode == MULTI_SERVER_MODE: self.masterapp() config = json.load(open(self.configpath, 'r')) sersconf = config.get('servers') for sername in sersconf.keys(): cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) reactor.run() elif mode == SINGLE_SERVER_MODE: config = json.load(open(self.configpath, 'r')) sername = server_name cmds = 'python %s %s %s' % (self.mainpath, sername, self.configpath) subprocess.Popen(cmds, shell=True) else: self.masterapp() reactor.run()
def config(self, config, servername=None, dbconfig=None, memconfig=None, redis_config=None, masterconf=None, model_default_config=None, model_config=None): """配置服务器""" GlobalObject().json_config = config GlobalObject().json_model_config = model_default_config GlobalObject().json_model_config = model_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连接 hasredis = config.get('redis') # redis连接 app = config.get('app') # 入口模块名称 cpuid = config.get('cpu') # 绑定cpu mreload = config.get('reload') # 重新加载模块名称 self.servername = servername if logpath: log_init(logpath) # 日志处理 if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = Flask("master") GlobalObject().webroot = self.webroot self.webroot.debug = True # reactor.listenTCP(webport, self.webroot) reactor.listenWSGI(webport, 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: # logger.info(str(dbconfig)) dbpool.initPool(**dbconfig) if hasmem and memconfig: urls = memconfig.get('urls') hostname = str(memconfig.get('hostname')) mclient.connect(urls, hostname) if hasredis and redis_config: host = redis_config.get("host") port = redis_config.get("port") db = redis_config.get("db", 0) redis_client.connect(host, port, db) 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
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 = [] 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 = Flask("master") GlobalObject().webroot = self.webroot reactor.listenTCP(webport, 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 import admin def remote_connect(self, rname, rhost): """进行rpc的连接 """ 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()
def config(self, config, servername=None, dbconfig=None, memconfig=None, redis_config=None, masterconf=None, model_default_config=None, model_config=None, msdk_config=None): """配置服务器""" GlobalObject().json_config = config GlobalObject().json_model_config = model_default_config GlobalObject().json_model_config = model_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连接 hasredis = config.get('redis') # redis连接 app = config.get('app') # 入口模块名称 cpuid = config.get('cpu') # 绑定cpu mreload = config.get('reload') # 重新加载模块名称 self.servername = servername if servername == 'net': time.sleep(6) if logpath: log_init(logpath) # 日志处理 if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = Flask("master") GlobalObject().webroot = self.webroot self.webroot.debug = True # reactor.listenTCP(webport, self.webroot) reactor.listenWSGI(webport, 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: # logger.info(str(dbconfig)) dbpool.initPool(**dbconfig) if hasredis and redis_config: connection_setting = redis_config.get('urls') redis_manager.connection_setup(connection_setting) 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 if msdk_config: #zone_id = msdk_config.get("zone_id") host = msdk_config.get("host") #pay_host = msdk_config.get("pay_host") goods_host = msdk_config.get("buy_goods_host") valid_host = msdk_config.get("valid_host") qq_appid = msdk_config.get("qq_appid") qq_appkey = msdk_config.get("qq_appkey") wx_appid = msdk_config.get("wx_appid") wx_appkey = msdk_config.get("wx_appkey") log = logger_sdk.new_log('TxApi') GlobalObject().msdk = Msdk(host, qq_appid, qq_appkey, wx_appid, wx_appkey, log=log) GlobalObject().pay = MidasApi(host, goods_host, valid_host, log=log) 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 = [] 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 netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport,self.netfactory) if webport: self.webroot = Flask("servername") GlobalObject().webroot = self.webroot reactor.listenWSGI(webport, 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 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 if app: __import__(app) if mreload: _path_list = mreload.split(".") GlobalObject().reloadmodule = __import__(mreload,fromlist=_path_list[:1]) GlobalObject().remote_connect = self.remote_connect def remote_connect(self, rname, rhost): """进行rpc的连接 """ 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] started...'%self.servername) log.msg('[%s] pid: %s'%(self.servername,os.getpid())) reactor.run()
def config(self, config, servername=None, dbconfig=None, memconfig=None, redis_config=None, masterconf=None, model_default_config=None, model_config=None, msdk_config=None): """配置服务器""" GlobalObject().json_config = config GlobalObject().json_model_config = model_default_config GlobalObject().json_model_config = model_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连接 hasredis = config.get('redis') # redis连接 app = config.get('app') # 入口模块名称 cpuid = config.get('cpu') # 绑定cpu mreload = config.get('reload') # 重新加载模块名称 self.servername = servername #if servername == 'net': #time.sleep(6) #if servername == 'gate': #time.sleep(16) if logpath: log_init(logpath) # 日志处理 if netport: self.netfactory = LiberateFactory() netservice = services.CommandService("netservice") self.netfactory.addServiceChannel(netservice) reactor.listenTCP(netport, self.netfactory) if webport: self.webroot = Flask("master") GlobalObject().webroot = self.webroot self.webroot.debug = True # reactor.listenTCP(webport, self.webroot) reactor.listenWSGI(webport, 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: # logger.info(str(dbconfig)) dbpool.initPool(**dbconfig) if hasredis and redis_config: connection_setting = redis_config.get('urls') redis_manager.connection_setup(connection_setting) 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 if msdk_config: #zone_id = msdk_config.get("zone_id") host = msdk_config.get("host") #pay_host = msdk_config.get("pay_host") goods_host = msdk_config.get("buy_goods_host") valid_host = msdk_config.get("valid_host") qq_appid = msdk_config.get("qq_appid") qq_appkey = msdk_config.get("qq_appkey") wx_appid = msdk_config.get("wx_appid") wx_appkey = msdk_config.get("wx_appkey") log = logger_sdk.new_log('TxApi') GlobalObject().msdk = Msdk(host, qq_appid, qq_appkey, wx_appid, wx_appkey, log=log) GlobalObject().pay = MidasApi(host, goods_host, valid_host, log=log) import admin