php 在laravel中集成grpc

发布时间 2023-12-21 17:16:46作者: 钻牛角尖儿

在laravel中集成使用grpc

一、安装:

  1、macOS

    php扩展:pecl install grpc,pecl install protobuf

    protocbuf:brew install protobuf

    etcd:brew install etcd

  2、ubuntu:

    php扩展: pecl install grpc(如果遇到 [Makefile:1038: src/core/lib/compression/message_compress.lo] Error 1,先安装:sudo apt install zlib1g-dev),pecl install protobuf

    protocbuf:sudo apt install libprotobuf-dev protobuf-compiler

    etcd:sudo apt install etcd (配置文件环境变量格式,在/etc/default/etcd)

二、使用:

  1、laravel 安装依赖包

    composer install google/protobuf

    composer install grpc/grpc

  2、编写.proto文件

  3、通过protoc生命模版代码(参考:https://github.com/grpc/grpc/tree/master/src/php)

  4、生成的代码的命名空间不能修改,可以把生成的目录加到composer.json的autoload下 

         

  4、编写好的client和server,可用通过增加console/commands,通过artisan启动测试(后续可以通过supervisor维护进程)

  5、xxxClient.php是client端(php项目可以直接使用),xxxStub.php 是服务端(非php项目调用服务,是根据该文件中注册的名字请求服务的

三、ETCD:

  由于laravel没有提供注册服务的封装,可以参考go-zero或Hyperf,结合supervisor自己实现;另外etcd的php客户端应该是停更了,把etcd的接口贴出来供

  参考(https://github.com/etcd-io/etcd/blob/release-3.5/Documentation/dev-guide/apispec/swagger/rpc.swagger.json)