美文网首页
Laravel-swoole扩展包的使用

Laravel-swoole扩展包的使用

作者: 周星星的学习笔记 | 来源:发表于2021-01-22 13:26 被阅读0次

    如何在Laravel简单快速地使用swoole呢?下面就简单给大家介绍一下。Github地址:https://github.com/swooletw/laravel-swoole

    一、确认PHP安装了swoole扩展

    /var/www/html/backend # php -m | grep swoole
    swoole
    

    二、进入Laravel工程中,安装Laravel-swoole扩展

    #首先进入到你的Laravel项目工程中
    cd  <your Laravel project>
    #执行composer命令安装
    composer require swooletw/laravel-swoole
    

    三、配置

    1.打开config/app.php文件,并且在providers数组中添加如下一行代码。

    [
        'providers' => [
            //添加此服务提供者
            SwooleTW\Http\LaravelServiceProvider::class,
        ],
    ]
    

    四、使用

    1.添加测试接口,在routes/api.php文件中添加示例代码如下:

    Route::get('/hello', function (Request $request) {
        return 'hello zhouxingxing' . PHP_EOL;
    });
    

    2.启动http服务

    /var/www/html/backend # php artisan swoole:http start
    
    
    Starting swoole http server...
    Swoole http server started: <http://127.0.0.1:1215>
    

    3.访问接口

    /var/www/html/backend # curl 'http://127.0.0.1:1215/api/hello'
    hello zhouxingxing
    

    五、测试对比

    1.使用php artisan serve 命令启动的http服务

    /var/www/html/backend # php artisan serve --port=1234
    
    Starting Laravel development server: http://127.0.0.1:1234
    [Fri Jan 22 13:05:53 2021] PHP 7.4.11 Development Server (http://127.0.0.1:1234) started
    
    

    2.接口测试如下:

    /var/www/html/backend # wrk -t4 -c10 http://127.0.0.1:1234/api/hello
    Running 10s test @ http://127.0.0.1:1234/api/hello
      4 threads and 10 connections
    
    
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency     1.34s   432.48ms   2.00s    51.52%
        Req/Sec     3.41      4.21    10.00     73.17%
      41 requests in 10.07s, 13.41KB read
      Socket errors: connect 0, read 41, write 0, timeout 8
    Requests/sec:      4.07
    Transfer/sec:      1.33KB
    
    

    3.使用swoole启动的http服务

    /var/www/html/backend # php artisan swoole:http start
    
    Starting swoole http server...
    Swoole http server started: <http://127.0.0.1:1215>
    
    

    4.接口测试如下:

    /var/www/html/backend # wrk -t4 -c10 http://127.0.0.1:1215/api/hello
    Running 10s test @ http://127.0.0.1:1215/api/hello
      4 threads and 10 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   303.11ms   94.68ms 821.46ms   90.08%
        Req/Sec     7.33      2.77    10.00     81.42%
      260 requests in 10.05s, 0.99MB read
      Non-2xx or 3xx responses: 144
    Requests/sec:     25.87
    Transfer/sec:    100.58KB
    

    5.说明:由于是在docker容器里面跑的服务,所以整体QPS都不高,但是二者对比一下前者QPS只有4.07,而后者的QPS为25.87,相差还是挺大的。

    六、常用的swoole:http的命令

    1.启动http服务

    php artisan swoole:http start
    

    2.停止http服务

    php artisan swoole:http stop
    

    3.重启http服务

    php artisan swoole:http restart
    

    4.重载http服务

    php artisan swoole:http reload
    

    5.查看http服务运行信息

    php artisan swoole:http infos
    
    /var/www/html/backend # php artisan swoole:http infos
    +-----------------+----------------------------------------------------+
    | Name            | Value                                              |
    +-----------------+----------------------------------------------------+
    | PHP Version     | 7.4.11                                             |
    | Swoole Version  | 4.5.2                                              |
    | Laravel Version | 8.18.1                                             |
    | Listen IP       | 127.0.0.1                                          |
    | Listen Port     | 1215                                               |
    | Server Status   | Offline                                            |
    | Reactor Num     | 2                                                  |
    | Worker Num      | 2                                                  |
    | Task Worker Num | 0                                                  |
    | Websocket Mode  | Off                                                |
    | Master PID      | None                                               |
    | Manager PID     | None                                               |
    | Log Path        | /var/www/html/backend/storage/logs/swoole_http.log |
    +-----------------+----------------------------------------------------+
    

    相关文章

      网友评论

          本文标题:Laravel-swoole扩展包的使用

          本文链接:https://www.haomeiwen.com/subject/upirzktx.html