クイックスタート
このガイドでは、簡単な動作例とともに PHP での gRPC の開始方法を説明します。
クイックスタート
前提条件
- PHP 7.0 以降、PECL、Composer
- grpc 拡張機能、protocol buffers コンパイラ:インストール方法については、gRPC PHP readme を参照してください。
サンプルコードを取得する
サンプルコードは grpc リポジトリの一部です。
注記
PHP で gRPC クライアントを作成できます。gRPC サーバーの作成には 他の言語 を使用してください。grpc リポジトリとそのサブモジュールをクローンします。
git clone --recurse-submodules -b v1.74.0 --depth 1 --shallow-submodules https://github.com/grpc/grpcクイックスタートのサンプルディレクトリに移動します。
cd grpc/examples/phpgrpccomposer パッケージをインストールします。./greeter_proto_gen.sh composer install
サンプルを実行する
クイックスタートサーバーを起動します:例えば、Node のクイックスタート の指示に従ってください。
examples/phpディレクトリから、PHP クライアントを実行します。./run_greeter_client.sh
おめでとうございます!gRPCを使用したクライアント・サーバーアプリケーションを実行できました。
gRPCサービスを更新する
次に、クライアントが呼び出すためのサーバー上の追加メソッドでアプリケーションを更新する方法を見てみましょう。gRPC サービスは protocol buffers を使用して定義されます。.proto ファイルでサービスを定義する方法については、Basics tutorial で詳細を確認できます。現時点では、サーバーとクライアントの "stub" の両方に、クライアントから HelloRequest パラメータを受け取り、サーバーから HelloResponse を返す SayHello RPC メソッドがあり、このメソッドは次のように定義されていることを知っておけば十分です。
// 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` サービスが 2 つのメソッドを持つようにしましょう。`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 ディレクトリの下にあるヘルパースクリプトを実行します。
./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 ディレクトリから
サーバーを実行する
node greeter_server.js別のターミナルから、
examples/phpディレクトリに移動し、クライアントを実行します。./run_greeter_client.sh
次は何をするか
- gRPCの仕組みをgRPCの紹介とコアコンセプトで学習してください。
- Basics tutorial を順に実行してください。
- APIリファレンス(APIリファレンス)を探索してください。