Using REST APIs for communication between microservices has been a common approach for many years. However, REST APIs have some limitations and challenges that can impact the performance, scalability, and reliability of your system.
Switching from REST APIs to gRPC can help address these problems. 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.
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.
.proto files into the target language, you will have to install the
You can download the latest
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
In case of the
Segmentation Fault error, try to install another
protobuf library. We recommend using
sudo pecl install protobuf-3.10.0
spiral/roadrunner-bridge package allows you to use RoadRunner
gRPC plugin with the Spiral Framework. This package provides
tools to generate proto files, client code, and a bootloader for your application.
To install the
spiral/roadrunner-bridge package, use the following command:
composer require spiral/roadrunner-bridge
Activate the component using bootloader
protected const LOAD = [ // ... \Spiral\RoadRunnerBridge\Bootloader\GRPCBootloader::class, \Spiral\RoadRunnerBridge\Bootloader\CommandBootloader::class, // ... ];
After the package installed you will need to download the
protoc-gen-php-grpc. It is a plugin for the
that generates PHP code for gRPC services.
To download the binary, you can use
download-protoc-binaryconsole command provided by the
spiral/roadrunner-clipackage. This command will download the latest version of the
protoc-gen-php-grpcbinary and save it to the root directory of your project.
Create the config file
app/config/grpc.php if you want to configure generate service classes:
<?php declare(strict_types=1); return [ /** * The path where generated DTO (Data Transfer Object) files will be stored. */ 'generatedPath' => directory('app') . '/GRPC', /** * The base namespace for the generated proto files. */ 'namespace' => '\App\GRPC', /** * The root dir for all proto files, where imports will be searched. */ 'servicesBasePath' => directory('root') . '/proto', /** * The path to the protoc-gen-php-grpc library. */ 'binaryPath' => directory('root').'/bin/protoc-gen-php-grpc', /** * An array of paths to proto files that should be compiled into PHP by the grpc:generate console command. */ 'services' => [ // directory('root').'proto/calculator.proto', ], ];
To enable the component in the RoadRunner application server, add the following configuration section:
version: "2.7" server: command: "php app.php" grpc: # GRPC address to listen listen: "tcp://0.0.0.0:9001" proto: - "proto/calculator.proto"
Full documentation about configuration the RoadRunner gRPC plugin here.
There is a good example Demo ticket booking system application built on the Spiral Framework, which is a high-performance PHP framework that follows the principles of microservices and allows developers to create reusable, independent, and easy-to-maintain components.
In this demo application, you can find an example of using RoadRunner's gRPC plugin to create and consume gRPC services.
Overall, our demo ticket booking system is a great example of how Spiral Framework and other tools can be used to build a modern and efficient application. We hope you have a blast using it and learning more about the capabilities of Spiral Framework and the other tools we've used.
Happy (fake) ticket shopping!
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
Make sure to use a proper domain name or
localhost, it will be required to make your clients connect properly.