在不殺掉進程下進行熱更新
官網熱更新,這裏不作詳細說
https://wiki.swoole.com/wiki/page/46.html
代碼目錄結構
testS.php ----- 服務端
<?php
$server = new swoole_server("0.0.0.0", 9501);
$server->on('WorkerStart', function ($serv, $worker_id){
// 熱更新
echo "WorkerStart: {$worker_id}\n";
require_once 'test.php';
});
$server->on('connect', function ($server, $fd){
echo "connection open: {$fd}\n";
$test = new Test();
$test->getTime();
});
$server->on('receive', function ($server, $fd, $reactor_id, $data) {
$server->send($fd, "Swoole: {$data}");
$test = new Test();
echo "send fd: {$fd}\n";
echo $data . "\n";
$test->getTime();
});
$server->on('close', function ($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();
testC.php ----- 客戶端
<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
echo "received: {$data}\n";
$cli->send(1);
sleep(5);
$cli->send(2);
});
$client->on("error", function($cli){
echo "connect failed\n";
});
$client->on("close", function($cli){
echo "connection close\n";
});
$client->connect("0.0.0.0", 9501, 0.5);
test.php ----- 操做類
<?php
class Test
{
/**
* [__construct 初始化]
*/
public function __construct()
{
date_default_timezone_set('Asia/Shanghai');
}
public function getTime()
{
echo "test\n";
}
}
運行代碼
服務端
php testS.php
控制檯輸出
WorkerStart: 1
WorkerStart: 0
connection open: 1
test
send fd: 1
hello world
test
send fd: 1
1
test
send fd: 1
2
test
send fd: 1
1
test
客戶端
php testC.php
熱更新
首先把test操做類的方法輸出改成
echo "test12\n";
而後理看進程 ps -auxf | grep php
root 261 0.0 0.0 11128 1028 pts/2 S+ 09:09 0:00 \_ grep php
root 259 0.0 1.0 142740 21656 pts/1 S+ 09:08 0:00 \_ php testC.php
root 253 0.1 13.0 540544 267480 pts/0 Sl+ 09:07 0:00 \_ php testS.php
root 254 0.0 0.2 392368 5960 pts/0 S+ 09:07 0:00 \_ php testS.php
root 257 0.0 0.5 394788 10608 pts/0 S+ 09:07 0:00 \_ php testS.php
root 258 0.0 0.5 394660 10928 pts/0 S+ 09:07 0:00 \_ php testS.php
root 1 0.0 1.0 148104 21580 ? Ss 00:52 0:02 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 5 0.0 1.0 153048 20992 ? S 00:52 0:00 php-fpm: pool www
www-data 6 0.0 1.0 153048 20992 ? S 00:52 0:00 php-fpm: pool www
把254下的worker進程重載
kill -USR1 254
重載後
root 265 0.0 0.0 11128 932 pts/2 S+ 09:10 0:00 \_ grep php
root 259 0.0 1.0 142740 21656 pts/1 S+ 09:08 0:00 \_ php testC.php
root 253 0.0 13.0 540544 267480 pts/0 Sl+ 09:07 0:00 \_ php testS.php
root 254 0.0 0.3 392368 8020 pts/0 S+ 09:07 0:00 \_ php testS.php
root 262 0.0 0.5 394792 10616 pts/0 S+ 09:10 0:00 \_ php testS.php
root 263 0.0 0.5 394792 10616 pts/0 S+ 09:10 0:00 \_ php testS.php
root 1 0.0 1.0 148104 21580 ? Ss 00:52 0:02 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 5 0.0 1.0 153048 20992 ? S 00:52 0:00 php-fpm: pool www
www-data 6 0.0 1.0 153048 20992 ? S 00:52 0:00 php-fpm: pool www
查看控制檯
test
send fd: 1
1
test
send fd: 1
2
test
send fd: 1
1
test
send fd: 1
2
test
send fd: 1
1
test
[2018-04-16 09:11:21 $254.0] NOTICE Server is reloading now.
WorkerStart: 0
WorkerStart: 1
send fd: 1
2
test12
send fd: 1
1
test12
熱更新成功