garveen / laravoole
在 Swoole 或 Workerman 上为 Laravel 提供 10 倍的性能
Requires
- php: >=5.5.16
- ext-posix: *
- garveen/fastcgi: ^0.1
- laravel/framework: ~5.1
- symfony/psr-http-message-bridge: ^1.0
- zendframework/zend-diactoros: ^1.3
Requires (Dev)
- adoy/fastcgi-client: ^1.0
- laravel/laravel: ~5.1
- phpunit/phpunit: >4.8 <6.0
- textalk/websocket: ^1.2
- workerman/workerman: ^3.3
Suggests
- ext-swoole: event-based extension for best performance
- workerman/workerman: event-based library without need for any extension
README
Laravel 在 Swoole 或 Workerman 上
比 php-fpm 快 10 倍
依赖
建议
安装
要开始,请将 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 请求 AND HTTP 请求
WorkermanFastCGI
使用 workerman 响应 fastcgi 请求(类似于 php-fpm)
用户自定义包装器
您可以通过实现 Laravoole\Wrapper\ServerInterface
来创建一个新的包装器,并将其完整类名放入 mode
。
pid_file
定义一个文件,将存储主进程的进程 ID。
deal_with_public
在 Http 模式下使用时,您可以打开此选项让 laravoole 向客户端发送静态资源。仅当开发时使用此选项。
主机和端口
默认 host
是 127.0.0.1
,而 port
是 9050
handler_config
本节配置后端,例如 swoole
或 workerman
。
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, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>'); 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; } }