sujiayi/laravoole

在 Swoole 或 Workerman 上为 Laravel 提供十倍性能

0.5.1 2017-03-14 08:17 UTC

README

Laravoole

在 Swoole 或 Workerman 上运行 Laravel

比 php-fpm 快 10 倍

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

依赖项

建议

安装

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

"garveen/laravoole": "^0.5.0"

或直接运行 shell 命令

 composer require garveen/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

处理器配置

本节配置后端,例如 swooleworkerman

Swoole

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

 LARAVOOLE_WORKER_NUM=8

或设置 config/laravoole.php

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

参见 Swoole 的文档

简体中文

English

Workerman

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

 LARAVOOLE_COUNT=8

或设置 config/laravoole.php

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

参见 Workerman 的文档

简体中文

English

Websocket 用法

子协议

参见 Mozilla 的文档: Writing WebSocket server

默认子协议是 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:$server_port;
        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