The GRPC protocol provides an extremely efficient way of cross-service communication for distributed applications. The public toolkit includes instruments to generate client and server code-bases for many languages allowing developers to use the most optimal language for their task.
The default GRPC build includes a pre-installed version of the spiral/php-grpc library.
Note
You will need to generate an application key and certificate to make the GRPC bundle work, see below how to do that.
You can read more about protobuf here.
It is possible to run a PHP application without any dependencies out of the box. However, to develop, debug, and extend the GRPC project, you will need several instruments.
To compile .proto
files into the target language, you will have to install the protoc
compiler.
You can download the latest protoc
binaries
from https://github.com/protocolbuffers/protobuf/releases.
Download and install protoc-gen-php-grpc
from roadrunner-server/roadrunner releases page.
This plugin is required to generate service code for your applications.
Note
Make sure that the plugin is available in your PATH.
To achieve better performance with larger messages, make sure to install the protobuf
extension for PHP.
You can compile the extension manually or install it via PECL.
sudo pecl install protobuf
Note
In case of theSegmentation Fault
error, try to install anotherprotobuf
library. We recommend using3.10.0
at the start.
sudo pecl install protobuf-3.10.0
To install the component in alternative bundles:
composer require spiral/roadrunner-bridge
Activate the component using bootloader Spiral\RoadRunnerBridge\Bootloader\GRPCBootloader
:
protected const LOAD = [
// ...
\Spiral\RoadRunnerBridge\Bootloader\GRPCBootloader::class,
// ...
];
Create the config file app/config/grpc.php
if you want to configure generate service classes:
<?php
declare(strict_types=1);
return [
/**
* Path to protoc-gen-php-grpc library.
* Default: null
*/
'binaryPath' => null,
// 'binaryPath' => __DIR__.'/../../protoc-gen-php-grpc',
'services' => [
__DIR__.'/../../proto/echo.proto',
],
];
To enable the component in the application server, add the following configuration section:
grpc:
listen: tcp://0.0.0.0:50051
workers.command: "php app.php"
# read how to write proto files in the next section
proto: "proto/service.proto"
Note
Full documentation about configuration the RoadRunnergrpc
plugin here.
It is possible to run GRPC without any encryption layer. However, to secure our application, we must issue the proper server key and certificate. You can use any regular SSL certificate (for example, one issued by https://letsencrypt.org/) or issue it manually via OpenSSL.
To issue a server key and certificate:
openssl req -newkey rsa:2048 -nodes -keyout app.key -x509 -days 365 -out app.crt
Note
Make sure to use a proper domain name orlocalhost
, it will be required to make your clients connect properly.
You can install the app-grpc
skeleton application to play with the GRPC services:
composer create-project spiral/app-grpc
cd app-grpc
openssl req -newkey rsa:2048 -nodes -keyout app.key -x509 -days 365 -out app.crt
Start the application:
./rr serve