加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

LaravelS - 基于Swoole加速Laravel/Lumen

发布时间:2020-05-25 08:20:07 所属栏目:PHP 来源:互联网
导读:LaravelS LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能、更多可能性。Github 特性 内置Http/WebSocket服务器

LaravelS

LaravelS是一个胶水项目,用于快速集成SwooleLaravelLumen,然后赋予它们更好的性能、更多可能性。Github

特性

  • 内置Http/WebSocket服务器
  • 多端口混合协议
  • 协程
  • 自定义进程
  • 常驻内存
  • 异步的事件监听
  • 异步的任务队列
  • 毫秒级定时任务
  • 平滑Reload
  • 修改代码后自动Reload
  • 同时支持Laravel与Lumen,兼容主流版本
  • 简单,开箱即用

要求

安装

1.通过Composer安装(packagist)。有可能找不到3.0版本,解决方案移步#81。

composer require "hhxsv5/laravel-s:~3.5.0" -vvv
# 确保你的composer.lock文件是在版本控制中

2.注册Service Provider(以下两步二选一)。

  • Laravel: 修改文件config/app.phpLaravel 5.5+支持包自动发现,你应该跳过这步

    'providers' => [
        //...
        Hhxsv5LaravelSIlluminateLaravelSServiceProvider::class,],

  • Lumen: 修改文件bootstrap/app.php

    $app->register(Hhxsv5LaravelSIlluminateLaravelSServiceProvider::class);

3.发布配置和二进制文件。

每次升级LaravelS后,需重新publish;点击Release去了解各个版本的变更记录。
php artisan laravels publish
# 配置文件:config/laravels.php
# 二进制文件:bin/laravels bin/fswatch bin/inotify

4.修改配置config/laravels.php:监听的IP、端口等,请参考配置项。

运行

php bin/laravels {start|stop|restart|reload|info|help}

在运行之前,请先仔细阅读:注意事项(非常重要)。

部署

建议通过Supervisord监管主进程,前提是不能加-d选项并且设置swoole.daemonizefalse
[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类,并实现接口WebSocketHandlerInterface。start时会自动实例化,不需要手动创建实例。

 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 }

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读