前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >详细带你了解什么是swoole 热重启?

详细带你了解什么是swoole 热重启?

作者头像
码农编程进阶笔记
发布2021-11-01 14:55:34
3260
发布2021-11-01 14:55:34
举报

1. swoole 服务端文件:swoole_reload_server.php

代码语言:javascript
复制
<?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

代码语言:javascript
复制
echo "Reloading..."
cmd=$(pidof reload_master)

kill -USR1 $cmd
echo "Reloaded"

3.重启测试页:reload_page.php

代码语言:javascript
复制
<?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 函数还是

代码语言:javascript
复制
<?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脚本 看看是否有变化

尝试修改为

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

发现这个原来的sever改变了

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-10-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农编程进阶笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档