如何在Laravel簡(jiǎn)單快速地使用swoole呢?下面就簡(jiǎn)單給大家介紹一下。Github地址:https://github.com/swooletw/laravel-swoole。
一、確認(rèn)PHP安裝了swoole擴(kuò)展
/var/www/html/backend # php -m | grep swoole
swoole
二、進(jìn)入Laravel工程中,安裝Laravel-swoole擴(kuò)展
#首先進(jìn)入到你的Laravel項(xiàng)目工程中
cd <your Laravel project>
#執(zhí)行composer命令安裝
composer require swooletw/laravel-swoole
三、配置
1.打開(kāi)config/app.php文件,并且在providers數(shù)組中添加如下一行代碼。
[
'providers' => [
//添加此服務(wù)提供者
SwooleTW\Http\LaravelServiceProvider::class,
],
]
四、使用
1.添加測(cè)試接口,在routes/api.php文件中添加示例代碼如下:
Route::get('/hello', function (Request $request) {
return 'hello zhouxingxing' . PHP_EOL;
});
2.啟動(dòng)http服務(wù)
/var/www/html/backend # php artisan swoole:http start
Starting swoole http server...
Swoole http server started: <http://127.0.0.1:1215>
3.訪問(wèn)接口
/var/www/html/backend # curl 'http://127.0.0.1:1215/api/hello'
hello zhouxingxing
五、測(cè)試對(duì)比
1.使用php artisan serve 命令啟動(dòng)的http服務(wù)
/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.接口測(cè)試如下:
/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啟動(dòng)的http服務(wù)
/var/www/html/backend # php artisan swoole:http start
Starting swoole http server...
Swoole http server started: <http://127.0.0.1:1215>
4.接口測(cè)試如下:
/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.說(shuō)明:由于是在docker容器里面跑的服務(wù),所以整體QPS都不高,但是二者對(duì)比一下前者QPS只有4.07,而后者的QPS為25.87,相差還是挺大的。
六、常用的swoole:http的命令
1.啟動(dòng)http服務(wù)
php artisan swoole:http start
2.停止http服務(wù)
php artisan swoole:http stop
3.重啟http服務(wù)
php artisan swoole:http restart
4.重載http服務(wù)
php artisan swoole:http reload
5.查看http服務(wù)運(yùn)行信息
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 |
+-----------------+----------------------------------------------------+