Skip to content

使用 protoc-gen-grpc 组件生成的 client 无法使用 #2068

@zxr615

Description

@zxr615

按照官网文档跑了跑了一遍 demo 是完全没有问题的
发现 protoc 有自动生成 client 的 plugin
https://github.com/grpc/grpc/tree/master/src/php#generate-php-classes-from-your-service-definition
试了试发现有些问题,不能使用,过程如下:

  • 官方文档没问题
// hyperf 已经封装好了 \Hyperf\GrpcClient\BaseClient, 只要根据 .proto 文件中的定义, 按需扩展:
class HiClient extends BaseClient
{
    public function sayHello(HiUser $argument)
    {
        return $this->simpleRequest(
            '/grpc.hi/sayHello',
            $argument,
            [HiReply::class, 'decode']
        );
    }
}
  • 使用 protoc-gen-grpc 生成的 client

当代码运行到 $this->_simpleRequest(...) 此方法没有任何响应,页面也一直处于请求中,具体执行到vendor/grpc/grpc/src/lib/BaseStub.php_GrpcUnaryUnary方法中的$call->start($argument, $metadata, $options); 就没有返回了,一直处于等待中,但我在 laravel 中不存在这个问题,不知道这个问题是因为常驻内存导致的呢?

<?php

namespace Grpc;

class hiClient extends \Grpc\BaseStub {
    
    public function __construct($hostname, $opts, $channel = null) {
        parent::__construct($hostname, $opts, $channel);
    }
    
    public function sayHello(\Grpc\HiUser $argument,
      $metadata = [], $options = []) {
        return $this->_simpleRequest('/grpc.hi/sayHello',
        $argument,
        ['\Grpc\HiReply', 'decode'],
        $metadata, $options);
    }
}
  • 调用
$client = new hiClient('127.0.0.1:9503', [
    'credentials' => ChannelCredentials::createInsecure()
]);

$request = new HiUser();
$request->setName('hyperf');
$request->setSex(1);
// 一直停留在这里,没有任何响应
$rs = $client->sayHello($request);
[$reply, $status] = $rs->wait();

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions