swoole热更新

在不杀掉进程下进行热更新

官网热更新,这里不做详细说

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

热更新成功

转载于:https://my.oschina.net/u/3004226/blog/1796309

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值