swoole 热重启

本文介绍了如何使用swoole进行热重启操作,详细解析了swoole_reload_server.php、reload.sh和reload_page.php的流程。通过修改reload_page.php中的代码并执行.sh脚本,观察到服务器状态的变化,从而实现swoole服务的热重启。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考原文 https://blog.youkuaiyun.com/ldy3243942/article/details/40712475

代码用的原来只是自己加了一些操作流程

 swoole_reload_server.php

<?php
/**
 * Example for swoole reload php code without shutdowning server.
 *
 * @author Lancelot https://github.com/LinkedDestiny
 */
class Server
<?php
/**
 * Example for swoole reload php code without shutdowning server.
 *
 * @author Lancelot https://github.com/LinkedDestiny
 */
class Server
{
        private $serv;
        public function __construct() {
        $this->serv = new swoole_server("0.0.0.0", 9501);
        $this->serv->set(array(
            'worker_num' => 8,
            'daemonize' => false,
            'max_request' => 10000,
            'dispatch_mode' => 2,
            'debug_mode'=> 1,
        ));
        $this->serv->on('Start', array($this, 'onStart'));
        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));
        $this->serv->start();
    }
    public function onStart( $serv ) {
        echo "Start\n";
        cli_set_process_title("reload_master");
    }
    public function onWorkerStart( $serv , $worker_id) {
        require_once "reload_page.php";
        Test();
    }
    public function onConnect( $serv, $fd, $from_id ) {
        echo "Client {$fd} connect\n";

    }
    public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
        echo "Get Message From Client {$fd}:{$data}\n";
    }
    public function onClose( $serv, $fd, $from_id ) {
        echo "Client {$fd} close connection\n";
    }
}
new Server();

reload.sh

echo "Reloading..."
cmd=$(pidof reload_master)

kill -USR1 "$cmd"
echo "Reloaded"

reload_page.php 

<?php
/**
 * If you change this function and want
 * swoole_server to use the new function,
 * just run 'reload.sh' to send a restart
 * signal to swoole_server.
 */
function Test() {
        echo "This is not a php file\n";
}


操作流程如下



这个时候 reload_page.php 函数还是



大神代码里的注释试修改这个 那么我们开始修改 再执行.sh脚本 看看是否有变化

尝试修改为


然后执行脚本呢  脚本 记得 chmod a+x 授权一下 


发现这个原来的sever改变了 


变成了 i‘m  fun  这里没加换行  大概就这样实现了热重启 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值