Download the PHP package vandarpay/laravel-grpc without Composer
On this page you can find all versions of the php package vandarpay/laravel-grpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-grpc
Laravel Grpc
This package is prepared for implementing GRPC
on the server side and client side in Laravel to implement the
microservice structure.
Please follow the steps below to set up the desired section, Also, at the end of the page, examples are given that will give you a better understanding
Installation
Publish Necessary File
Requirement
- PHP 8.1
- Install
protobuf-ext
. - Install
grpc-ext
.
Server Side
This package use RoadRunner for manage process and implement grpc server, RoadRunner is a high-performance PHP application server, load-balancer, and process manager written in Golang.
Installation RoadRunner Binary
You can also install RoadRunner automatically using command shipped with the composer package, run:
Server binary will be available at the root of your project.
PHP's extensions php-curl and php-zip are required to download RoadRunner automatically. PHP's extensions php-sockets need to be installed to run roadrunner. Check with php --modules your installed extensions.
Environments
Please add this environment variable to .env
file
#Available value : panic, error, warn, info, debug. Default: debug
ROAD_RUNNER_LOG_LEVEL=debug
GRPC_XDEBUG=0 # 1,0
GRPC_SERVER=tcp://127.0.0.1:6001
GRPC_WORKER_MAX_JOBS=0
GRPC_WORKER_NUM_WORKERS=2
-
GRPC_XDEBUG :
To activate xDebug make sure to set the xdebug.mode=debug
in your php.ini
.
To enable xDebug in your application make sure to set ENV
variable GRPC_XDEBUG
-
GRPC_SERVER :
Consider the address you want for the GRPC server
-
GRPC_WORKER_MAX_JOBS :
Maximal count of worker executions. Zero (or nothing) means no limit.(Default: 0)
-
GRPC_WORKER_NUM_WORKERS :
How many worker processes will be started. Zero (or nothing) means the number of logical CPUs.(Default : 0)
Make New Service
Compile proto files
To start, create a proto file named echo.proto
in path app/Protobuf
and copy the following content into it, if this
folder does not exist, create it
Note that the following 3 lines of this file should not be changed
Install protobuf compiler
Next, after installing protoc
program, execute the following command to create the required files for connection
After executing command protoc
, a folder named GrpcServices
is created in path app/Protobuf
, which contains
messages and the main service class
Create First Method
Add this line to created repository
And then in the related service, add the additional method in the repository and put its output according to the
definitions of the proto
file.
Register Proto File In RoadRunner
The path of file proto created in the previous step should be added in file .rr.yaml
in the following way
Register Service To GRPC Server
To register the service created in method register
, class App\Providers\GrpcServiceProvider.php
, register your
service as below
Run RoadRunner Sever
Client Side
To build the communication class on the client side, first, the proto file must be compiled like the server routine, and the communication class must be created by the following command.
The proto file should be in app/Protobuf
path. And after executing the above command, folder Clients
will be created
in path app/Protobuf
.
After this, the communication settings of the defined service should be placed in the config file grpc.php
Note: The index of this setting must be equal to the service name
In this step, transfer the proto file from the server side to the client and create the required files with the proto compiler.
Below is an example to better understand the GRPC client class
Versioning On Service
Of course, in the development of a service, there are times when there is a need to upgrade the old version, and this part is fully supported in this package. For this purpose, after considering the necessary folders for the service.
In this case, the folder structure changes as follows
All versions of laravel-grpc with dependencies
vandarpay/service-repository Version ^0.1
ext-protobuf Version *
ext-grpc Version *
spiral/roadrunner Version ^2.11
spiral/roadrunner-laravel Version ^5.9
spiral/roadrunner-grpc Version ^2.0
nyholm/psr7 Version ^1.5
google/common-protos Version ^3.0
grpc/grpc Version ^1.36
google/protobuf Version ^3.11