playtini/grpc-server-bundle

Create grpc server using spriral/php-grpc

1.0.0 2021-07-30 11:18 UTC

This package is auto-updated.

Last update: 2024-04-10 15:51:54 UTC


README

Create gRPC server using spriral/php-grpc

Installation

Step 1: Download the bundle

$ composer require playtini/grpc-server-bundle

Step 2: Copy server binary to ./project/bin folder

./vendor/playtini/grpc-server-bundle/bin/rr-grpc ./project/bin/

Step 3: Create server config .rr.yaml in project root folder:

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

Usage example

Create *.proto

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

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

Create server side code

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

Help

spriral/php-grpc - high-performance PHP GRPC server build at top of RoadRunner.

Related articles and source