<?php namespace Message\Controller; use Think\Controller; use Thrift\Exception\TException; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\THttpClient; use Thrift\Transport\TPhpStream; use Thrift\TMultiplexedProcessor; use Thrift\Protocol\TMultiplexedProtocol; use Message\Services\MessageServie; use Rpc\Msg\MessageClient; use Rpc\Msg\MessageProcessor; use Thrift\Factory\TBinaryProtocolFactory; use Thrift\Factory\TTransportFactory; use Thrift\Server\TServerSocket; use Thrift\Server\TSimpleServer; use Thrift\Server\TForkingServer; use Thrift\Transport\TSocket;
server
public function message_rpc() { try { // 初始化多个服务提供者handle $messageprocessor = new \Rpc\Msg\MessageProcessor(new MessageServie()); // 创建多个服务Processor $sendProcessor = new \Rpc\Msg\SendMsgProcessor(new \Message\Services\SendMsgServie()); // 将服务注册到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) { \Think\Log::write($tx->getMessage()); } catch(\Exception $e){ \Think\Log::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 \Rpc\Msg\SendMsgClient(new TMultiplexedProtocol($protocol, "sendMsg")); // $client = new \Rpc\Msg\SendMsgClient($protocol); $transport->open(); $result = $client->sendMsg('send msg'); print_r($result); $transport->close(); } catch (TException $tx) { print_r($tx->getMessage()); } }
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/11016277.html
分类:
thrift
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能