php中Swoole的熱更新實現程式碼例項
阿新 • • 發佈:2021-03-05
使用swoole_http_server
替代php-fpm
後,由於php
長駐記憶體,修改了程式碼不能實時除錯,需要去手動去重啟服務,很是不方便,決定使用inotify
來監控檔案狀態的改變,來給swoole
傳送reload
訊號,來實現swoole
的熱更新。
如何安裝inotify
就不寫了http://www.cppcns.com,安裝之http://www.cppcns.com後可以建立一個指令碼檔案,如php_reload.sh
:
#!/bin/sh # src 需要監控的地址 src=/home/server/Project/test/app/ /usr/bin/inotifywait -rmq -e create,modify,delete $src | while read event do /home/server/Project/test/bin/httpserver reload done
linux shell 寫swoole重啟指令碼
程式碼如下
#!/bin/sh kill `lsof -t -i:9501` sleep 2 php /data/web/mircoweb/wwwroot/Public/swoole.php sleep 1 netstat -ntlp
如果不支援lsof命令 那就yum install lsof安裝下吧
swoole服務平滑重啟
1. reload.sh指令碼
echo "loading..." pid="pidof live_name" echo $pid kill -USR1 $pid echo "loading success"
2. linux中執行
sh reload.sh
程式碼用的原來只是自己加了一些操作流程
swoole_reload_server.php
<?php clawww.cppcns.comss 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','onWorkerStart')); $this->serv->on('Connect','onConnect')); $this->serv->on('Receive','onReceive')); $this->serv->on('Close','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,$from_id,$data ) { echo "Get Message From Client {$fd}:{$data}\n"; } public function onClose( $serv,$from_id ) { www.cppcns.com60; echo "Client {$fd} close connection\n"; } } new Server();
reload.sh
echo "Reloading..."
cmd=$(pidof rehttp://www.cppcns.comload_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"; }
到此這篇關於php中SWOOLE的熱更新實現程式碼例項的文章就介紹到這了,更多相關php中SWOOLE的熱更新實現內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!