平滑重启使PHP文件有效测试
在 swoole 文档中有这么一说明
" 平滑重启只对onworkerstart或onreceive等在worker进程中include/require的php文件有效,server启动前就已经include/require的php文件,不能通过平滑重启重新加载 "
让我们来测试一下:
# php http_server.php
workerstart
workerstart
访问 https://www.test-yy.com
页面内容输出 " hello swoole test "
命令行中输出:
test1
request 100test1
当我们将
test.php
test_request.php
代码中的 test1 替换成 test2 时
访问 https://www.test-yy.com
命令行中输出:
test1
request 88test2
# ps -ef | grep php
# kill -usr1 8155 // 平滑重启,8155(主进程/管理进程 id)
再次访问 https://www.test-yy.com
命令行中输出:
test1
request 88test2
以上结果说明了 include/require ,再 server 启动前载入的php文件,不能通过平滑重启重新加载
测试代码:
http_server.php
<?php
echo 'shell start';
define('__root__', realpath('.'));
include __root__.'/test.php';
$http = new swoole_http_server("127.0.0.1", 9501);
$http->set([
'worker_num'=>2,
'max_request'=>50
]);
$http->on('workerstart', function ($serv, $worker_id){
echo "workerstart \n";
});
$http->on('request', function ($request, $response) {
gettest();
include __root__.'/test_request.php';
$response->header("content-type", "text/html; charset=utf-8");
$response->end(gettplcontent());
});
$http->start();
function gettplcontent() {
return "hello swoole test \n";
}
?>
test.php
<?php
function gettest() {
echo "test1 \n";
}
?>
test_request.php
<?php
echo 'request '.rand(1,100)."test1 \n";
?>
nginx 配置
test.conf
server {
listen 80;
server_name www.test-yy.com;
...
location ~\.php$ {
proxy_http_version 1.1;
proxy_set_header connection "keep-alive";
proxy_set_header x-real-ip $remote_addr;
proxy_pass https://127.0.0.1:9501;
}
}