grpc 使用方法


php中使用gRPC
php需要安装grpc扩展。
使用protoc命令生成对应的php代码:
protoc --php_out=plugins=grpc:./ exporttask.proto

生成代码包括:
Exporttask/
  |-WshExportFormat.php
  |-WshExportTaskCreateTplReq.php
  |-WshExportTaskCreateTplRes.php
  |-WshExportTaskListTplReq.php
  |-WshExportTaskListTplRes.php
  |-WshExportTpl.php
  \-WshExportTplStatus.php
GPBMetadata/
  \-Exporttask.php
每一个message对应生成一个类,在Exporttask命名空间下。
这里就不都贴出来了,只贴一个WshExportTpl.php:
<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: exporttask.proto

namespace Exporttask;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

/**
 * Protobuf type <code>exporttask.WshExportTpl</code>
 */
class WshExportTpl extends \Google\Protobuf\Internal\Message
{
    /**
     * <code>string etplName = 1;</code>
     */
    private $etplName = '';
    /**
     * <code>string etplTag = 2;</code>
     */
    private $etplTag = '';
    /**
     * <code>.exporttask.WshExportFormat etplOutputFormat = 3;</code>
     */
    private $etplOutputFormat = 0;
    /**
     * <code>string etplOutputColumns = 4;</code>
     */
    private $etplOutputColumns = '';
    /**
     * <code>string etplExpr = 5;</code>
     */
    private $etplExpr = '';
    /**
     * <code>int32 etplId = 6;</code>
     */
    private $etplId = 0;
    /**
     * <code>int32 etplExecTimes = 7;</code>
     */
    private $etplExecTimes = 0;
    /**
     * <code>int32 etplExecOkTimes = 8;</code>
     */
    private $etplExecOkTimes = 0;
    /**
     * <code>int32 etplStatus = 9;</code>
     */
    private $etplStatus = 0;
    /**
     * <code>string etplCreated = 10;</code>
     */
    private $etplCreated = '';
    /**
     * <code>string etplUpdated = 11;</code>
     */
    private $etplUpdated = '';
    /**
     * <code>string etplDeleted = 12;</code>
     */
    private $etplDeleted = '';
    /**
     * <code>int32 operatorId = 13;</code>
     */
    private $operatorId = 0;

    public function __construct() {
        \GPBMetadata\Exporttask::initOnce();
        parent::__construct();
    }

    /**
     * <code>string etplName = 1;</code>
     */
    public function getEtplName()
    {
        return $this->etplName;
    }

    /**
     * <code>string etplName = 1;</code>
     */
    public function setEtplName($var)
    {
        GPBUtil::checkString($var, True);
        $this->etplName = $var;
    }
    // ... 其他省略
<?php
  $client = new \Exporttask\GreeterClient('127.0.0.1:50051', [
        'credentials' => \Grpc\ChannelCredentials::createInsecure(),
    ]);
  $request = new Exporttask\WshExportTaskCreateTplReq();
  $request->setEtplName($name);
  list($reply, $status) = $client->createTpl($request)->wait();
  $message = $reply->getMessage();
  var_dump($message);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gRPC是Google开源的一款高性能、通用的远程过程调用(RPC)框架,支持多种编程语言。它可以让客户端应用程序像调用本地对象一样直接调用远程服务,使得分布式应用程序更加容易构建。下面简单介绍一下gRPC的原理和使用方法。 ## 原理 gRPC采用基于HTTP/2协议的传输方式,使用protobuf作为序列化协议,在网络传输时,客户端和服务端通过protobuf序列化和反序列化来进行数据的传输和解析。gRPC的传输方式可以使用TLS加密,确保数据的安全性。 gRPC支持四种服务类型: - Unary:一元RPC,客户端发送一个请求给服务端,服务端返回一个响应结果。 - Server streaming:服务器流式RPC,客户端发送一个请求给服务端,服务端返回一个消息流给客户端,即服务器可以使用流式响应多次。 - Client streaming:客户端流式RPC,客户端使用流式传输一系列消息给服务端,服务端返回一个响应结果。 - Bidirectional streaming:双向流式RPC,客户端和服务端都可以使用流式传输来发送消息,客户端和服务端可以同时使用流式消息互相通讯。 ## 使用方法 gRPC使用步骤如下: 1. 定义服务接口:使用protobuf语言定义服务接口,包括服务方法名、请求参数和响应类型。 2. 生成服务端和客户端代码:使用protobuf编译器将服务接口定义文件生成对应语言的代码,生成的代码包含服务端和客户端的代码。 3. 实现服务端:编写服务端代码,实现服务端接口定义中的方法。 4. 实现客户端:编写客户端代码,调用服务端提供的方法。 5. 启动服务端和客户端:启动服务端和客户端,使它们可以相互通信。 gRPC使用方法可以分为下面几个步骤: 1. 安装gRPC:安装gRPC的核心库和插件。 2. 定义服务接口:使用protobuf语言定义服务接口。 3. 生成代码:使用protobuf编译器生成对应语言的代码。 4. 实现服务端:编写服务端代码。 5. 实现客户端:编写客户端代码。 6. 启动服务端和客户端:启动服务端和客户端,使它们可以相互通信。 以上就是gRPC的原理和使用方法的简单介绍,如果需要更加详细的了解可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值