螺旋 / roadrunner-bridge
RoadRunner集成包
Requires
- php: >=8.1
- grpc/grpc: ^1.57
- psr/http-factory: ^1.1
- psr/simple-cache: ^3.0
- roadrunner-php/app-logger: ^1.0
- roadrunner-php/centrifugo: ^2.0
- roadrunner-php/lock: ^1.0
- spiral/grpc-client: ^1.0
- spiral/hmvc: ^3.14
- spiral/roadrunner-grpc: ^3.3
- spiral/roadrunner-http: ^3.5
- spiral/roadrunner-jobs: ^4.4
- spiral/roadrunner-kv: ^4.0
- spiral/roadrunner-metrics: ^3.0
- spiral/roadrunner-tcp: ^3.1 || ^4.0
- spiral/scaffolder: ^3.13
- spiral/serializer: ^3.13
Requires (Dev)
- buggregator/trap: ^1.10
- internal/dload: ^1.0.0
- phpunit/phpunit: ^10.5
- spiral/framework: ^3.14
- spiral/nyholm-bridge: ^1.3
- spiral/roadrunner-cli: ^2.6
- spiral/testing: ^2.8
- vimeo/psalm: ^5.25
Suggests
- ext-protobuf: For better performance, install the protobuf C extension.
- 4.x-dev
- 4.0.x-dev
- v4.0.0-RC1
- v4.0.0-alpha2
- v4.0.0-alpha
- 3.x-dev
- v3.7.0
- v3.6.2
- v3.6.1
- v3.6.0
- v3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.x-dev
- 2.6.0
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 2.0-beta2
- 2.0-beta1
- 2.0-beta
- 1.x-dev
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.0
- dev-feature/grpc-tokenizer
- dev-feature/rr-services
This package is auto-updated.
Last update: 2024-09-10 19:01:56 UTC
README
要求
请确保您的服务器已配置以下PHP版本和扩展
- PHP 8.1+
- Spiral框架 3.7+
安装
要安装此包
composer require spiral/roadrunner-bridge
在包安装后,您需要在您的应用程序顶部列表中添加包中的引导加载器。
use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge; protected const LOAD = [ RoadRunnerBridge\HttpBootloader::class, // Optional, if it needs to work with http plugin RoadRunnerBridge\QueueBootloader::class, // Optional, if it needs to work with jobs plugin RoadRunnerBridge\CacheBootloader::class, // Optional, if it needs to work with KV plugin RoadRunnerBridge\GRPCBootloader::class, // Optional, if it needs to work with GRPC plugin RoadRunnerBridge\CentrifugoBootloader::class, // Optional, if it needs to work with centrifugo server RoadRunnerBridge\TcpBootloader::class, // Optional, if it needs to work with TCP plugin RoadRunnerBridge\MetricsBootloader::class, // Optional, if it needs to work with metrics plugin RoadRunnerBridge\LoggerBootloader::class, // Optional, if it needs to work with app-logger plugin RoadRunnerBridge\LockBootloader::class, // Optional, if it needs to work with lock plugin RoadRunnerBridge\ScaffolderBootloader::class, // Optional, to generate Centrifugo handlers and TCP services via Scaffolder RoadRunnerBridge\CommandBootloader::class, // ... ];
用法
TC
RoadRunner包含TCP服务器,可以用于用更高的性能和灵活性替换经典的TCP设置。
引导加载器
将Spiral\RoadRunnerBridge\Bootloader\TcpBootloader
添加到应用程序引导加载器列表中
use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge; protected const LOAD = [ // ... RoadRunnerBridge\TcpBootloader::class, // ... ];
此引导加载器为TCP工作添加了分发器和必要的服务。此外,使用addService
和addInterceptors
方法可以动态地将服务添加到TCP服务器并配置拦截器。
配置
在RoadRunner的.rr.yaml
配置文件中的tcp
部分配置所需的TCP服务器。示例
tcp: servers: smtp: addr: tcp://127.0.0.1:22 delimiter: "\r\n" # by default monolog: addr: tcp://127.0.0.1:9913 pool: num_workers: 2 max_jobs: 0 allocate_timeout: 60s destroy_timeout: 60s
创建配置文件app/config/tcp.php
。在配置中,需要指定将处理来自特定TCP服务器的请求的服务。可选地,可以为每个特定服务器添加拦截器。借助这些拦截器,可以在服务中处理请求之前添加一些逻辑。配置示例
<?php declare(strict_types=1); return [ /** * Services for each server. */ 'services' => [ 'smtp' => SomeService::class, 'monolog' => OtherService::class ], /** * Interceptors, this section is optional. * @see https://spiral.dev/docs/cookbook-domain-core/2.8/en#core-interceptors */ 'interceptors' => [ // several interceptors 'smtp' => [ SomeInterceptor::class, OtherInterceptor::class ], 'monolog' => SomeInterceptor::class // one interceptor ], 'debug' => env('TCP_DEBUG', false) ];
服务
服务必须实现接口Spiral\RoadRunnerBridge\Tcp\Service\ServiceInterface
,该接口有一个必需的方法handle
。在处理请求后,必须使用包含结果的handle
方法返回Spiral\RoadRunnerBridge\Tcp\Response\ResponseInterface
对象(RespondMessage
、CloseConnection
、ContinueRead
)。
示例
<?php declare(strict_types=1); namespace App\Tcp\Service; use Spiral\RoadRunner\Tcp\Request; use Spiral\RoadRunnerBridge\Tcp\Response\RespondMessage; use Spiral\RoadRunnerBridge\Tcp\Response\ResponseInterface; use Spiral\RoadRunnerBridge\Tcp\Service\ServiceInterface; class TestService implements ServiceInterface { public function handle(Request $request): ResponseInterface { // some logic return new RespondMessage('some message', true); } }
可以使用Scaffolder组件生成服务。请确保已将引导加载器Spiral\RoadRunnerBridge\Bootloader\ScaffolderBootloader
添加到您的应用程序,并运行
php app.php create:tcp-service Test
这将生成文件夹Endpoint/Tcp/Service/TestService.php
中的服务TestService
。
注意命名空间(以及生成路径)可以进行配置。有关Scaffolder组件的更多信息。
注意有关RoadRunner配置的更多信息,请参阅官方网站https://roadrunner.dev。
许可证
MIT许可证(MIT)。有关更多信息,请参阅LICENSE
文件LICENSE
。由Spiral Scout维护。