php 使用 grpc

编辑于 2024-01-04 13:42:27 阅读 139

前提

  1. protoc(the protobuf compiler binary to generate PHP classes for your messages and service definition.)
  2. grpc_php_plugin(a plugin for protoc to generate the service stub classes.)
  3. grpc(PHP扩展)
  4. protobuf(PHP扩展)
  5. grpc/grpc(composer包)
  6. google/protobuf(composer包)

其中protocgrpc_php_plugingrpcprotobuf,这4个最耗时(正常需要30~40分钟),我做了一个php-fpm 8.1.9的镜像,大家可以参考: https://github.com/chudaozhe/grpc-php

测试

服务端demo

https://grpc.io/docs/languages/node/quickstart/

您只能在 PHP 中创建 gRPC 客户端。使用另一个 用于创建 gRPC 服务器的语言。

所以咱使用node创建服务端

# Clone the repository to get the example code
$ git clone -b @grpc/grpc-js@1.9.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc-node
# Navigate to the node example
$ cd grpc-node/examples
# Install the example's dependencies
$ npm install
# Navigate to the dynamic codegen "hello, world" Node example:
$ cd helloworld/dynamic_codegen

运行 服务端 程序

cd examples/helloworld/dynamic_codegen
node greeter_server.js

客户端demo(PHP)

https://github.com/chudaozhe/php-demo/tree/master/grpc

参考

https://github.com/grpc/grpc/blob/v1.60.0/src/php/README.md

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验