laravle 整合 thrift

1,安装thrift

https://www.cnblogs.com/sunlong88/p/9965522.html

 

2,生成 RPC文件

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 }
composer

 

新建 EchopServie文件:

E:\workspace\laravel51-server\app\services\EchopServie.php

<?php
namespace Services;
use Rpc\Test\EchopIf;

class EchopServie implements EchopIf{
    public function Echop($str){
        \Log::info($str);
        return "RPC:".$str;
    }
}

  

3,编写服务端和客户端:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值