gRPC

RoadRunner gRPC plugin enables PHP applications to communicate with gRPC clients.

It consists of two main parts:

  1. protoc-plugin protoc-gen-php-grpc: This is a plugin for the protoc compiler that generates PHP code from a gRPC service definition file (.proto). It generates PHP classes that correspond to the service definition and message types. These classes provide an interface for handling incoming gRPC requests and sending responses back to the client.

  2. gRPC server: This is a server that starts PHP workers and listens for incoming gRPC requests. It receives requests from gRPC clients, proxies them to the PHP workers, and sends the responses back to the client. The server is responsible for managing the lifecycle of the PHP workers and ensuring that they are available to handle requests.

Protoc-plugin

The first step is to define a .proto file that describes the gRPC service and messages that your PHP application will handle.

In our documentation, we will use the following example of a .proto file that is stored in the <app>/proto directory:

proto/helloworld.proto
syntax = "proto3";

option go_package = "proto/greeter";
option php_namespace = "GRPC\\Greeter";
option php_metadata_namespace = "GRPC\\GPBMetadata";

package helloworld;

// 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;
}

It defines a simple gRPC service called Greeter that takes a name as input and returns a greeting.

The php_namespace and php_metadata_namespace options allow you to specify the namespaces to use in the generated DTO and Service interface.

Generating PHP code

After defining the proto file, you need to generate the PHP files using the protoc compiler and the protoc-gen-php-grpc plugin. You can install the plugin binary using Composer or download a pre-built binary from the GitHub releases page.

The simplest way to get the latest version of protoc-gen-php-grpc plugin is to download one of the pre-built release binaries on the GitHub releases page.

Just download the appropriate archive from the release page and extract it into your desired application directory.

Once the plugin is installed, you can use the protoc command to compile the proto file into PHP files.

Here's an example command:

Make sure that the generated directory exists and is writable.

After running the command, you can find the generated DTO and GreeterInterface files in the <app>/generated/GRPC/Greeter directory.

We recommend also registering the GRPC namespace in the composer.json file:

By doing this, you can easily use the generated PHP classes in your application.

Using BUF plugin

You can also use the BUF RoadRunner gRPC plugin to generate PHP code from the .proto file instead of using the protoc compiler.

To use BUF to generate PHP gRPC services, you'll need to create two files: buf.yaml and buf.gen.yaml.

Here's an example of a buf.yaml file:

Note that you need to optionally replace <your_org_name> and <your_project_name> with your organization and project names created on the BUF website.

In the deps section, you can specify the dependencies that your .proto file relies on. In this example, we're using a dependency from the Google APIs repository.

Also, you may configure the linting and breaking changes rules.

Here's an example of a buf.gen.yaml file:

As you can see, the buf.gen.yaml file specifies the plugins that will be used to generate the code. In this example, we're using the buf.build/community/roadrunner-server-php-grpc plugin to generate PHP gRPC services.

Also, you may generate code for other languages like Go and Python.

PHP Client

The RoadRunner gRPC plugin comes with a convenient PHP package that simplifies the process of integrating the plugin with your PHP application.

Installation

You can install the package via Composer using the following command:

Implement Service

Next, you will need to create a PHP class that implements the Greeter service defined in the .proto file. This class should implement the GRPC/Greeter/GreeterInterface.

Here's an example:

Usage

To use the Greeter service, you can create a PHP worker that registers the service with the gRPC server.

Here's an example of how to do this:

After creating the worker, you need to configure RoadRunner to register the proto/helloworld.proto service.

Here's an example configuration:

You can define command to start server in the server.command section. It will be used to start PHP workers for all registered plugins, such as grpc, http, jobs, etc.

You can define multiple proto files in the proto section.

After configuring the server, you can start it using the following command:

This will start the gRPC server and make the Greeter service available for remote clients to call. You can use any gRPC client library in any language that supports gRPC to call the SayHello method.

To quickly verify your code's correctness, you can install grpc-client-cli which provides an interactive CLI tool for talking to gRPC services.

To access the Greeter service with grpc-client-cli run the following in a terminal:

grpc-client-cli --proto proto/helloworld.proto 127.0.0.1:9001

It will provide some interactive prompts asking you what service and method you want to call

You will then be prompted to manually form the JSON payload it expects and upon pressing enter, your Greeter service should respond in an amicable manner:

On the PHP side, you should see that the successful request has been logged:

Congratulations on being able to communicate over gRPC to PHP using Roadrunner!

Metrics

RoadRunner has a metrics plugin that provides metrics for the gRPC server, which can be used with Prometheus and a preconfigured Grafana dashboard

grpc-metrics

mTLS

To enable mTLS use the following configuration:

Options for the client_auth_type are:

  • request_client_cert

  • require_any_client_cert

  • verify_client_cert_if_given

  • require_and_verify_client_cert

  • no_client_certs

Full example of Configuration

OTLP support in the gRPC plugin: [>=2023.3.8]

In the v2023.3.8 we added experimental support for the OTLP protocol in the gRPC plugin. Stable since v2024.1.1. To enable it, you need to activate otel plugin by adding the following lines to the .rr.yaml file:

Trace keys passed to the PHP workers are:

  1. Traceparent

  2. Uber-Trace-Id

Example:

More about OTLP plugin you may read here.

Common issues

  1. Registering two services with the same name is not allowed. GRPC server will panic after that.

Last updated

Was this helpful?