用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
## gRPC [gRPC](http://link.zhihu.com/?target=https%3A//github.com/grpc/grpc) 是谷歌基于 [Protobuf](https://github.com/protocolbuffers/protobuf) + Http2 研发的 RPC 通用框架,几乎支持流行的全部语言,该框架使用 .proto 文件定义交互的数据结构,并通过 protoc 代码生成器生成对应语言的代码进行交互。 ## PHP gRPC 现状 由于官方的 protoc 生成器里生成的 PHP 代码只有 Client 代码,这个是因为 PHP 流行的是短生命周期 phpfpm 搭建 Server 时是通过 nginx+h2+phpfpm 这种方式,因此也就不需要 Server 代码了,加上生成的 Client 代码使用的是 phpgrpc 扩展,由于使用的多线程技术,导致 Swoole 无法 Hook 因此无法使用协程。 ## Mix gRPC 由于以上问题,Mix 直接抛弃了 phpgrpc 扩展,直接通过 Swoole + [Protobuf](https://github.com/protocolbuffers/protobuf) 打造 gRPC 的 Server/Client ,为了和 go-micro 编写微服务的体验一致,Mix 开发 [protoc-gen-mix](https://github.com/mix-php/grpc/tree/master/protoc-gen-mix) 插件,能通过 .proto 文件直接生成 Service 的 Server/Client 代码,该库是独立的,能在任何框架或原生代码中使用。 ## 组件 使用 [composer](https://www.phpcomposer.com/) 安装: ``` composer require mix/grpc ``` ## 依赖注入配置 - [manifest/beans/grpc.php](https://github.com/mix-php/mix-micro-skeleton/blob/master/manifest/beans/grpc.php) ## 下载 protoc 与相关 plugin - [protoc](https://github.com/protocolbuffers/protobuf) 是 protobuf 数据结构代码生成器,负责将 .proto 数据结构文件生成为对应语言的 class、struct 供程序使用, - [protoc-gen-mix](https://github.com/mix-php/grpc/tree/master/protoc-gen-mix) 是 mix 开发的 protoc 插件,用来生成 service 的 server/client 代码。 以上 2 个二进制文件,我都帮你们编译好了,包含多个常用 OS 类型,直接下载即可: - [https://github.com/mix-php/grpc/releases/tag/binary](https://github.com/mix-php/grpc/releases/tag/binary) 下载完成后 linux、macOS 将二进制文件放入系统 `/usr/local/bin` 目录,win 放入 `C:\WINDOWS\system32` ## 生成代码 首先我们编写一个 proto 文件: ``` syntax = "proto3"; package php.micro.grpc.greeter; service Say { rpc Hello(Request) returns (Response) {} } message Request { string name = 1; } message Response { string msg = 1; } ``` 然后使用 protoc 生成代码: - Linux/Mac ``` protoc --php_out=. --mix_out=. greeter.proto ``` - Win ``` protoc.exe --php_out=. --mix_out=. greeter.proto ``` 执行命令后将在当前目录生成以下文件: ``` |-- GPBMetadata | `-- Greeter.php |-- Php | `-- Micro | `-- Grpc | `-- Greeter | |-- Request.php | |-- Response.php | |-- SayClient.php | `-- SayInterface.php `-- greeter.proto ``` 其中 Request.php、Response.php 为 `--php_out` 生成,SayClient.php SayInterface.php 由 `--mix_out` 生成。 接下来我们将生成的文件加入到 composer autoload 中,我们修改 composer.json: ``` "autoload-dev": { "psr-4": { "GPBMetadata\\": "protos/GPBMetadata/", "Php\\": "protos/Php/" } } ``` 修改后执行 `composer dump-aotoload` 使其生效。 ## 服务器 我们用原生 php 代码来编写一个 gRPC 服务器: ``` // 编写一个服务,实现 protoc-gen-mix 生成的接口 class SayService implements \Php\Micro\Grpc\Greeter\SayInterface { public function Hello(\Mix\Context\Context $context, \Php\Micro\Grpc\Greeter\Request $request): \Php\Micro\Grpc\Greeter\Response { $response = new \Php\Micro\Grpc\Greeter\Response(); $response->setMsg(sprintf('hello, %s', $request->getName())); return $response; } } // 创建一个服务器 $server = new \Mix\Grpc\Server('0.0.0.0', 9595); // 默认会随机分配端口,也可以指定 $server->register(SayService::class); $server->start(); ``` ## 客户端 接下来我们用另一个 php 文件来编写一个客户端程序: ``` // 拨号一个连接 $dialer = new \Mix\Grpc\Client\Dialer(); $conn = $dialer->dial('127.0.0.1', 9595); // 通过连接创建客户端 $client = new \Php\Micro\Grpc\Greeter\SayClient($conn); // 发送请求 $request = new \Php\Micro\Grpc\Greeter\Request(); $request->setName('xiaoming'); $response = $client->Hello($request); // 打印结果 var_dump($response->getMsg()); ```