LaravelS - 基于Swoole加速Laravel/Lumen
发布时间:2020-05-25 08:20:07 所属栏目:PHP 来源:互联网
导读:LaravelS LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能、更多可能性。Github 特性 内置Http/WebSocket服务器
LaravelS
特性
要求安装1.通过Composer安装(packagist)。有可能找不到 composer require "hhxsv5/laravel-s:~3.5.0" -vvv # 确保你的composer.lock文件是在版本控制中 2.注册Service Provider(以下两步二选一)。
3.发布配置和二进制文件。 每次升级LaravelS后,需重新publish;点击Release去了解各个版本的变更记录。
4.修改配置 运行
部署建议通过Supervisord监管主进程,前提是不能加 [program:laravel-s-test] command=/user/local/bin/php /opt/www/laravel-s-test/bin/laravels start -i numprocs=1 autostart=true autorestart=true startretries=3 user=www-data redirect_stderr=true stdout_logfile=/opt/www/laravel-s-test/storage/logs/supervisord-stdout.log 与Nginx配合使用(推荐)示例。 gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
upstream swoole {
# 通过 IP:Port 连接
server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
# 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
#server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
#server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
#server 192.168.1.2:5200 backup;
keepalive 16;
}
server {
listen 80;
# 别忘了绑Host哟
server_name laravels.com;
root /xxxpath/laravel-s-test/public;
access_log /yyypath/log/nginx/$server_name.access.log main;
autoindex off;
index index.html index.htm;
# Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。
location / {
try_files $uri @laravels;
}
# 当请求PHP文件时直接响应404,防止暴露public/*.php
#location ~* .php$ {
# return 404;
#}
location @laravels {
# proxy_connect_timeout 60s;
# proxy_send_timeout 60s;
# proxy_read_timeout 120s;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
proxy_pass http://swoole;
}
}
与Apache配合使用 1 LoadModule proxy_module /yyypath/modules/mod_deflate.so
2 <IfModule deflate_module>
3 SetOutputFilter DEFLATE
4 DeflateCompressionLevel 2
5 AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml
6 </IfModule>
7
8 <VirtualHost *:80>
9 # 别忘了绑Host哟
10 ServerName www.laravels.com
11 ServerAdmin hhxsv5@sina.com
12
13 DocumentRoot /xxxpath/laravel-s-test/public;
14 DirectoryIndex index.html index.htm
15 <Directory "/">
16 AllowOverride None
17 Require all granted
18 </Directory>
19
20 LoadModule proxy_module /yyypath/modules/mod_proxy.so
21 LoadModule proxy_module /yyypath/modules/mod_proxy_balancer.so
22 LoadModule proxy_module /yyypath/modules/mod_lbmethod_byrequests.so.so
23 LoadModule proxy_module /yyypath/modules/mod_proxy_http.so.so
24 LoadModule proxy_module /yyypath/modules/mod_slotmem_shm.so
25 LoadModule proxy_module /yyypath/modules/mod_rewrite.so
26
27 ProxyRequests Off
28 ProxyPreserveHost On
29 <Proxy balancer://laravels>
30 BalancerMember http://192.168.1.1:5200 loadfactor=7
31 #BalancerMember http://192.168.1.2:5200 loadfactor=3
32 #BalancerMember http://192.168.1.3:5200 loadfactor=1 status=+H
33 ProxySet lbmethod=byrequests
34 </Proxy>
35 #ProxyPass / balancer://laravels/
36 #ProxyPassReverse / balancer://laravels/
37
38 # Apache处理静态资源,LaravelS处理动态资源。
39 RewriteEngine On
40 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
41 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
42 RewriteRule ^/(.*)$ balancer://laravels/%{REQUEST_URI} [P,L]
43
44 ErrorLog ${APACHE_LOG_DIR}/www.laravels.com.error.log
45 CustomLog ${APACHE_LOG_DIR}/www.laravels.com.access.log combined
46 </VirtualHost>
启用WebSocket服务器WebSocket服务器监听的IP和端口与Http服务器相同。 1.创建WebSocket Handler类,并实现接口 1 namespace AppServices;
2 use Hhxsv5LaravelSSwooleWebSocketHandlerInterface;
3 use SwooleHttpRequest;
4 use SwooleWebSocketFrame;
5 use SwooleWebSocketServer;
6 /**
7 * @see https://wiki.swoole.com/wiki/page/400.html
8 */
9 class WebSocketService implements WebSocketHandlerInterface
10 {
11 // 声明没有参数的构造函数
12 public function __construct()
13 {
14 }
15 public function onOpen(Server $server,Request $request)
16 {
17 // 在触发onOpen事件之前,建立WebSocket的HTTP请求已经经过了Laravel的路由,
18 // 所以Laravel的Request、Auth等信息是可读的,Session是可读写的,但仅限在onOpen事件中。
19 // Log::info('New WebSocket connection',[$request->fd,request()->all(),session()->getId(),session('xxx'),session(['yyy' => time()])]);
20 $server->push($request->fd,'Welcome to LaravelS');
21 // throw new Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
22 }
23 public function onMessage(Server $server,Frame $frame)
24 {
25 // Log::info('Received message',[$frame->fd,$frame->data,$frame->opcode,$frame->finish]);
26 $server->push($frame->fd,date('Y-m-d H:i:s'));
27 // throw new Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
28 }
29 public function onClose(Server $server,$fd,$reactorId)
30 {
31 // throw new Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
32 }
33 }
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
