Create gRPC server using spriral/php-grpc
$ composer require playtini/grpc-server-bundle
./vendor/playtini/grpc-server-bundle/bin/rr-grpc ./project/bin/
grpc:
listen: "tcp://:6096"
proto: "./proto/base.proto"
workers:
command: "./bin/console roadrunner:grpc-worker"
relay: "unix://var/roadrunner.sock"
pool:
numWorkers: 1
limit:
interval: 1
services:
grpc:
maxMemory: 100
TTL: 0
idleTTL: 0
execTTL: 60
base.proto
syntax="proto3";
package playtini;
option php_generic_services = true;
option php_namespace = "Playtini\\MainServiceName";
option php_metadata_namespace = "Playtini\\MainServiceName\\Meta";
import 'calculator.proto';
calculator.proto
syntax = "proto3";
package playtini.calculator;
option php_namespace = "Playtini\\MainServiceName\\Calculator";
option php_metadata_namespace = "Playtini\\MainServiceName\\Meta";
message Sum {
int32 a = 1;
int32 b = 2;
}
message Result {
int32 result = 1;
}
service Calculator {
rpc Sum (calculator.Sum) returns (calculator.Result);
}
Generate service using protoc
and protoc-gen-grpc
plugin(roadrunner custom plugin):
protoc /proto/base.proto \
-I/proto -I/proto/base.proto proto/calculator.proto \
--php_out=/proto/src \
$(: 👇 custom plugin from roadrunner to generate server interface) \
--php-grpc_out=/proto/src \
$(: 👇 generates the client code) \
--grpc_out=/proto/src \
--plugin=protoc-gen-grpc=/protobuf/grpc/bins/opt/grpc_php_plugin \
--proto_path /proto
Implement generated interface:
<?php
namespace App\Calculator;
use Playtini\MainServiceName\Calculator\CalculatorInterface;
use Playtini\MainServiceName\Calculator\Result;
use Playtini\MainServiceName\Calculator\Sum;
use Spiral\GRPC;
class CalculatorService implements CalculatorInterface
{
public function Sum(GRPC\ContextInterface $ctx, Sum $in): Result
{
return new Result([
'result' => $in->getA() + $in->getB(),
]);
}
}
Add to service.yaml
:
services:
App\Calculator\CalculatorService:
tags: ['playtini.roadrunner.grpc_service']
Run the server:
bin/rr-grpc serve -v -d
spriral/php-grpc - high-performance PHP GRPC server build at top of RoadRunner.
- Building gRPC server
- RoadRunner gRPC
- roadrunner-bundle - thank
vsychov
for cool pool request that add gRPC server support