工作中需要使用gRPC,服务端采用的python,客户端采用PHP。这里主要讲述PHP客户端。

分为以下几个部分:

  1. 安装protoc
  2. 生成protobuf
  3. 安装PHP扩展
  4. 定义客户端

安装protoc

这里是mac环境

下载地址:https://github.com/protocolbu...

  • 解压之后进入目录,执行./autogen.sh
  • 如果报错的话需要安装插件brew install automake
  • 再次执行./autogen.sh
  • ./configure --prefix=/usr/local/protobuf
  • make && make install

最后不要忘记配置环境变量

vim ~/.bash_profile
export PROTOBUF=/usr/local/protobuf
export PATH=$PROTOBUF/bin:$PATH
source ~/.bash_profile

验证

protoc --version

生成protobuf文件

使用服务端的.proto文件,执行protoc --php_out=. lottery.proto


syntax = "proto3";
package lotteryservice;
service Greeter {
    rpc lottery(lotteryReq) returns (lotteryRes){}
}

message lotteryReq {
    string param = 1;
}

message lotteryRes {
    string data = 1;
}

会生成如下目录:

clipboard.png

安装PHP扩展

gRPC扩展: http://pecl.php.net/package/gRPC
protobuf扩展: http://pecl.php.net/package/p...

自定义客户端

在项目目录下编写composer.json

{
  "name": "grpc-go-php",
  "require": {
    "grpc/grpc": "^v1.3.0",
    "google/protobuf": "^v3.3.0"
  },
  "autoload":{
    "psr-4":{
      "GPBMetadata\\":"GPBMetadata/",
      "Lotteryservice\\":"Lotteryservice/"
    }
  }
}

composer install 之后会生成如下目录:

clipboard.png

在Lotteryservice文件夹中,创建lotteryServiceClient.php

<?php
namespace Lotteryservice;

class lotteryServiceClient extends \Grpc\BaseStub{

    public function __construct($hostname, $opts, $channel = null) {
        parent::__construct($hostname, $opts, $channel);
    }

    public function lottery(\Lotteryservice\lotteryReq $argument, $metadata=[], $options=[]){
        // (/Greeter/lottery) 是请求服务端那个服务和方法,基本和 proto 文件定义一样
        // (\Lotteryservice\lotteryRes) 是响应信息(那个类),基本和 proto 文件定义一样
        return $this->_simpleRequest('/Greeter/lottery',
            $argument,
            ['\Lotteryservice\lotteryRes', 'decode'],
            $metadata, $options);
    }

}

创建channel文件夹,在channel文件夹下创建 channels.php文件,获取client

<?php
namespace channel;

class channels
{
    public function lotteryService()
    {
        $client = new \Lotteryservice\lotteryServiceClient('127.0.0.1:50051', [
            'credentials' => \Grpc\ChannelCredentials::createInsecure()
        ]);

        return $client;
    }

}

在项目目录下创建app.php,用于测试连接:

<?php

//引入 composer 的自动载加
require __DIR__ . '/vendor/autoload.php';

$channels = new channel\channels();

$lotteryClient = $channels->lotteryService();

$lotteryRequest = new \Lotteryservice\lotteryReq();

$lotteryRequest->setParam('{"一等奖": 10,"二等奖":20,"三等奖":30,"四等奖":40}');

$lottery_res = $lotteryClient->lottery($lotteryRequest)->wait();

list($reply, $status) = $lottery_res;
$data = $reply->getData();
var_dump($data);die;

最后执行php app.php


否思
30 声望1 粉丝

小码农