andrey_mireichyk / roadrunner-bundle
作为 Symfony Bundle 的 RoadRunner 工作者
Requires
- php: >=7.3
- dflydev/fig-cookies: ^2.0
- php-http/discovery: ^1.9
- psr/http-factory: ^1.0
- psr/http-factory-implementation: 1.0.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^1.1
- spiral/goridge: ^2.0.2
- spiral/roadrunner: ^1.5
- symfony/config: ^4.4 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.0
- symfony/http-kernel: ^4.4 || ^5.0
- symfony/psr-http-message-bridge: ^1.1 || ^2.0
- symfony/yaml: ^4.4 || ^5.0
Requires (Dev)
- blackfire/php-sdk: ^1.21
- doctrine/doctrine-bundle: ^2.0
- doctrine/mongodb-odm-bundle: ^4.1
- doctrine/orm: ^2.7.3
- friendsofphp/php-cs-fixer: ^2.16
- nyholm/psr7: ^1.2
- ocramius/package-versions: ^1.10 || ^2.0
- phpspec/prophecy: ^1.11
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12.2
- phpunit/phpunit: ^9.1
- sentry/sentry-symfony: ^3.4
- symfony/framework-bundle: ^4.0||^5.0
- symfony/proxy-manager-bridge: ^4.0 || ^5.0
- symfony/var-dumper: ^4.0||^5.0
Suggests
- nyholm/psr7: For a super lightweight PSR-7/17 implementation
- symfony/proxy-manager-bridge: For doctrine re-connection implementation
This package is not auto-updated.
Last update: 2024-09-21 07:09:10 UTC
README
RoadRunner 是一个使用 Golang 编写的性能高效的 PHP 应用程序服务器、负载均衡器和进程管理器。
此扩展包提供集成的 RoadRunner Worker,易于配置和扩展。
安装
运行以下命令
composer require baldinof/roadrunner-bundle
如果您不使用 Symfony Flex
- 在您的内核中注册
Baldinof\RoadRunnerBundle\BaldinofRoadRunnerBundle
- 复制默认的 RoadRunner 配置文件:
cp vendor/baldinof/roadrunner-bundle/.rr.* .
使用方法
- 获取 RoadRunner 二进制文件:
vendor/bin/rr get --location bin/
- 使用
bin/rr serve
运行 RoadRunner - 在 https://:8080 访问您的应用程序
配置
如果您想覆盖扩展包的一些部分,可以替换一些定义。
示例:如果您想使用 TCP 套接字作为中继
# config/services.yaml services: Spiral\Goridge\RelayInterface: class: 'Spiral\Goridge\SocketRelay' arguments: - localhost - 7000
# .rr.yaml http: workers: relay: "tcp://:7000"
集成
根据已安装的扩展包和您的配置,此扩展包添加了一些集成
- Sentry:配置请求上下文(如果已安装
SentryBundle
) - 会话:将会话 cookie 添加到 PSR 响应(如果
framework.sessions.enabled
配置为true
) - Doctrine Mongo Bundle:在每次请求后调用所有打开的管理器的
clear()
(对于常规 doctrine 扩展包不需要)
默认集成可以禁用
baldinof_road_runner: default_integrations: false
Doctrine 连接处理器
由于 RoadRunner 假设进程在演示模式下运行,因此断开数据库连接可能会出现问题,如果使用 doctrine 连接到数据库,此扩展包将处理此问题。
默认情况下,在连接失败的情况下,进程将被停止,请求将结束于 500 http 状态码,为了防止这种情况并正确处理失败的连接,而不产生错误的响应 - 您需要将 symfony/proxy-manager-bridge 添加到您的项目中
composer require symfony/proxy-manager-bridge
数据库中的会话
根据上述问题,要在数据库中存储会话,您应使用 doctrine 连接,例如,您可以使用 shapecode/doctrine-session-handler-bundle 扩展包
composer require shapecode/doctrine-session-handler-bundle
中间件
您可以使用中间件来操作 PSR 请求和响应。中间件可以实现 PSR MiddlewareInterface
或 Baldinof\RoadRunnerBundle\Http\IteratorMiddlewareInterface
。
IteratorMiddlewareInterface
允许在将响应发送到客户端之后进行工作,您只需将响应 yield
而不是返回。
示例配置
baldinof_road_runner: middlewares: - App\Middleware\YourMiddleware
请注意
- 中间件在 Symfony
Kernel::handle()
之外运行 - 中间件堆栈总是在工作进程启动时解析(如果您的中间件初始化花费时间,则可能成为性能问题)
度量
Roadrunner 有能力 收集应用程序度量,通过此扩展包配置启用度量
# config/packages/baldinof_road_runner.yaml baldinof_road_runner: metrics_enabled: true
并配置 RoadRunner
# .rr.yaml rpc: enable: true listen: unix://var/roadrunner_rpc.sock metrics: address: localhost:2112 collect: app_metric_counter: type: counter help: "Application counter."
简单地注入 Spiral\RoadRunner\MetricsInterface
来记录度量
class YouController { public function index(MetricsInterface $metrics): Response { $metrics->add('app_metric_counter', 1); return new Response("..."); } }
如果您注入了
Spiral\RoadRunner\MetricsInterface
,但配置中禁用了指标收集,则会注入一个NullMetrics
,不会收集任何指标。
内核重启
Symfony 内核和依赖注入容器在请求之间被 保留。如果在请求处理过程中抛出异常,则内核会重启并使用一个新的容器。
目标是防止服务在错误后处于无法恢复的状态。
为了优化您的worker,您可以允许不会使您的应用处于错误状态的异常。
# config/packages/baldinof_road_runner.yaml baldinof_road_runner: kernel_reboot: strategy: on_exception allowed_exceptions: - Symfony\Component\HttpKernel\Exception\HttpExceptionInterface - Symfony\Component\Serializer\Exception\ExceptionInterface - App\Exception\YourDomainException
如果您遇到问题并希望在每次请求上使用新的容器,您可以使用 always
重启策略。
# config/packages/baldinof_road_runner.yaml baldinof_road_runner: kernel_reboot: strategy: always
如果您的某些服务具有状态,您可以实现
Symfony\Contracts\Service\ResetInterface
,您的服务将在每次请求上被重置。
开发模式
如果不存在,则复制开发配置文件:cp vendor/baldinof/roadrunner-bundle/.rr.dev.yaml .
使用开发配置文件启动 RoadRunner
bin/rr serve -c .rr.dev.yaml
参考:https://roadrunner.dev/docs/beep-beep-reload
如果您使用 Symfony VarDumper,则转储将不会显示在 HTTP 响应体中。您可以使用 bin/console server:dump
或在分析器中查看转储。
与 Docker 的使用
# Dockerfile FROM php:7.4-alpine RUN apk add --no-cache autoconf openssl-dev g++ make pcre-dev icu-dev zlib-dev libzip-dev && \ docker-php-ext-install bcmath intl opcache zip sockets && \ apk del --purge autoconf g++ make WORKDIR /usr/src/app COPY --from=composer:latest /usr/bin/composer /usr/bin/composer COPY composer.json composer.lock ./ RUN composer install --no-dev --no-autoloader --no-scripts --no-plugins --prefer-dist --no-progress --no-interaction RUN ./vendor/bin/rr get-binary --location /usr/local/bin COPY . . ENV APP_ENV=prod RUN composer dump-autoload --optimize && \ composer check-platform-reqs && \ php bin/console cache:warmup EXPOSE 8080 CMD ["rr", "serve"]