要求:
不适用nginx+fastcgi情况下,分布式系统之间如果通讯,如果不阻塞,能并发处理请求
环境:
luman/laravel:5.5
php:7.2
thrift -version :Thrift version 0.11.0
thrift文件模板:testServer.thrift
1 2 3 4 5 6 | namespace php Rpc.Test service Echop { string Echop( 1 : string str ) , } |
生成RPC文件:
1 | thrift - r - - out . / app - - gen php:server . / ThriftSource / testServer.thrift |
服务端的实现:
安装第三方扩展包:
composer require sunlong/thrift
或者
https://github.com/sunlongv5/thrift.git
composer文件修改:
1 2 3 4 5 6 7 8 9 10 11 | "autoload" : { "classmap" : [ "app/Rpc" ], "psr-4" : { "Rpc\\": " app / Rpc", "Services\\": " app / services", "Thrift\\": " vendor / sunlong / thrift / lib / php / lib / Thrift / " } }, |
新建Sevice文件夹创建文件EchopServie.php 实现thrift的Echop方法
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace Services; use Rpc\Test\EchopIf; class EchopServie implements EchopIf{ public function Echop($str){ \Log::info($str); sleep(5); return "RPC:" .$str; } } |
创建文件app\Console\Commands\RpcServer.php
此代码为thrift的server实现

<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Thrift\Exception\TException; use Thrift\Factory\TBinaryProtocolFactory; use Thrift\Factory\TTransportFactory; use Thrift\Server\TServerSocket; use Thrift\Server\TSimpleServer; use Thrift\Server\TForkingServer; use Thrift\TMultiplexedProcessor; use Rpc\Test\EchopClient; use Rpc\Test\EchopProcessor; use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TBufferedTransport; class RpcServer extends Command{ protected $signature = 'server:rpc'; /** * 控制台命令说明。 * * @var string */ protected $description = 'rpc 服务'; protected static $socketController; public function handle() { try { $handler = new \Services\EchopServie(); $processor = new EchopProcessor($handler); // 将服务注册到TMultiplexedProcessor中 $tFactory = new TTransportFactory(); $pFactory = new TBinaryProtocolFactory(true, true); $multiplexedProcessor = new TMultiplexedProcessor(); $multiplexedProcessor->registerProcessor("Echop", $processor); // 监听开始 $transport = new TServerSocket('0.0.0.0', '9998'); $server = new TForkingServer($processor, $transport, $tFactory, $tFactory, $pFactory, $pFactory); $server->serve(); } catch (TException $te) { throw new \Exception($te->getMessage()); } } }
app\Console\Kernel.php文件中添加
1 2 3 | protected $commands = [ RpcServer:: class , ]; |
客户端实现:
添加路由:
1 | $router - >get( '/rpc/test' , [ 'uses' = > 'Controller@test' ]); |
1 | Controller文件新增test方法 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public function test(){ try { ini_set( 'memory_limit' , '1024M' ); / / $socket = new THttpClient( 'http://192.168.1.188' , 5201 , '/rpc/test2' ); $socket = new TSocket( '192.168.1.188' , '9998' ); $socket - >setRecvTimeout( 50000 ); $socket - >setDebug(true); $transport = new TBufferedTransport($socket, 1024 , 1024 ); $protocol = new TBinaryProtocol($transport); $client = new \Rpc\Test\EchopClient($protocol); $transport - > open (); $result = $client - >Echop( 'hello world !' ); print_r($result); $transport - >close(); } catch (TException $tx) { print_r($tx - >getMessage()); } } |
启动服务端:
1 | / application / php7 / bin / php artisan server:rpc |
模拟请求:
7878端口为nginx启动的客户端地址
同时刷新三个页面,发现每个页面都是5秒返回的结果,没有阻塞
采用python客户端测试:
/application/python3.6.4/bin/pip3 install thrift
生成pthon客户端thrift文件
thrift -out .. --gen py ./ThriftSource/testPyServer.thrift
编写python的客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #! /usr/bin/env python # -*- coding: utf-8 -*- from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from Rpc.Python.Echop import Client import threading __HOST = '192.168.1.188' __PORT = 9998 def send(data): tsocket = TSocket.TSocket(__HOST, __PORT) transport = TTransport.TBufferedTransport(tsocket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Client(protocol) transport. open () print (client.Echop(data)) if __name__ = = '__main__' : threadl = [] t1 = threading.Thread(target = send,args = ( 'python001' ,)) t2 = threading.Thread(target = send,args = ( 'python002' ,)) t3 = threading.Thread(target = send,args = ( 'python003' ,)) threadl.append(t1) threadl.append(t2) threadl.append(t3) for x in threadl: x.start() |
执行客户端
1 | python3 . / client.py |
结果同一时刻返回三条记录
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/10702842.html
【推荐】国内首个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 加持,客户体验更智能