小白兔晒黑了

导航

 

视频地址 https://www.bilibili.com/video/BV14E411t7T4?p=24&vd_source=4a69745b599dffec877b0fcfe130b092

注意:开发时才使用热加载,实际上线时不使用

1 关于服务的平滑重启

1.1 代码

pro\core\server\HttpServer.php

<?php
namespace Core\Server;


use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

class HttpServer
{
    private $server;
    
    /**
     * HttpServer constructor.
     *
     * @param $server
     */
    public function __construct()
    {
        $this->server  = new \Swoole\Http\Server('0.0.0.0',80);
        //配置参数 https://wiki.swoole.com/#/server/setting
        $this->server->set(array(
          'worker_num'=>1,     //设置启动的 Worker 进程数。【默认值:CPU 核数】
          'daemonize'=>false     // 使用docker 不需要设置守护进程
        ));
        //$this->server->on('request',function ($req,$res){});
        $this->server->on('request',[$this,'onRequset']);
    
        $this->server->on('Start',[$this,'onStart']);
        
        $this->server->on('ShutDown',[$this,'onShutDown']);
        $this->server->on('WorkerStart',[$this,'onWorkerStart']);
        $this->server->on('ManagerStart',[$this,'onManagerStart']);
        //$http->start();
        
    }
    
    public function onWorkerStart(Server $server,int $workerId)
    {
        require_once (__DIR__."./../../test3.php");
        cli_set_process_title("ttt worker"); //设置进程名称
    }
    
    public function onRequset(Request $request,Response $response)
    {
        
        $response->end(showMe());
    }
    
    
    public function onStart(Server $server)
    {
        cli_set_process_title("ttt master"); //设置进程名称
        $mid =  $server->master_pid;   //返回当前服务器主进程的 PID。
        file_put_contents("./ttt.pid",$mid);  //会覆盖
    }
    
    public function onManagerStart(Server $server)
    {
        cli_set_process_title("ttt manager"); //设置进程名称
    }
    public function onShutDown(Server $server)
    {
        echo '关闭了'.PHP_EOL;
        unlink("./ttt.pid");
    }
    
    public function run()
    {
        $this->server->start();
    }
}
View Code

pro\test3.php

<?php
function showMe()
{
    return 'ttt2';
}

1.2 测试

docker exec -it 67e09c446436 sh

 

原先

ps -ef |grep php

 

 现在

ps -ef |grep php

 

 1.3 reload

  • SIGUSR1: 向主进程 / 管理进程发送 SIGUSR1 信号,将平稳地 restart 所有 Worker 进程和 TaskWorker 进程
  • SIGUSR2: 向主进程 / 管理进程发送 SIGUSR2 信号,将平稳地重启所有 Task 进程
kill -USR1 136
# 重启所有worker进程
kill -USR1 主进程PID

# 仅重启task进程
kill -USR2 主进程PID

参考手册 https://wiki.swoole.com/#/server/methods?id=reload

2 添加前置进程、修改代码服务器自动热更新

在我们的http server中加一个启动进程

Server->addProcess

https://wiki.swoole.com/wiki/page/390.html

添加一个用户自定义的工作进程。此函数通常用于创建一个特殊的工作进程,用于监控、上报或者其他特殊的任务。

 

bool Server->addProcess(Process $process);

2.1 代码

\pro\core\init\TestProcess.php

<?php
/**
 * Created by PhpStorm.
 * User: SUN
 * Date: 2022/7/25
 * Time: 3:48
 */

namespace Core\init;
use Swoole\Process;

class TestProcess
{
    public function run()
    {
        
        $process = new Process(function() {
            while (true) {
                echo "ttt".PHP_EOL;
                sleep(3);   
            }
        });
        return  $process;
    }
}
View Code

\pro\core\server\HttpServer.php

 public function run()
    {
        $p = new TestProcess();
        $this->server->addProcess($p->run());
        $this->server->start();
    }

2.2 测试

3 自动热更新

\pro\core\init\TestProcess.php

<?php

namespace Core\init;
use Swoole\Process;

class TestProcess
{
    private $md5file;
    public function run()
    {
        
        $process = new Process(function() {
            while (true) {
                //echo "ttt".PHP_EOL;
                sleep(3);
                //监控文件夹
                //glob() 函数返回一个包含匹配指定模式的文件名或目录的数组。
                //该函数返回一个包含有匹配文件/目录的数组。如果失败则返回 FALSE
                $files = glob(__DIR__."/../../*.php");
                $md5Arr=[];
                foreach ($files as $file){
                    //计算指定文件的md5值
                    $md5Arr[] = md5_file($file);
                }
                
                $md5Value = md5(implode('',$md5Arr));
                if ($this->md5file==''){
                    $this->md5file = $md5Value;
                    continue;  //放弃本次循环中continue语句之后的代码并进行下一次循环
                }
                //strcmp函数是string compare(字符串比较)的缩写,用于比较两个字符串并根据比较结果返回整数。基本形式为strcmp(str1,str2),若str1=str2,则返回零;若str1<str2,则返回负数;若str1>str2,则返回正数
                //文件有改动
                if (strcmp($this->md5file,$md5Value)!==0){
                   
                    echo "代码有改动,重新加载ing".PHP_EOL;
                    $getPid = intval(file_get_contents("./ttt.pid"));
                    Process::kill($getPid,SIGUSR1);
                    $this->md5file = $md5Value;
                    echo "重新加载ed".PHP_EOL;
                }
            }
            
            
        });
        return  $process;
    }
}

 

posted on 2022-07-25 04:12  小白兔晒黑了  阅读(218)  评论(0编辑  收藏  举报