<?php namespace MessageController; use ThinkController; use ThriftExceptionTException; use ThriftProtocolTBinaryProtocol; use ThriftTransportTBufferedTransport; use ThriftTransportTHttpClient; use ThriftTransportTPhpStream; use ThriftTMultiplexedProcessor; use ThriftProtocolTMultiplexedProtocol; use MessageServicesMessageServie; use RpcMsgMessageClient; use RpcMsgMessageProcessor; use ThriftFactoryTBinaryProtocolFactory; use ThriftFactoryTTransportFactory; use ThriftServerTServerSocket; use ThriftServerTSimpleServer; use ThriftServerTForkingServer; use ThriftTransportTSocket;
server
public function message_rpc() { try { // 初始化多个服务提供者handle $messageprocessor = new RpcMsgMessageProcessor(new MessageServie()); // 创建多个服务Processor $sendProcessor = new RpcMsgSendMsgProcessor(new MessageServicesSendMsgServie()); // 将服务注册到TMultiplexedProcessor中 $tFactory = new TTransportFactory(); $pFactory = new TBinaryProtocolFactory(true, true); $processor = new TMultiplexedProcessor(); // 将服务注册到TMultiplexedProcessor中 //队列消费者rpc请求 $processor->registerProcessor("MessageApiAction", $messageprocessor); //消息发送rpc请求 $processor->registerProcessor("sendMsg", $sendProcessor); // 初始化数据传输方式transport // 利用该传输方式初始化数据传输格式protocol // 监听开始 $transport = new TServerSocket('0.0.0.0', '9988'); // $processor->process($pFactory, $pFactory); $server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory); $server->serve(); } catch (TException $tx) { ThinkLog::write($tx->getMessage()); } catch(Exception $e){ ThinkLog::write($e->getMessage()); } }
client
public function local() { try { ini_set('memory_limit', '1024M'); $socket = new TSocket('192.168.1.188', '9988'); $socket->setRecvTimeout(50000); $socket->setDebug(true); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new MessageClient(new TMultiplexedProtocol($protocol, "MessageApiAction")); // $client = new MessageClient($protocol); $transport->open(); $result = $client->MessageApiAction('message api'); print_r($result); $transport->close(); } catch (TException $tx) { print_r($tx->getMessage()); } } public function send_msg() { try { ini_set('memory_limit', '1024M'); $socket = new TSocket('192.168.1.188', '9988'); $socket->setRecvTimeout(50000); $socket->setDebug(true); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new RpcMsgSendMsgClient(new TMultiplexedProtocol($protocol, "sendMsg")); // $client = new RpcMsgSendMsgClient($protocol); $transport->open(); $result = $client->sendMsg('send msg'); print_r($result); $transport->close(); } catch (TException $tx) { print_r($tx->getMessage()); } }