• 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:workspacelaravel51-serverappservicesEchopServie.php

    <?php
    namespace Services;
    use RpcTestEchopIf;
    
    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 StatsControllers;
     2 use IlluminateSupportFacadesAuth;
     3 use IlluminateSupportFacadesDB as DB;
     4 use RpcTestEchopClient;
     5 use RpcTestEchopProcessor;
     6 use StatsModelsTestTest;
     7 use EntryUser;
     8 use Request;
     9 use ThriftExceptionTException;
    10 use ThriftProtocolTBinaryProtocol;
    11 use ThriftTransportTBufferedTransport;
    12 use ThriftTransportTHttpClient;
    13 use ThriftTransportTPhpStream;
    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 "
    ";
    25                 exit();
    26             }
    27             $handler = new ServicesEchopServie();
    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 RpcTestEchopClient($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

  • 相关阅读:
    EELS
    企业管理软件随想透视>包容,无形思想>有形方便
    定风波
    企业管理软件随想也谈企业框架软件需求
    Delphi数据库开发-前言
    游戏引发的……
    js 当前时间
    代码片段
    阶段总结
    Web界面设计
  • 原文地址:https://www.cnblogs.com/sunlong88/p/10031988.html
Copyright © 2020-2023  润新知