def thrift(self): from boarpc.protocol.tgunicorn.thrift.thriftapp import ThriftApplication from boarpc.protocol.thrift import BoarpcService from boarpc.protocol.dispatcherHandler import DispatcherHandler from common.utils.netUtils import NetUtils from common.config.conf import config import multiprocessing handler = DispatcherHandler() app = BoarpcService.Processor(handler) bind = "%s:%s" % (NetUtils.getIp(), str(NetUtils.getFreePort())) options = { 'worker_class': 'thrift_gevent', 'thrift_protocol_factory': 'thrift.protocol.TCompactProtocol:TCompactProtocolAcceleratedFactory', 'thrift_transport_factory': 'thrift.transport.TTransport:TBufferedTransportFactory', 'service_register_cls': 'boarpc.protocol.tgunicorn.zk_process:ZkRigster', 'bind': bind, 'workers': int(config.getConf('rpc', 'processes', multiprocessing.cpu_count())), 'on_exit': GunicornProtocol.on_exit, 'daemon': config.getConf('profiles', 'active', 'dev') == 'prod', 'reuse-port': True, } return ThriftApplication(app, options)
def startWorker(self): processes = int( config.getConf('rpc', 'processes', multiprocessing.cpu_count())) urlSet = set() portSet = set() # get all endpoints endpointSet = self.getAllEndpoints() host = NetUtils.getIp() for i in range(0, processes): # get a port port = NetUtils.getFreePort() if port in portSet: flag = True while flag: time.time(5) port = NetUtils.getFreePort() if port not in portSet: break portSet.add(port) url = URL(self.PROTOCOL, host, port) url.setEndpints(endpoints=endpointSet) p = Process(target=self.worker, args=(url, )) urlSet.add(url) url.setProcess(p) p.start() return urlSet
def request(self, endpoint, param): endpoints = endpoint.split('.') appName = endpoints[0] client = self.zkRegistry.addApp(appName) #client.maintain_connections() logger.info("pool size: %s" % (str(client.pool_size()))) logger.info( "request from:%s; app:%s; path:%s; class:%s; method:%s; param:%s;" % (NetUtils.getIp(), appName, '.'.join( endpoints[1:-2]), endpoints[-2], endpoints[-1], str(param))) return client.dispatcher(NetUtils.getIp(), '.'.join(endpoints[1:-2]), endpoints[-2], endpoints[-1], list(param))
def __addAppListener(self, appName): __APP_REGISTER_PROVIDER_PATH = APP_REGISTER_PROVIDER_PATH % (appName) __APP_REGISTER_CONSUMER_PATH = APP_REGISTER_CONSUMER_PATH % (appName) if self.createPersistent(__APP_REGISTER_PROVIDER_PATH): @self.zkClient.ChildrenWatch(__APP_REGISTER_PROVIDER_PATH) def watch_children(childrens): logger.info(u'更新服务列表: %s' % childrens) if not childrens or len(childrens) == 0: self.__delApp(appName) else: old_servers = self.APP_DICT[appName].get_servers() or [] servers = list() for child in childrens: protocol, host, port = child.split(':') servers.append((host, int(port))) for oserver in old_servers: if oserver not in servers and self.APP_DICT[ appName].produce_client( oserver[0], oserver[1]).test_connect(): servers.append(oserver) self.APP_DICT[appName].set_servers(servers) if self.createPersistent(__APP_REGISTER_CONSUMER_PATH): self.createEphemeral(__APP_REGISTER_CONSUMER_PATH + "/" + NetUtils.getIp())
def invoke(self, endpointUrl='', params=[]): ''' invoke endpoint :return: ''' result = None transport = None try: # Make socket transport = TSocket.TSocket(self.url.host, self.url.port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TCompactProtocol.TCompactProtocolAccelerated(transport) # Create a client to use the protocol encoder client = BoarpcService.Client(protocol) # Connect! transport.open() endpointMetas = endpointUrl.split('.') result = client.dispatcher(NetUtils.getIp(), endpointMetas[0], endpointMetas[1], endpointMetas[2], params) finally: if transport is not None: transport.close() return result
def test_connection(self): if self.is_expired() or self.is_tired(): return False try: endpoints = HEART_ENDPOINT.split('.') self.dispatcher(NetUtils.getIp(), '.'.join(endpoints[0:-2]), endpoints[-2], endpoints[-1], list()) return True except: return False