基础教程
PHP gRPC 基础教程简介。
基础教程
本教程为 PHP 开发人员提供了 gRPC 入门基础知识。
通过此示例,您将学习如何
- 在 .proto 文件中定义服务。
- 使用协议缓冲区编译器生成客户端代码。
- 使用 PHP gRPC API 为您的服务编写一个简单的客户端。
本教程假设您对 Protocol Buffers 有基本了解。请注意,本教程中的示例使用的是 Protocol Buffers 的 proto2 版本。
另请注意,目前您只能使用 PHP 创建 gRPC 客户端。如果要创建 gRPC 服务端,请使用其他语言。
为什么使用 gRPC?
我们的示例是一个简单的路线映射应用程序,它允许客户端获取其路线上特征的信息,创建其路线摘要,并与服务器及其他客户端交换路线信息(例如交通更新)。
使用 gRPC,我们可以一次在 .proto 文件中定义服务,并生成 gRPC 支持的任何语言的客户端和服务器。这些客户端和服务器可以在各种环境中运行,从大型数据中心内的服务器到您的平板电脑,应有尽有——不同语言和环境之间通信的所有复杂性都由 gRPC 为您处理。我们还可以获得使用 Protocol Buffers 的所有优势,包括高效的序列化、简单的 IDL 和易于更新的接口。
示例代码和设置
本教程的示例代码位于 grpc/grpc/examples/php/route_guide。要下载该示例,请通过运行以下命令克隆 grpc 仓库及其子模块:
git clone --recurse-submodules -b v1.78.1 --depth 1 --shallow-submodules https://github.com/grpc/grpc
您需要 grpc-php-plugin 来帮助编译 .proto 文件。请按照以下方式从源码构建它:
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake ../..
make protoc grpc_php_plugin
popd
然后切换到 route guide 目录并编译示例的 .proto 文件:
cd examples/php/route_guide
./route_guide_proto_gen.sh
我们的示例是一个简单的路线映射应用程序,它允许客户端获取其路线上特征的信息,创建其路线摘要,并与服务器及其他客户端交换路线信息(例如交通更新)。
您还应该安装相关工具以生成客户端接口代码(以及用于测试的另一种语言的服务端)。例如,您可以按照这些安装说明来获取后者。
尝试一下!
要尝试示例应用,我们需要在本地运行一个 gRPC 服务端。让我们编译并运行该仓库中的 Node.js 服务端作为示例:
cd ../../node
npm install
cd dynamic_codegen/route_guide
nodejs ./route_guide_server.js --db_path=route_guide_db.json
运行 PHP 客户端(在另一个终端中):
./run_route_guide_client.sh
接下来的章节将逐步指导您如何定义此 proto 服务、如何从中生成客户端库,以及如何创建使用该库的客户端存根(stub)。
定义服务
首先,让我们看看我们使用的服务是如何定义的。gRPC 服务及其方法的请求和响应类型均使用 Protocol Buffers。您可以在 examples/protos/route_guide.proto 中查看我们示例的完整 .proto 文件。
要定义服务,请在您的 .proto 文件中指定一个命名的service
service RouteGuide {
...
}
然后在您的服务定义中定义 rpc 方法,并指定它们的请求和响应类型。Protocol Buffers 允许您定义四种类型的服务方法,所有这些方法都用于 RouteGuide 服务中:
简单 RPC:客户端向服务端发送请求,随后接收响应,就像普通的远程过程调用一样。
// Obtains the feature at a given position. rpc GetFeature(Point) returns (Feature) {}响应流式 RPC:客户端向服务端发送请求,并返回一系列响应消息。您可以通过在响应类型前加上
stream关键字来指定响应流式方法。// Obtains the Features available within the given Rectangle. Results are // streamed rather than returned at once (e.g. in a response message with a // repeated field), as the rectangle may cover a large area and contain a // huge number of features. rpc ListFeatures(Rectangle) returns (stream Feature) {}请求流式 RPC:客户端向服务端发送一系列消息。一旦客户端完成消息写入,它就会等待服务端读取所有消息并返回响应。您可以通过在请求类型前加上
stream关键字来指定请求流式方法。// Accepts a stream of Points on a route being traversed, returning a // RouteSummary when traversal is completed. rpc RecordRoute(stream Point) returns (RouteSummary) {}双向流式 RPC:双方都向对方发送一系列消息。这两个流独立运行,因此客户端和服务端可以按任何顺序进行读写:例如,服务端可以等待接收所有客户端消息后再写入响应,或者交替读取一条消息、写入一条消息,或其他读写组合。每条流中消息的顺序都会被保留。通过在请求和响应类型前都加上
stream关键字,即可指定此类方法。// Accepts a stream of RouteNotes sent while a route is being traversed, // while receiving other RouteNotes (e.g. from other users). rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
我们的 .proto 文件还包含我们服务方法中使用的所有请求和响应类型的协议缓冲区消息类型定义——例如,这是 Point 消息类型
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}
生成客户端代码
proto 文件的 PHP 客户端存根实现可以由 gRPC PHP Protoc 插件生成。要编译该插件:
make grpc_php_plugin
要生成客户端存根实现 .php 文件:
cd grpc
protoc --proto_path=examples/protos \
--php_out=examples/php/route_guide \
--grpc_out=examples/php/route_guide \
--plugin=protoc-gen-grpc=bins/opt/grpc_php_plugin \
./examples/protos/route_guide.proto
或者,如果您是从源码构建的 grpc-php-plugin,则在 grpc/example/php/route_guide 目录下运行辅助脚本:
./route_guide_proto_gen.sh
examples/php/route_guide 目录下将生成若干文件。您无需修改这些文件。
要加载这些生成的文件,请在 examples/php 目录下的 composer.json 文件中添加此部分:
"autoload": {
"psr-4": {
"": "route_guide/"
}
}
该文件包含:
- 用于填充、序列化和检索我们的请求和响应消息类型的所有 Protocol Buffer 代码。
- 一个名为
Routeguide\RouteGuideClient的类,允许客户端调用RouteGuide服务中定义的方法。
创建客户端
在本节中,我们将介绍如何为我们的 RouteGuide 服务创建 PHP 客户端。您可以在 examples/php/route_guide/route_guide_client.php 中查看我们完整的客户端示例代码。
构建客户端对象
要调用服务方法,我们首先需要创建一个客户端对象,即生成的 RouteGuideClient 类的实例。该类的构造函数需要我们希望连接的服务端地址和端口:
$client = new Routeguide\RouteGuideClient('localhost:50051', [
'credentials' => Grpc\ChannelCredentials::createInsecure(),
]);
调用服务方法
现在让我们看看如何调用我们的服务方法。
简单 RPC
调用简单 RPC GetFeature 几乎和调用本地异步方法一样简单。
$point = new Routeguide\Point();
$point->setLatitude(409146138);
$point->setLongitude(-746188906);
list($feature, $status) = $client->GetFeature($point)->wait();
如您所见,我们创建并填充了一个请求对象,即 Routeguide\Point 对象。然后,我们调用存根上的方法,并将请求对象传递给它。如果没有错误,我们就可以从响应对象(即 Routeguide\Feature 对象)中读取服务端返回的信息。
print sprintf("Found %s \n at %f, %f\n", $feature->getName(),
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
流式 RPC
现在让我们看看我们的流式方法。这是我们调用服务端流式方法 ListFeatures 的地方,它会返回一系列地理 Feature 数据:
$lo_point = new Routeguide\Point();
$hi_point = new Routeguide\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
$rectangle = new Routeguide\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
$call = $client->ListFeatures($rectangle);
// an iterator over the server streaming responses
$features = $call->responses();
foreach ($features as $feature) {
// process each feature
} // the loop will end when the server indicates there is no more responses to be sent.
$call->responses() 方法调用会返回一个迭代器。当服务端发送响应时,foreach 循环中会返回一个 $feature 对象,直到服务端指示没有更多响应消息要发送为止。
客户端流式方法 RecordRoute 类似,不同之处在于我们为每个要从客户端写入的点调用 $call->write($point),并获得一个 Routeguide\RouteSummary 作为响应。
$call = $client->RecordRoute();
for ($i = 0; $i < $num_points; $i++) {
$point = new Routeguide\Point();
$point->setLatitude($lat);
$point->setLongitude($long);
$call->write($point);
}
list($route_summary, $status) = $call->wait();
最后,让我们看看我们的双向流式 RPC routeChat()。在这种情况下,我们只需将上下文(context)传递给该方法,并获得一个 BidiStreamingCall 流对象,我们可以使用它来写入和读取消息。
$call = $client->RouteChat();
从客户端写入消息:
foreach ($notes as $n) {
$point = new Routeguide\Point();
$point->setLatitude($lat = $n[0]);
$point->setLongitude($long = $n[1]);
$route_note = new Routeguide\RouteNote();
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);
$call->write($route_note);
}
$call->writesDone();
从服务端读取消息:
while ($route_note_reply = $call->read()) {
// process $route_note_reply
}
双方将始终按照对方写入消息的顺序接收消息,客户端和服务端都可以按任何顺序读取和写入 —— 这些流完全独立运行。