Home  >  Article  >  PHP Framework  >  What is Laravel Octane? how to use?

What is Laravel Octane? how to use?

藏色散人
藏色散人forward
2021-10-19 15:14:144105browse

Taylor Otwell has demonstrated Laravel’s latest open source package Octane in his Laracon online talk. The new package is available on Github and everyone can test it.

In this blog post, we'll explore Laravel Octane and tell you what it is, how to install and use it, and why you might need it. [Recommended learning: laravel tutorial]

What is Laravel Octane?

Laravel Octane is an open source package that can enhance your Laravel application performance. Laravel Octane requires PHP 8, if you are still using 7.x, you need to upgrade your PHP version. Under the hood, Octane uses two application servers, Swoole and RoadRunner, which are responsible for serving and starting your Laravel program.

For traditional PHP applications served through a web server like nginx, each incoming request spawns a PHP-FPM worker. This means that each request starts a separate PHP process that runs all the necessary tasks in order to service that request.

In the case of Laravel, this means that the framework needs to be started, all service providers register their services in the container, all providers start themselves, requests go through a list of middleware classes, and hit your controller , rendering views, etc. Until we finally get a response from our server.

With Swoole or RoadRunner, we still have a worker thread for each incoming HTTP request, but they all share the same bootstrapping framework. This means that only the first incoming request will bootstrap the framework (including all service providers, etc.). All other requests can use the ready-made framework. That's why Octane is so fast.

Getting started with Laravel Octane

Since Laravel Octane is a package, you need to install it as a dependency for your specific application. You can install it via composer:

composer require laravel/octane

After installing Octane in your application, make sure to run php artisan octane:install. This will publish Octane's configuration files and add the rr - RoadRunner binary to the .gitignore file.

As I said, Octane will publish its configuration file as follows:

return [

    /*
    |--------------------------------------------------------------------------
    | Octane 服务器
    |--------------------------------------------------------------------------
    |
    | 该值决定了 Octane 在通过 CLI 启动、
    | 重启或停止服务器时将使用的默认 “服务器”。
    | 您可以随意将其更改为您选择的受支持服务器。
    |
    */

    'server' => env('OCTANE_SERVER', 'roadrunner'),

    /*
    |--------------------------------------------------------------------------
    | 强制 HTTPS
    |--------------------------------------------------------------------------
    |
    | 当该配置值设置为 “true” 时,
    | Octane 将通知框架必须使用 HTTPS 协议生成链接。
    | 否则,您的链接可能会使用普通 HTTP 生成。
    |
    */

    'https' => env('OCTANE_HTTPS', false),

    /*
    |--------------------------------------------------------------------------
    | Octane 监听器
    |--------------------------------------------------------------------------
    |
    | Octane 事件的所有事件监听器定义如下
    | 这些监听器负责为下一个请求重置应用程序的状态。
    | 您可以将自己的监听器添加到列表中。
    |
    */

    'listeners' => [
        WorkerStarting::class => [
            EnsureUploadedFilesAreValid::class,
        ],

        RequestReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            ...Octane::prepareApplicationForNextRequest(),
            //
        ],

        RequestHandled::class => [
            //
        ],

        RequestTerminated::class => [
            //
        ],

        TaskReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            //
        ],

        TickReceived::class => [
            ...Octane::prepareApplicationForNextOperation(),
            //
        ],

        OperationTerminated::class => [
            FlushTemporaryContainerInstances::class,
            // DisconnectFromDatabases::class,
            // CollectGarbage::class,
        ],

        WorkerErrorOccurred::class => [
            ReportException::class,
            StopWorkerIfNecessary::class,
        ],

        WorkerStopping::class => [
            //
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 预热 / 刷新绑定
    |--------------------------------------------------------------------------
    |
    | 下面列出的绑定要么在工作进程启动时预热,
    | 要么在每次新请求之前刷新。
    | 刷新绑定将强制容器在被请求时再次解析该绑定。
    |
    */

    'warm' => [
        ...Octane::defaultServicesToWarm(),
    ],

    'flush' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | 垃圾收集阈值
    |--------------------------------------------------------------------------
    |
    | 在执行诸如 Octane 之类的长期存在的 PHP 脚本时,
    | 内存可能会在被 PHP 清除之前积累。
    | 如果您的应用程序消耗了此数量的兆字节,
    | 您可以强制 Octane 运行垃圾回收。
    |
    */

    'garbage' => 50,

    /*
    |--------------------------------------------------------------------------
    | 最长执行时间
    |--------------------------------------------------------------------------
    |
    | (提示) 0 表示没有最长时间限制
    |
    */

    'max_execution_time' => 30,

    /*
    |--------------------------------------------------------------------------
    | Octane 缓存表
    |--------------------------------------------------------------------------
    |
    | 在使用 Swoole 时,
    | 您可以利用由 Swoole 表提供支持的 Octane 缓存。
    | 您可以使用以下配置选项设置最大行数以及每行的字节数。
    |
    */

    'cache' => [
        'rows' => 1000,
        'bytes' => 10000,
    ],

    /*
    |--------------------------------------------------------------------------
    | Octane Swoole 表
    |--------------------------------------------------------------------------
    |
    | 在使用 Swoole 时,
    | 您可以根据应用程序的需要定义额外的表。
    | 这些表可用于存储特定 Swoole 服务器上其他工作人员需要快速访问的数据。
    |
    */

    'tables' => [
        'example:1000' => [
            'name' => 'string:1000',
            'votes' => 'int',
        ],
    ],];

Next, you need to decide whether you want to use RoadRunner or Swoole, then, you can customize the configuration by server key in the file to configure the application server to use. This can be swoole or roadrunner.

RoadRunner

RoadRunner is an application server written in Go that does not have any other dependencies in PHP itself. If you don’t want to install additional PHP extensions, choose RoadRunner. You can install RoadRunner via composer as follows:

composer require spiral/roadrunner

Swoole

Swoole brings some benefits that RoadRunner cannot provide. Since Swoole is an extension on top of PHP, PHP itself gets some cool new features like “ticks” and “coroutines”, which I’ll cover later. RoadRunner doesn't have these features, so if you want to use them, you should use Swoole.

You can install the Swoole extension using the following method:

pecl install swoole

During the installation process, you will be asked if you want to support HTTP2, curl, JSON and open_ssl in Swoole. You can safely use the defaults here (i.e. off), as these settings only affect things like coroutines.

Starting Octane

Once you have installed RoadRunner or Swoole and defined it in your octane.php configuration file, you You can start Octane and have it serve your Laravel application.

php artisan octane:start

By default, Octane will start the server on port 8000, since you can access your application in a browser via http://localhost:8000.

Go ahead, visit this route and check out the Laravel application! If you make multiple requests, you will see that the first request is slightly slower because this is where the framework is started, while the other requests are significantly faster because they can use the framework from memory after it is started.

200 GET / .............. 14.23 ms200 GET / ..............  1.93 ms200 GET / ..............  2.06 ms

更改代码

如果现在添加新的 /test 路由 - 并尝试访问,将收到 404 响应! 这是因为请求访问的是 Octane 服务内存中的框架(及其所有路由/代码)。 因此,为了查看代码更改,需要重启 Octane 服务。 这导致在开发过程中非常麻烦,所以 Octane 提供了一种很好的方式来自动监视代码库的更改并自动重启 Octane 服务。

为了完成这项工作,请确保安装 Chokidar - 基于 NodeJS 的文件监视库:

npm install --save-dev chokidar

然后,您可以在 “watch” 模式下启动 Octane 服务器,如下所示:

php artisan octane:start --watch

现在,下次您对代码库进行更改时,系统会检测到这一点,Octane 将为请求重启工作程序,您可以立即看到您的更改。

自定义 Workers

说到 Workers,默认情况下,Octane 将为您拥有的每个 CPU 内核启动一个 worker,但是您也可以改变这一点,通过 --workers 选项传递 octane:start 命令:

php artisan octane:start --workers=2

Swoole 特定功能

正如文中所提到的,Octane 带有一些 Swoole 特定的功能,我认为非常有趣,让我们来看看它们。

并发任务

Octane 可以同时执行多个任务。 它们会同时执行,并在所有任务完成后立即返回。

以下是 GitHub 上 Octane 文档中的一个示例:

use App\User;use App\Server;use Laravel\Octane\Facades\Octane;[$users, $servers] = Octane::concurrently([
    fn () => User::all(),
    fn () => Server::all(),]);

在这个例子中,我们同时获取所有用户和所有服务。 为了证明它们是同时执行,这是另一个例子:

Octane::concurrently([
        function () {
                sleep(0.5);
                return 1;
        },
        function () {
                sleep(1);
                return 2;
        },
    ]);

我们同时执行两个「任务」,一旦它们完成,PHP 将继续执行代码。 一个任务等待 0.5 秒,另一个任务等待 1 秒。 由于它们同时被执行,在两个单独的任务中,PHP 将等待 1 秒(不是 1.5 秒),直到两个结果都可用。 此功能是同时执行多个较小任务的绝佳方式。

就像 --workers 选项一样,您也可以自定义 Octane 提供 --task-workers 的数量。

Ticks / 间隔

Octane 与 Swoole 相结合,允许您注册 ticks - 这些操作将在给定的时间间隔内自动执行。类似 JavasScript 中的 setInterval 方法。不幸的是,目前无法停止这些 ticks,但您可以像这样在应用程序中注册它们:

Octane::tick('simple-ticker', fn () => ray('Ticking...'))
        ->seconds(10);

Octane 缓存

Octane 和 Swoole 中的另一个新功能是新缓存驱动程序。根据官方文档,此缓存驱动程序提供读与写速度高达每秒 200 万次操作。在幕后,Swoole 使用 Swoole 表 将数据缓存在共享内存中,所有 workers 都可以访问该表。当服务器重新启动时,缓存数据将被刷新,因为缓存仅保留在内存中。

要使用此缓存,您可以通过 Cache 门面的 otance 缓存存储访问它,如下所示:

Cache::store('octane')->put('framework', 'Laravel', 30);

另一个很酷的新增功能,即 Swoole 和 Octane 特有的是 “缓存间隔” 能力。这允许您将信息存储在 Octane 缓存中并在给定的时间间隔内刷新数据:

use Illuminate\Support\Str;Cache::store('octane')->interval('random', function () {
    return Str::random(10);}, seconds: 5)

Octane 表

基于 Swoole 表 的功能,您可以创建自己想要在 Octane 应用程序中访问的表。这些表具有与缓存相同的性能优势,允许您以结构化方式保存数据。请记住,当服务器重新启动时,您存储在此表中的所有数据都会丢失。

要配置表,您可以在 octane.php 配置文件中的 tables 部分创建一个条目:

'tables' => [
    'example:1000' => [
        'name' => 'string:1000',
        'votes' => 'int',
    ],],

在示例中,我们定义了一个名为 example 的表,该表最多可容纳 1.000 个条目/行。表的结构体是一个 name,它的最大长度为 1000 的字符串, 然而 votes,它是一个整数。

要把数据写入表中,我们可以使用 Octane::table 方法:

use Laravel\Octane\Facades\Octane;Octane::table('example')->set('a-unique-identifier', [
    'name' => 'Marcel Pociot',
    'votes' => 1000,]);

要取出数据,我们可以在表上使用 get 方法,如下所示:

return Octane::table('example')->get('a-unique-identifier');

Octane 的注意事项

当您想要为 Octane 准备现有应用程序或从开头开始构建新应用程序时,您需要注意几件事。

由于 Octane 将框架保存在所有 workers 的内存中,因此所有应用程序服务提供商之类的东西只会注册和启动一次。但 Octane 不可能重置您在自己的应用程序代码中可能具有的全局状态。

目前可以在 GitHub 上找到的官方文档包含一些您应该注意的最常见场景。

监听器

Octane 的一个尚未被记录的特性是:无论何时在Octane内的应用程序服务器中发生什么事情,都可以注册自定义监听器。您可以监听以下事件:

  • WorkerStarting
  • RequestReceived
  • RequestHandled
  • RequestTerminated
  • TaskReceived
  • TickReceived
  • OperationTerminated
  • WorkerErrorOccurred
  • WorkerStopping

要将监听器附加到这些事件,您可以将它们添加到您的 octane.php 配置文件中。

服务预热和刷新

当一个新的 Octane worker 启动时,您可以指定一个在启动过程中要「预热」的容器绑定 / 服务列表。这意味着,在 Worker 启动时,服务容器将已经使指定的服务可用,以便后续请求可以立即访问它们。

Octane 已经有一个内部服务列表,它会在每个工作程序启动过程中保持热加载,您也可以将自己的服务添加到 octane.php 配置文件中的 warm 部分。

与此类似,您还可以在新请求进入之前定义要刷新的服务列表。这对于在请求过程中被操纵的服务很有用,当新请求进入时,您希望将其重置为原始/卸载状态。

Octane 路由

如果 Octane 没有给您提升足够的速度,您可以通过 Octane 内置的路由来进一步提高速度。您可以通过 Octane 自定义路由,如下所示:

Octane::route('GET', '/fast', function() {});

这些路由非常快,因为它们完全跳过了 Laravel 的路由系统(因此这些路由不提供任何类型的中间件)— 这对只需要快速提供数据的端点很有帮助。

由于 Laravel     中的 HTTP 内核未用于这些请求,您需要自己返回一个 Symfony Response 对象,如下所示:

use Symfony\Component\HttpFoundation\Response;Octane::route('GET', '/faster', function() {
    return new Response('Hello from Octane.');});

当然,所有服务提供者都已启动且可用,因此您仍然可以使用这些服务执行 Eloquent 查询等。

好吧…那为什么是 Octane?

Laravel Octane 肯定会给您的 Laravel 应用程序带来巨大的性能提升 — 我们都喜欢性能提升,不是吗?我们真的需要这种性能提升吗?好吧,也许 — 我认为这取决于您正在运行的应用程序。但对我来说更重要的是 Laravel 正在(再次)推动 PHP 的当前状态。Octane 不仅是一个至少需要 PHP 8 的包,并且它还在 PHP 世界中推出了令人兴奋的新功能,例如协程、ticks,以及使用 artisan 命令服务您应用程序的思维模式。

我为 Octane 的未来感到兴奋!


原文地址:https://beyondco.de/blog/laravel-octane-introduction

译文地址:https://learnku.com/laravel/t/61524

The above is the detailed content of What is Laravel Octane? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete