PHP GRPC 模塊安裝配置

  1. PHP 依賴安裝

    • 相關(guān)依賴安裝
    $ git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
    $ cd grpc
    $ git pull --recurse-submodules && git submodule update --init --recursive
    $ make
    $ sudo make install
    
    # make install 會(huì)在 /usr/local/bin 目錄下生成以下文件
    #grpc_cpp_plugin  
    #grpc_csharp_plugin  
    #grpc_node_plugin  
    #grpc_objective_c_plugin  
    #grpc_php_plugin  
    #grpc_python_plugin  
    #grpc_ruby_plugin
    #protobuf文件生成各種語(yǔ)言的插件
    #注意node 不需要可以直接解析
    
    #protobuf 編譯模塊安裝 protoc
    $ git clone  https://github.com/google/protobuf.git
    $ cd protobuf
    $ ./configure
    $ sudo make
    $ sudo make install 
    #會(huì)生成 /usr/local/bin/protoc 可執(zhí)行文件
    
    #安裝gRPC PHP拓展
    #方法一
    $ cd grpc/src/php/ext/grpc
    $ phpize
    $ ./configure
    $ make
    $ sudo make install
    #別忘記在php.ini 文件中加入 extension_dir = "$PHP_PATH/lib/php/extensions/debug-zts-20131226/"     extension = grpc.so
    
    #方法2
    $ sudo pecl install grpc
    #別忘記在php.ini 文件中加入 extension_dir = "$PHP_PATH/lib/php/extensions/debug-zts-20131226/"     extension = grpc.so
    
    
    #安裝protobuf 依賴按住
    #方法1 C依賴模塊
    $ pecl install protobuf
    
    #方法2 PHP 依賴模塊 需要安裝 composer
    $ composer require google/protobuf
    
    #安裝依賴包
    $ composer require grpc/grpc
    
  2. protobuf 文件編譯成PHP文件

    • lisa.proto文件
    syntax = "proto3";
    
    package lisa;
    // The greeting service definition.
    service Greeter {
      // Sends a greeting
      rpc SayName (LisaRequest) returns (LisaReply) {}
    }
    // The request message containing the user's name.
    message LisaRequest {
      string name = 1;
    }
    // The response message containing the greetings
    message LisaReply {
      string message = 1;
    }
    
    • 編譯protobuf 文件生成PHP代碼
    $ protoc --proto_path=./ --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin ./lisa.proto
    #執(zhí)行成功之后可以看到生成生成以下文件
    #Lisa/GreeterClient.php  
    #Lisa/LisaReply.php  
    #Lisa/LisaRequest.php
    #GPBMetadata/Lisa.php
    
    • PHP客戶端代碼
    lisa_client.php
    <?php
    include __DIR__ . '/vendor/autoload.php';
    include __DIR__ . '/GPBMetadata/Lisa.php';
    include __DIR__ . '/Lisa/LisaReply.php';
    include __DIR__ . '/Lisa/LisaRequest.php';
    include __DIR__ . '/Lisa/GreeterClient.php';
    $client = new Lisa\GreeterClient('localhost:12345', [
       'credentials' => Grpc\ChannelCredentials::createInsecure(),
       ]);
    $request = new Lisa\LisaRequest();
    $name = !empty($argv[1]) ? $argv[1] : 'world';
    $request->setName($name);
    list($reply, $status) = $client->SayName($request)->wait();
    $message = $reply->getMessage();
    echo $message,PHP_EOL;
    ?>
    
    //要先運(yùn)行g(shù)RPC服務(wù)端代碼
    //服務(wù)端用node 實(shí)現(xiàn) PHP 不支持
    //服務(wù)端代碼看d介紹
    //php lisa_client.php 執(zhí)行文件
    //執(zhí)行成功數(shù)據(jù) Hello world
    
    • gRPC 不支持PHP服務(wù)端用NODE代替
    lisa_server.js
    //需要安裝grpc模塊,npm install grpc 
    //node 不需要把protbuf文件翻譯成相應(yīng)代碼,可以直接引入protobuf 文件
    //lisa_server.js
    var PROTO_PATH = __dirname + '/lisa.proto';
    var grpc = require('grpc');
    var lisa_proto = grpc.load(PROTO_PATH).lisa;
    /**
     * Implements the SayHello RPC method.
     */
    function sayName(call, callback) {
      callback(null, {message: 'Hello ' + call.request.name});
    }
    /**
     * Starts an RPC server that receives requests for the Greeter service at the
     * sample server port
     */
    function main() {
      var server = new grpc.Server();
      server.addProtoService(lisa_proto.Greeter.service, {sayName: sayName});
      server.bind('0.0.0.0:12345', grpc.ServerCredentials.createInsecure());
      server.start();
    }
    main();
    // node lisa_server.
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容