grpc+php接入指南

2019-04-18  本文已影响0人  耗子_aca3

grpc+php接入指南:附https://github.com/grpc/grpc/tree/master/src/php

grpc端使用go实现,客户端使用php实现

php需要grpc扩展 下载地址

composer 需要安装两个包

grpc/grpc

google/protobuf

使用protoc 生成对应的php protocol buffers 文件

protoc --php_out=.  token.proto

下面基于phalcon框架写的一个简单grpc调用

grpc 客户端代码

<?php

namespace App\Utils;

use Grpc;

class TokenClient extends Grpc\BaseStub

{

    public function __construct(string $hostname, array $opts, $channel = null)

    {

        parent::__construct($hostname, $opts, $channel);

    }

    public function GetComponentToken(\Protoc\ReqTokenInfo $args)

    {

        //"\Protoc\ReqTokenInfo"    //路径为请求体对应的那个类

        //"protoc.TokenService/GetComponentToken"  //路径为proto文件对应的(包名.服务名/方法名)

        return $this->_simpleRequest('/protoc.TokenService/GetComponentToken',

            $args,

            ['\Protoc\ResTokenInfo','decode']);

        //"\Protoc\ResTokenInfo"  //路径为响应体对应的那个类

    }

}

grpc php调用客户端实现代码

<?php

namespace Controllers\Proto;

use App\Utils\TokenClient;

use Grpc\ChannelCredentials;

use Protoc\ReqTokenInfo;

class Request

{

    public function index()

    {

        $client = new TokenClient('10.0.1.62:7666',[

            'credentials' => ChannelCredentials::createInsecure()

        ]);

        $request = new ReqTokenInfo();

        $request->setComponentAppid("wx9ad710182d38d107");

        $get = $client->GetComponentToken($request)->wait();

        var_dump($get);

    }

}

上一篇 下一篇

猜你喜欢

热点阅读