mosamirzz/octane-grpc
Composer 安装命令:
composer require mosamirzz/octane-grpc
包简介
Support gRPC server for laravel octane
关键字:
README 文档
README
Add support for gRPC server in laravel octane.
Installation
You can install the package via composer:
composer require mosamirzz/octane-grpc
Usage
- add the following at the end of your octane.php config file.
return [ // ... /* |-------------------------------------------------------------------------- | gRPC Server |-------------------------------------------------------------------------- | | The following setting used by the swoole gRPC server. | */ 'grpc' => [ 'host' => '0.0.0.0', 'port' => 50051, 'mode' => OpenSwoole\Http\Server::POOL_MODE, 'services_path' => base_path('routes/grpc.php'), ], ];
- create the routes file that will contains all the services that needs to be registered in the server.
touch routes/grpc.php
and the content of the file will be like:
<?php use Proto\Greeter\GreeterService; return [ // GreeterService::class => new GreeterService(), ];
- add the following to composer.json:
{
// ...
"autoload": {
"psr-4": {
// ...
"Proto\\": "Proto/"
}
}
// ...
}
then run this command
composer dump-autoload -o
- run the server
sail artisan octane:swoole-grpc
requirements
openswoolephp extensionprotocprotoc-gen-openswoole-grpcplugin (to generate grpc serevr stubs)protoc-gen-php-grpcplugin (to generate grpc client stubs)
- download
openswooleextension.
# first: install needed libs sudo apt-get install -y libcurl4-openssl-dev openssl libssl-dev libpcre3-dev build-essential php8.3-common # download & install the extension git clone https://github.com/openswoole/ext-openswoole.git cd ext-openswoole git checkout v22.1.2 # choose the version you want phpize ./configure --enable-openssl --enable-http2 --enable-hook-curl sudo make && sudo make install # create openswoole.ini && write the following in the file sudo echo "; priority=20" >> /etc/php/8.3/mods-available/openswoole.ini sudo echo "extension=openswoole.so" >> /etc/php/8.3/mods-available/openswoole.ini # enable the extension sudo phpenmod openswoole # verify it is installed php -m | grep openswoole
- download
protoc
you can install the release you want from this page: Protobuf releases
# download the zip file curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v29.0-rc1/protoc-29.0-rc-1-linux-x86_64.zip # extract it unzip protoc-29.0-rc-1-linux-x86_64.zip -d protoc # move the binary to the /usr/bin directory to be used from anywhere sudo mv protoc/bin/protoc /usr/bin # remove unwanted files rm protoc-29.0-rc-1-linux-x86_64.zip rm -r protoc
- download the
protoc-gen-openswoole-grpc
you can install the release you want from this page: OpenSwoole plugin releases
# download the zip file curl -LO https://github.com/openswoole/protoc-gen-openswoole-grpc/releases/download/0.1.1/protoc-gen-openswoole-grpc-0.1.1-linux-amd64.tar.gz # extract it mkdir openswoole && tar -xvzf protoc-gen-openswoole-grpc-0.1.1-linux-amd64.tar.gz -C openswoole # move the binary to the /usr/bin directory to be used from anywhere sudo mv openswoole/protoc-gen-openswoole-grpc /usr/bin # remove unwanted files rm protoc-gen-openswoole-grpc-0.1.1-linux-amd64.tar.gz rm -r openswoole
- download the
protoc-gen-php-grpc
# you will clone the grpc repository but you must replace the RELEASE_TAG_HERE with the tag version you want to install git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc # e.g. download tag v1.67.0 git clone -b v1.67.0 https://github.com/grpc/grpc cd grpc git submodule update --init mkdir -p cmake/build cd cmake/build cmake ../.. make protoc grpc_php_plugin # now mv the grpc_php_plugin into /usr/bin but rename it to protoc-gen-php-grpc sudo mv grpc_php_plugin /usr/bin/protoc-gen-php-grpc # you can remove unwanted directories like above
- install protobuf compiler
sudo apt install protobuf-compiler
server example
- Create a
Proto/greeter.proto
syntax = "proto3"; package greeter; option php_namespace = "Proto\\Greeter"; option php_metadata_namespace = "Proto\\Greeter\\Metadata"; service Greeter { rpc SayHello(HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
- generate the code for the server
protoc --php_out=. --openswoole-grpc_out=. Proto/greeter.proto
the command above will generate the following in your currenct directory
Proto/
└── Greeter/
├── Metadata/
│ └── Greeter.php
├── GreeterClient.php
├── GreeterInterface.php
├── GreeterService.php
├── HelloReply.php
└── HelloRequest.php
- write the service implementation
you will need to write the business login inside the generated service GreeterService.php it will be like the following:
<?php declare(strict_types=1); namespace Proto\Greeter; use OpenSwoole\GRPC; class GreeterService implements GreeterInterface { /** * @param GRPC\ContextInterface $ctx * @param HelloRequest $request * @return HelloReply * * @throws GRPC\Exception\InvokeException */ public function SayHello(GRPC\ContextInterface $ctx, HelloRequest $request): HelloReply { // 1. get the request data $name = $request->getName(); // 2. write any business logic here $message = "Hello " . $name; // 3. build the response object $response = new HelloReply(); $response->setMessage($message); // 4. return the response return $response; } }
- now register the
GreeterServicein the gRPC server.
open routes/grpc.php and add the following:
<?php use Proto\Greeter\GreeterService; return [ GreeterService::class => new GreeterService(), ];
- run the gRPC server
sail artisan octane:swoole-grpc
client example
first of all you need to install grpc extension
you can download it with
sudo apt-get install php8.3-grpc # or pecl install grpc # then add grpc.so into php.ini file
- install
grpc/grpcpackage
composer require grpc/grpc
- generate the php code from
greeter.protofile for the client.
protoc --php_out=. --php-grpc_out=. Proto/greeter.proto
it will generate the files like:
Proto/
└── Greeter/
├── Metadata/
│ └── Greeter.php
├── GreeterClient.php
├── HelloReply.php
└── HelloRequest.php
- create a
client.phpfile
<?php use Proto\Greeter\GreeterClient; use Proto\Greeter\HelloRequest; require __DIR__ . '/vendor/autoload.php'; // connect to the Greeter service $client = new GreeterClient("0.0.0.0:50051", [ 'credentials' => Grpc\ChannelCredentials::createInsecure(), ]); // build the request data $request = new HelloRequest(); $request->setName("Mohamed Samir"); // send the request and wait for the response /** @var Proto\Greeter\HelloReply $response */ [$response, $status] = $client->SayHello($request)->wait(); // check the status code of the response and get the error details if ($status->code !== Grpc\STATUS_OK) { echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL; exit(1); } // do anything with the response var_dump($response->getMessage());
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email gm.mohamedsamir@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
mosamirzz/octane-grpc 适用场景与选型建议
mosamirzz/octane-grpc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.08k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mosamirzz」 「octane-grpc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mosamirzz/octane-grpc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mosamirzz/octane-grpc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mosamirzz/octane-grpc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 2.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-13