Revision: Wed, 27 Nov 2024 11:25:20 GMT
v3.13 – outdated
This version of the documentation is outdated. Consider upgrading your project to Spiral Framework 3.14
Edit this page

GRPC — Getting started

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.

See more
You can read more about protobuf here.

Toolkit Installation

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.

Install Protoc

To compile .proto files into the target language, you will have to install the protoc compiler.

Note
You can download the latest protoc binaries from here.

Install Protobuf extension (optional)

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 3.10.0 at the start.

sudo pecl install protobuf-3.10.0

Package Installation

The spiral/roadrunner-bridge package allows you to use RoadRunner gRPC plugin with Spiral. This package provides tools to generate proto files, client code, and a bootloader for your application.

At first, you need to install the spiral/roadrunner-bridge package.

Once the package is installed, activate the component using bootloader Spiral\RoadRunnerBridge\Bootloader\GRPCBootloader:

php
app/src/Application/Kernel.php
public function defineBootloaders(): array
{
    return [
        // ...
        \Spiral\RoadRunnerBridge\Bootloader\GRPCBootloader::class,
        \Spiral\RoadRunnerBridge\Bootloader\CommandBootloader::class,
        // ...
    ];
}

Read more about bootloaders in the Framework — Bootloaders section.

After the package installed you will need to download the protoc-gen-php-grpc. It is a plugin for the protoc tool that generates PHP code for gRPC services.

Note
To download the binary, you can use download-protoc-binary console command provided by the spiral/roadrunner-cli package. This command will download the latest version of the protoc-gen-php-grpc binary and save it to the root directory of your project.

./vendor/bin/rr download-protoc-binary

Configuration

Create the config file app/config/grpc.php if you want to configure generate service classes:

php
app/config/grpc.php
return [
    /**
     * The path where generated DTO (Data Transfer Object) files will be stored.
     */
    'generatedPath' => directory('root') . '/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',
    ],
];

Application Server

To enable the component in the RoadRunner application server, add the following configuration section:

yaml
.rr.yaml
server:
  command: "php app.php"

grpc:
  # GRPC address to listen
  listen: "tcp://0.0.0.0:9001"
  proto:
    - "proto/calculator.proto"

Note
Full documentation about configuration the RoadRunner gRPC plugin here.

Example Application

There is a good example Demo ticket booking system application built on Spiral, 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 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.

Happy (fake) ticket shopping!

Generate Certificate

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 or localhost, it will be required to make your clients connect properly.