unfazed/laravoole

在 Swoole 或 Workerman 上为 Laravel 提供 10 倍的性能,由 garveen/laravoole 分支而来

0.5.4 2017-06-29 08:47 UTC

README

Swoole 或 Workerman 上的 Laravel

比 php-fpm 快 10 倍,由 garveen/laravoole 分支而来

https://github.com/garveen/laravoole 不同

请求不能从 json 获取参数时,直接启动 swoole 进程,而不是通过 popen,因此可以使用 supervisor 来管理进程

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Code Coverage

依赖

建议

安装

要开始使用,请将 laravoole 添加到您的 composer.json 文件中,并运行 composer update

"unfazed/laravoole": "^0.5.0"

或直接运行 shell 命令

 composer require unfazed/laravoole

一旦 composer 完成工作,您需要在 config/app.php 中注册 Laravel 服务提供者

'providers' => [
    ...
    Laravoole\LaravooleServiceProvider::class,
],

注意:您不应该使用文件会话处理器,因为在这个环境中它不稳定。请使用 redis 或其他处理器。

用法

php artisan laravoole [start | stop | reload | reload_task | restart | quit]

迁移

升级到 0.4

事件名称已更改

  • laravoole.on_request => laravoole.requesting
  • laravoole.on_requested => laravoole.requested
  • laravoole.swoole.websocket.on_close => laravoole.swoole.websocket.closing

配置

生成 config/laravoole.php

php artisan vendor:publish --provider="Laravoole\LaravooleServiceProvider"

大多数配置都可以通过 .env 完成,并且您应该使用 LARAVOOLE_{UPPER_CASE} 格式,例如

[
    'base_config' => [
        'host' => '0.0.0.0',
    ]
]

等同于

LARAVOOLE_HOST=0.0.0.0

事件

您可以通过编辑 EventServiceProvider 来处理事件

public function boot()
{
    parent::boot();
    \Event::listen('laravoole.requesting', function ($request) {
        \Log::info($request->segments());
    });
}
  • laravoole.requesting(Illuminate\Http\Request)
  • laravoole.requested(Illuminate\Http\Request, Illuminate\Http\Response)
  • laravoole.swoole.websocket.closing(Laravoole\Request, int $fd)

基本配置

本节配置 laravoole 本身。

模式

SwooleHttp 使用 swoole 来响应 http 请求

SwooleFastCGI 使用 swoole 来响应 fastcgi 请求(就像 php-fpm 一样)

SwooleWebSocket 使用 swoole 来响应 websocket 请求 以及 http 请求

WorkermanFastCGI 使用 workerman 来响应 fastcgi 请求(就像 php-fpm 一样)

用户自定义包装器

您可以通过创建一个实现 Laravoole\Wrapper\ServerInterface 的新包装器,并将其实例的全类名放入 mode 中。

pid_file

定义一个文件,用于存储主进程的进程 ID。

deal_with_public

当使用 Http 模式时,您可以打开此选项,让 laravoole 向客户端发送静态资源。仅当开发时使用此选项。

主机和端口

默认 host127.0.0.1port9050

handler_config

本节配置后端,例如 swooleworkerman

Swoole

例如,如果您想将 worker_num 设置为 8,您可以在 .env 中设置

 LARAVOOLE_WORKER_NUM=8

或设置 config/laravoole.php

[
    'handler_config' => [
        'worker_num' => 8,
    ]
]

参见 Swoole 的文档

简体中文

英文

Workerman

例如,如果您想将 worker_num 设置为 8,您可以在 .env 中设置

 LARAVOOLE_COUNT=8

或设置 config/laravoole.php

[
    'handler_config' => [
        'count' => 8,
    ]
]

参见 Workerman 的文档

简体中文

英文

WebSocket 使用

子协议

参见 Mozilla 的文档:编写 WebSocket 服务器

默认子协议是 jsonrpc,但有一些不同:params 是一个对象,并且有两个额外的属性

status 作为 HTTP 状态码

method 与请求的方法相同

您可以通过实现 Laravoole\WebsocketCodec\CodecInterface 并将其添加到 config/laravoole.php 中来定义自己的子协议。

客户端示例

<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<style>
p{word-wrap: break-word;}
tr:nth-child(odd){background-color: #ccc}
tr:nth-child(even){background-color: #eee}
</style>
<h2>WebSocket Test</h2>
<table><tbody id="output"></tbody></table>
<script>
    var wsUri = "ws://:9050/websocket";
    var protocols = ['jsonrpc'];
    var output = document.getElementById("output");

    function send(message) {
        websocket.send(message);
        log('Sent', message);
    }

    function log(type, str) {
        str = str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
        output.insertAdjacentHTML('beforeend', '<tr><td>' + type + '</td><td><p>' + htmlEscape(str) + '</p></td></tr>');
    }

    websocket = new WebSocket(wsUri, protocols);
    websocket.onopen = function(evt) {
        log('Status', "Connection opened");
        send(JSON.stringify({method: '/', params: {hello: 'world'},  id: 1}));
        setTimeout(function(){ websocket.close() },1000)
    };
    websocket.onclose = function(evt) { log('Status', "Connection closed") };
    websocket.onmessage = function(evt) { log('<span style="color: blue;">Received</span>', evt.data) };
    websocket.onerror = function(evt) {  log('<span style="color: red;">Error</span>', evt.data) };
</script>
</html>

与 nginx 一起使用

server {
    listen       80;
    server_name  localhost;

    root /path/to/laravel/public;

    location / {
            try_files $uri $uri @laravoole;
            index  index.html index.htm index.php;
        }

    # http
    location @laravoole {
			proxy_set_header   Host $host;
			proxy_set_header   X-Forwarded-Host $host;
			proxy_set_header   X-Forwarded-Port $server_port;
			proxy_set_header   X-Forwarded-Proto $scheme;
			proxy_set_header   X-Real-IP $remote_addr;
			proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_http_version 1.1;

			proxy_pass http://127.0.0.1:9050;
    }

    # fastcgi
    location @laravoole {
			include fastcgi_params;
			fastcgi_pass 127.0.0.1:9050;
    }

    # websocket
    # send close if there has not an upgrade header
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
    location /websocket {
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
        proxy_pass http://127.0.0.1:9050;
        proxy_http_version 1.1;
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

许可证

MIT