1,安装thrift
https://www.cnblogs.com/sunlong88/p/9965522.html
2,生成 RPC文件
1 | thrift -r -- out ./app --gen php:server ./ThriftSource/testServer.thrift |
composer文件:

1 { 2 "name": "laravel/laravel", 3 "description": "The Laravel Framework.", 4 "keywords": ["framework", "laravel"], 5 "license": "MIT", 6 "type": "project", 7 "require": { 8 "php": ">=5.5.9", 9 "laravel/framework": "5.1.*", 10 "bican/roles": "2.1.*", 11 "leric/php-thrift": "0.9.*" 12 }, 13 "require-dev": { 14 "fzaninotto/faker": "~1.4", 15 "mockery/mockery": "0.9.*", 16 "phpunit/phpunit": "~4.0", 17 "phpspec/phpspec": "~2.1" 18 }, 19 "autoload": { 20 "classmap": [ 21 "database", 22 "app/Rpc" 23 24 ], 25 "psr-4": { 26 "Entry\\": "app/entry", 27 "Api\\": "app/api", 28 "Stats\\": "app/stats", 29 "Rpc\\": "app/Rpc", 30 "Services\\": "app/services" 31 } 32 }, 33 "autoload-dev": { 34 "classmap": [ 35 "tests/TestCase.php" 36 ] 37 }, 38 "scripts": { 39 "post-root-package-install": [ 40 "php -r \"copy('.env.example', '.env');\"" 41 ], 42 "post-create-project-cmd": [ 43 "php artisan key:generate" 44 ], 45 "post-install-cmd": [ 46 "Illuminate\\Foundation\\ComposerScripts::postInstall", 47 "php artisan optimize" 48 ], 49 "post-update-cmd": [ 50 "Illuminate\\Foundation\\ComposerScripts::postUpdate", 51 "php artisan optimize" 52 ] 53 }, 54 "config": { 55 "preferred-install": "dist" 56 } 57 }
新建 EchopServie文件:
E:\workspace\laravel51-server\app\services\EchopServie.php
1 2 3 4 5 6 7 8 9 10 | <?php namespace Services; use Rpc\Test\EchopIf; class EchopServie implements EchopIf{ public function Echop($str){ \Log::info($str); return "RPC:" .$str; } } |
3,编写服务端和客户端:
1 2 | Route::post( '/rpc/index' , 'RpcController@index' )->name( 'rpc.index' ); Route:: get ( '/rpc/test' , 'RpcController@test' )->name( 'rpc.test' ); |
RpcController文件
1 <?php namespace Stats\Controllers; 2 use Illuminate\Support\Facades\Auth; 3 use Illuminate\Support\Facades\DB as DB; 4 use Rpc\Test\EchopClient; 5 use Rpc\Test\EchopProcessor; 6 use Stats\Models\Test\Test; 7 use Entry\User; 8 use Request; 9 use Thrift\Exception\TException; 10 use Thrift\Protocol\TBinaryProtocol; 11 use Thrift\Transport\TBufferedTransport; 12 use Thrift\Transport\THttpClient; 13 use Thrift\Transport\TPhpStream; 14 15 class RpcController extends Controller 16 { 17 18 public function index(Request $request) 19 { 20 try { 21 ob_end_clean(); 22 header("Content-type: application/x-thrift; charset=utf-8"); 23 if (php_sapi_name() == 'cli') { 24 echo "\r\n"; 25 exit(); 26 } 27 $handler = new \Services\EchopServie(); 28 $processor = new EchopProcessor($handler); 29 $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); 30 $protocol = new TBinaryProtocol($transport, true, true); 31 $transport->open(); 32 $processor->process($protocol, $protocol); 33 $transport->close(); 34 } catch (TException $tx) { 35 \Log::info($tx->getMessage()); 36 } 37 } 38 39 public function test(Request $request) 40 { 41 try { 42 ini_set('memory_limit', '1024M'); 43 $socket = new THttpClient('192.168.1.188', 8082, '/rpc/index'); 44 $transport = new TBufferedTransport($socket, 1024, 1024); 45 $protocol = new TBinaryProtocol($transport); 46 $client = new \Rpc\Test\EchopClient($protocol); 47 $transport->open(); 48 $result = $client->Echop('hello world !'); 49 print_r($result); 50 $transport->close(); 51 } catch (TException $tx) { 52 print_r($tx->getMessage()); 53 } 54 55 } 56 57 58 }
最后测试:
访问:http://192.168.1.188:8082/rpc/test
详情:https://github.com/sunlongv5/laravel-thrift
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/p/10031988.html
分类:
thrift
标签:
laravel 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 加持,客户体验更智能