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

 

版权声明:本文为sunlong88原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sunlong88/p/10031988.html