快速入门

本指南通过一个简单的示例,帮助你开始在 PHP 中使用 gRPC。

快速入门

本指南通过一个简单的示例,帮助你开始在 PHP 中使用 gRPC。

先决条件

  • PHP 7.0 或更高版本,PECL,Composer
  • grpc 扩展,protocol buffers 编译器:有关安装说明,请参阅 gRPC PHP 自述文件

获取示例代码

示例代码是 grpc 存储库的一部分。

  1. 克隆 grpc 存储库及其子模块

    git clone --recurse-submodules -b v1.66.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
    
  2. 切换到快速入门示例目录

    cd grpc/examples/php
    
  3. 安装 grpc composer 包

    ./greeter_proto_gen.sh
    composer install
    

运行示例

  1. 启动快速入门服务器:例如,按照 Node 快速入门 中的说明进行操作。

  2. examples/php 目录中,运行 PHP 客户端

    ./run_greeter_client.sh
    

恭喜!你刚刚使用 gRPC 运行了一个客户端-服务器应用程序。

更新 gRPC 服务

现在,让我们看看如何更新应用程序,在服务器上添加一个额外的方法供客户端调用。我们的 gRPC 服务使用 protocol buffers 定义;你可以在 基础教程 中找到更多关于如何在 .proto 文件中定义服务的信息。目前,你只需要知道服务器和客户端的“桩”都有一个 SayHello RPC 方法,该方法从客户端接收 HelloRequest 参数,并从服务器返回 HelloResponse,并且该方法的定义如下:

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

让我们更新一下,使 Greeter 服务有两个方法。编辑 examples/protos/helloworld.proto 并使用新的 SayHelloAgain 方法进行更新,该方法具有相同的请求和响应类型

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

记住保存文件!

重新生成 gRPC 代码

接下来,我们需要更新应用程序使用的 gRPC 代码,以使用新的服务定义。从 grpc 根目录

protoc --proto_path=examples/protos \
  --php_out=examples/php \
  --grpc_out=examples/php \
  --plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \
  ./examples/protos/helloworld.proto

如果通过源代码构建 grpc-php-plugin,则运行 grpc/example/php 目录下的 helper 脚本

./greeter_proto_gen.sh

这将重新生成 protobuf 文件,其中包含我们生成的客户端类,以及用于填充、序列化和检索我们的请求和响应类型的类。

更新并运行应用程序

我们现在有了新的生成的客户端代码,但我们仍然需要在示例应用程序中人工编写的部分中实现并调用新方法。

更新服务器

在同一目录中,打开 greeter_server.js。像这样实现新方法

function sayHello(call, callback) {
  callback(null, {message: 'Hello ' + call.request.name});
}

function sayHelloAgain(call, callback) {
  callback(null, {message: 'Hello again, ' + call.request.name});
}

function main() {
  var server = new grpc.Server();
  server.addProtoService(hello_proto.Greeter.service,
                         {sayHello: sayHello, sayHelloAgain: sayHelloAgain});
  server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
  server.start();
}
...

更新客户端

在同一目录中,打开 greeter_client.php。像这样调用新方法

$request = new Helloworld\HelloRequest();
$request->setName($name);
list($reply, $status) = $client->SayHello($request)->wait();
$message = $reply->getMessage();
list($reply, $status) = $client->SayHelloAgain($request)->wait();
$message = $reply->getMessage();

运行!

就像我们之前做的那样,从 grpc-node/examples/helloworld/dynamic_codegen 目录中

  1. 运行服务器

    node greeter_server.js
    
  2. 从另一个终端,从 examples/php 目录中,运行客户端

    ./run_greeter_client.sh
    

下一步

上次修改时间 2024 年 11 月 25 日: feat: move the $ shell line indicator to scss (#1354) (ab8b3af)