nyatmeat/roadrunner-bundle

一个作为 Symfony Bundle 的 RoadRunner 工作者

资助包维护!
Baldinof

安装: 21

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 1

分支: 47

类型:symfony-bundle

2.1.2 2021-11-29 20:50 UTC

README

RoadRunner 是一个用 Golang 编写的性能高效的 PHP 应用程序服务器、负载均衡器和进程管理器。

此包提供集成到 Symfony 中的 RoadRunner Worker,易于配置和扩展。

安装

运行以下命令

composer require baldinof/roadrunner-bundle

如果你不使用 Symfony Flex

  • 在 kernel 中注册 Baldinof\RoadRunnerBundle\BaldinofRoadRunnerBundle
  • 复制默认 RoadRunner 配置文件: cp vendor/baldinof/roadrunner-bundle/.rr.* .

使用

  • 获取 RoadRunner 二进制文件: vendor/bin/rr get --location bin/
  • 使用 bin/rr servebin/rr serve -c .rr.dev.yaml (监视模式)运行 RoadRunner
  • https://:8080 访问你的应用

集成

根据安装的包和你的配置,此包添加了一些集成

  • Sentry:配置请求上下文(如果已安装 SentryBundle
  • 会话:将会话 Cookie 添加到 Symfony 响应(如果 framework.sessions.enabled 配置为 true
  • Doctrine Mongo Bundle:在每个请求后清除打开的管理器(如果已安装 DoctrineMongoDBBundle
  • Doctrine ORM Bundle:在每个请求后清除打开的管理器并检查连接是否仍然可用(如果已安装 DoctrineBundle
  • Blackfire:在请求配置文件时启用探针(如果已安装 blackfire 扩展)

即使不建议,您也可以禁用默认集成

baldinof_road_runner:
  default_integrations: false

中间件

您可以使用中间件来操作请求和响应。中间件必须实现 Baldinof\RoadRunnerBundle\Http\MiddlewareInterface

示例配置

baldinof_road_runner:
    middlewares:
        - App\Middleware\YourMiddleware

请注意

  • 中间件在 Symfony Kernel::handle() 之外运行
  • 中间件堆栈始终在工作者启动时解析(如果中间件初始化花费时间,则可能会出现性能问题)

Kernel 重启

Symfony kernel 和依赖注入容器在请求之间保持不变。如果在请求处理期间抛出异常,则重启 kernel 并使用新的容器。

目的是防止服务在错误后处于不可恢复的状态。

为了优化你的工作者,你可以允许不会使你的应用处于错误状态的异常

# 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

如果你的某些服务是状态性的,你可以实现 Symfony\Contracts\Service\ResetInterface 并在每次请求时重置你的服务

如果你在每次请求上使用新鲜容器,可以使用 always 重启策略

# config/packages/baldinof_road_runner.yaml
baldinof_road_runner:
    kernel_reboot:
      strategy: always

事件

以下事件在工作者生命周期中分发

  • Baldinof\RoadRunnerBundle\Event\WorkerStartEvent:在工作者开始监听请求之前分发。
  • Baldinof\RoadRunnerBundle\Event\WorkerStopEvent:在工作者关闭之前分发。
  • Baldinof\RoadRunnerBundle\Event\WorkerExceptionEvent:在请求处理过程中遇到未捕获的异常后触发。
  • Baldinof\RoadRunnerBundle\Event\WorkerKernelRebootedEvent:在 symfony 内核重启后触发(参见内核重启)。

开发模式

如果不存在,则复制开发配置文件: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 或在分析器中查看转储。

指标

Roadrunner 可以 收集应用程序指标,并暴露 Prometheus 端点。

示例配置

# config/packages/baldinof_road_runner.yaml
baldinof_road_runner:
  metrics:
    enabled: true
    collect:
      user_login:
        type: counter
        help: "Number of logged in user"

然后配置 RoadRunner

# .rr.yaml
rpc:
  listen: "tcp:127.0.0.1:6001"

metrics:
  address: "0.0.0.0:9180" # prometheus endpoint

然后简单地将 Spiral\RoadRunner\MetricsInterface 注入以记录指标

class YouController
{
    public function index(MetricsInterface $metrics): Response
    {
        $metrics->add('user_login', 1);

        return new Response("...");
    }
}

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-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"]