詳細帶你了解什么是swoole 熱重啟?


1. swoole 服務端文件: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();

2.重啟服務腳本:reload.sh

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

kill -USR1 $cmd
echo "Reloaded"

3.重啟測試頁: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";
}

操作流程如下

4e4f2aa12de8a9771f25c77b9b01dae7.png

這個時候 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";
}

 那么我們開始修改 再執行.sh腳本 看看是否有變化

嘗試修改為

e24f478a481a44ed827e2842d1a9ae40.png

然后執行腳本呢  腳本 記得 chmod a+x 授權一下 

c2332d76f400a84ec74fb3310aea5b3f.png

發現這個原來的sever改變了 

63e73a4ebeb51ff6ebcaaf523d91a5b6.png

變成了 i‘m  fun  這里沒加換行  大概就這樣實現了熱重啟 

求一鍵三連:點贊、轉發、在看

084acbcfbb7d2c1b076f1084ab3ca205.png


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM