binhtv / grpc-base-laravel
该软件包帮助您更容易地实现gRPC
v1.0.6
2022-07-27 09:31 UTC
Requires
- php: ^7.4
- google/common-protos: ^2.0.0
- google/protobuf: ^3.19
- grpc/grpc: ^1.42
- spiral/php-grpc: ^1.6
- spiral/roadrunner-laravel: ^3.7
This package is not auto-updated.
Last update: 2024-09-18 20:30:30 UTC
README
关于软件包
这是程序员sun*的作品。
此软件包包含一些类和方法,可以帮助用户更轻松地实现Grpc到Laravel的集成。因此,通过使用Grpc可以提升系统性能。
使用方法
composer require binhtv/grpc-base-laravel
然后运行命令以发布配置。
php artisan vendor:publish --tag=binhtv-grpc-config
您需要了解如何使用php编译.proto文件。
客户端
- 自动加载生成的proto文件
"autoload": { "psr-4": { ... "": "protos/generated/" }, ... },
- 快速调用服务器(推荐使用)
步骤1:创建一个继承自BaseGrpcApi的类;
<?php namespace App\Services\MicroserviceGrpc; use binhtv\GrpcLaravel\Client\Contracts\BaseGrpcApi; use Google\Protobuf\Internal\Message; use Illuminate\Http\Request; use Protobuf\Company\ExampleServiceClient; /** * @method Message ExampleMethod(array|Request $request) */ class ExampleGrpcClient extends BaseGrpcApi { public function grpcClient(): string { return ExampleServiceClient::class; } }
步骤2:在该类中调用方法;
<?php ... // ExampleMethod is a method of ExampleServiceClient; (new ExampleGrpcClient())->ExampleMethod($request); ...
- 或创建一个新的Grpc客户端;
$clientGrpc = (new GrpcFactory)->make(ExampleServiceClient::class);
- 使用特性;
... use binhtv\GrpcLaravel\Client\Traits\HandleDataRequest; ... class ExampleController extends Controller { use HandleDataRequest; }
- 方法
服务
- 开始服务
./vendor/binhtv/grpc-base-laravel/rr-grpc serve -v -d
- 示例工作文件
<?php declare(strict_types=1); use App\Grpc\ExampleGrpcController; use Spiral\Goridge\StreamRelay; use Spiral\RoadRunner\Worker; ini_set('display_errors', 'stderr'); require __DIR__.'/vendor/autoload.php'; $app = require_once __DIR__.'/bootstrap/app.php'; $app->singleton( binhtv\GrpcLaravel\Server\Contracts\Kernel::class, binhtv\GrpcLaravel\Server\Kernel::class ); $app->singleton( binhtv\GrpcLaravel\Server\Contracts\ServiceInvoker::class, binhtv\GrpcLaravel\Server\LaravelServiceInvoker::class ); $kernel = $app->make(binhtv\GrpcLaravel\Server\Kernel::class); $kernel->registerService(ExampleGrpcController::class); $w = new Worker(new StreamRelay(STDIN, STDOUT)); $kernel->serve($w);