Blogger Information
Blog 35
fans 0
comment 0
visits 29266
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php中Swoole的热更新实现代码实例
P粉526161432
Original
515 people have browsed it
  1. 这篇文章主要介绍了phpSWOOLE的热更新实现代码实例,热更新是比较常用的技术,有需要同学可以研究下
  1. 使用swoole_http_server替代php-fpm后,由于php长驻内存,修改了代码不能实时调试,需要去手动去重启服务,很是不方便,决定使用inotify来监控文件状态的改变,来给swoole发送reload信号,来实现swoole的热更新。
  2. 如何安装inotify就不写了,安装之后可以建立一个脚本文件,如php_reload.sh:
  1. #!/bin/sh
  2. # src 需要监控的地址
  3. src=/home/server/Project/test/app/
  4. /usr/bin/inotifywait -rmq -e create,modify,delete $src | while read event
  5. do
  6. /home/server/Project/test/bin/httpserver reload
  7. done
  1. linux shell swoole重启脚本
  1. 代码如下
  1. #!/bin/sh
  2. kill `lsof -t -i:9501`
  3. sleep 2
  4. php /data/web/mircoweb/wwwroot/Public/swoole.php
  5. sleep 1
  6. netstat -ntlp
  1. 如果不支持lsof命令 那就yum install lsof安装下吧
  2. swoole服务平滑重启
  3. 1. reload.sh脚本
  1. echo "loading..."
  2. pid="pidof live_name"
  3. echo $pid
  4. kill -USR1 $pid
  5. echo "loading success"
  1. 2. linux中执行
  2. sh reload.sh
  3. 代码用的原来只是自己加了一些操作流程
  4. swoole_reload_server.php
  1. <?php
  2. class Server
  3. {
  4. private $serv;
  5. public function __construct() {
  6. $this->serv = new swoole_server("0.0.0.0", 9501);
  7. $this->serv->set(array(
  8. 'worker_num' => 8,
  9. 'daemonize' => false,
  10. 'max_request' => 10000,
  11. 'dispatch_mode' => 2,
  12. 'debug_mode'=> 1,
  13. ));
  14. $this->serv->on('Start', array($this, 'onStart'));
  15. $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
  16. $this->serv->on('Connect', array($this, 'onConnect'));
  17. $this->serv->on('Receive', array($this, 'onReceive'));
  18. $this->serv->on('Close', array($this, 'onClose'));
  19. $this->serv->start();
  20. }
  21. public function onStart( $serv ) {
  22. echo "Start\n";
  23. cli_set_process_title("reload_master");
  24. }
  25. public function onWorkerStart( $serv , $worker_id) {
  26. require_once "reload_page.php";
  27. Test();
  28. }
  29. public function onConnect( $serv, $fd, $from_id ) {
  30. echo "Client {$fd} connect\n";
  31. }
  32. public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
  33. echo "Get Message From Client {$fd}:{$data}\n";
  34. }
  35. public function onClose( $serv, $fd, $from_id ) {
  36. echo "Client {$fd} close connection\n";
  37. }
  38. }
  39. new Server();
  1. reload.shreload.sh
  1. echo "Reloading..."
  2. cmd=$(pidof reload_master)
  3. kill -USR1 "$cmd"
  4. echo "Reloaded"
  1. reload_page.php
  1. <?php
  2. /**
  3. * If you change this function and want
  4. * swoole_server to use the new function,
  5. * just run 'reload.sh' to send a restart
  6. * signal to swoole_server.
  7. */
  8. function Test() {
  9. echo "This is not a php file\n";
  10. }
  1. 到此这篇关于phpSWOOLE的热更新实现代码实例的文章就介绍到这了,更多相关phpSWOOLE的热更新实现内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post