在上一篇我们实现了 Swoole
在 Laravel
中的安装和使用,这一篇将讲解 RoadRunner
的安装和使用。
安装 RoadRunner
和安装 Swoole
类似,唯一了区别是 RoadRunner
无需安装 php
的扩展。
环境要求
- PHP 8.0+
- Larave 8.35+
Swoole
和RoadRunner
必须在unix
系统下安装(macOS, linux, window(WSL2))RoadRunner
:请先确保curl
,zip
sockets
等php
扩展已经安装- 虚拟机IP:
192.168.2.11
Step1. 安装 octane
包
composer require laravel/octane
Step2. 配置 nginx 代理
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 8009; # 浏览器端访问的端口
#listen [::]:80;
server_name 127.0.0.1;
server_tokens off;
root /var/www/html/octane/public;
index index.php;
charset utf-8;
location /index.php {
try_files /not_exists @octane;
}
location / {
try_files $uri $uri/ @octane;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log /var/log/nginx/octane-access.log main;
error_log /var/log/nginx/octane-error.log error;
error_page 404 /index.php;
location @octane {
set $suffix "";
if ($uri = /index.php) {
set $suffix ?$query_string;
}
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://127.0.0.1:8010$suffix; # 代理请求
}
}
proxy_pass
表示 http://127.0.0.1:8010
代理转发来自 http://192.168.2.110:8009
的请求
Step3. 使用 RoadRunner
1. 安装 spiral/roadrunner
# 不同版本可能表现不同,尽量用最新版本
composer require spiral/roadrunner:v2.4.0
2. 发布配置文件
php artisan octane:install
输出:
Which application server you would like to use?:
[0] roadrunner
[1] swoole
> 0
选择 0,表示我们选择了 RoadRunner
3. 获取二进制文件
./vendor/bin/rr get-binary
4. 运行
php artisan octane:start --server=roadrunner --port=8010
输出:
INFO Server running…
Local: http://127.0.0.1:8010
Press Ctrl+C to stop the server
看到这里,说明安装成功了。
5. 在浏览器中访问
在浏览器中输入 http://192.168.2.11:8009
并回车。可以看到命令界面上会出现 GET 请求。
可以看到,只有前两次请求的时间比较长,后面的请求只有几毫秒
Step4. ab 压测
跟 Swoole
的压测一样,压测命令:
ab -n 1000 -c 8 http://127.0.0.1:8010/
-n 1000
: 指定测试会话使用的请求数为1000-c 8
: 并发数为8
使用 octane
的压测结果
[root@localhost octane]# ab -n 1000 -c 8 http://127.0.0.1:8010/test-mysql
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: 127.0.0.1
Server Port: 8010
Document Path: /test-mysql
Document Length: 0 bytes
Concurrency Level: 8
Time taken for tests: 22.446 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 1048000 bytes
HTML transferred: 0 bytes
Requests per second: 44.55 [#/sec] (mean)
Time per request: 179.566 [ms] (mean)
Time per request: 22.446 [ms] (mean, across all concurrent requests)
Transfer rate: 45.60 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 9 178 43.9 180 360
Waiting: 9 178 43.9 180 360
Total: 9 178 43.9 180 360
Percentage of the requests served within a certain time (ms)
50% 180
66% 194
75% 213
80% 221
90% 234
95% 242
98% 250
99% 255
100% 360 (longest request)
**再看看不使用 octane
的压测结果:
[root@localhost octane]# ab -n 1000 -c 8 http://127.0.0.1:8008/test-mysql
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.20.1
Server Hostname: 127.0.0.1
Server Port: 8008
Document Path: /test-mysql
Document Length: 0 bytes
Concurrency Level: 8
Time taken for tests: 52.588 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 1134000 bytes
HTML transferred: 0 bytes
Requests per second: 19.02 [#/sec] (mean)
Time per request: 420.705 [ms] (mean)
Time per request: 52.588 [ms] (mean, across all concurrent requests)
Transfer rate: 21.06 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 78 419 172.2 396 986
Waiting: 78 419 172.2 396 986
Total: 78 419 172.2 396 986
Percentage of the requests served within a certain time (ms)
50% 396
66% 510
75% 568
80% 591
90% 655
95% 706
98% 740
99% 761
100% 986 (longest request)
从上面的两种结果可以看到,RoadRunner
同样能够极大的提升速度。