Easyswoole利用Nginx实现服务不中断部署

1. 介绍

近几天有幸接触到Nginx反向代理、负载均衡相关知识,为了加深印象,感觉还是有必要自己去踩一遍坑。

2. 学习案例

EasySwoole+git+Nginx 实现服务的不中断部署

    1. 先部署9501服务
    1. 单起一个进程,定时轮询Git分支是否有新版本发布
    1. 如有新版本发布,clone一份
    1. composer update 更新库
    1. 启动9502服务
  • 6 更改nginx配置为9502并重启

只要有新版本发布,就轮询上面那几个步骤

整个过程的简单架构图

image.png

3. 提前需要了解的知识点

  1. Nginx负载均衡和反向代理
  2. EasySwoole自定义进程
  3. Nginx reload 和 restart的区别

4. Nginx 配置

nginx.conf

当有新版本发布的时候EasySwoole自定义进程会将nginx.conf 的端口改为最新服务


worker_processes  1;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
 
    keepalive_timeout  65;
    
    // 轮询配置(这里是重点)
    upstream  easyswoole.relase.com {
           server    127.0.0.1:9501;
    }

    server {
        listen       8080;
        server_name  localhost;
    }

    include servers/*;
}

es-release.conf
server {
        listen       80;
        server_name  easyswoole.relase.com;

        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://easyswoole.relase.com; // 这里是重点
        }

        access_log /usr/local/etc/nginx/logs/es.access.log main;
        error_log /usr/local/etc/nginx/logs/es.error.log error;
}%

5. EasySwoole 代码实现

只是为了学习记录,代码有些粗糙,并且这种定时检查代码是否有新版本,最好单独去做,比如用shell脚本

自定义进程文件
<?php
namespace App\Relase;
use EasySwoole\Component\Process\AbstractProcess;
use Swoole\Coroutine;
class Relase extends AbstractProcess
{

    protected function run($arg)
    {
        go(static function () {
            while (true)
            {

                $shellLog = ' 2>> /Users/xxx/sites/shell.log';
                error_log('开始检测代码是否更新5'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');

                // 检查Git是否有新代码发布
                $diffExec = 'cd ' .EASYSWOOLE_ROOT. '; git fetch; git diff --stat master origin/master;';
                $pullResult = exec($diffExec);
                error_log(json_encode($pullResult), 3, '/Users/xxx/sites/es-log.log');

                if ($pullResult !== '') {
                    error_log('有新版本发布'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');

                    // 新版本项目的目录
                    $newVersionPath = '/Users/xxx/sites/relase-'.time();

                    // 开始clone, 初始化代码
                    $cloneExec = "git clone https://github.com/huizhang-Easyswoole/release.git {$newVersionPath} {$shellLog};cd {$newVersionPath} {$shellLog};composer update {$shellLog}; {$shellLog}";
                    $res = exec($cloneExec, $a, $b);
                    error_log('新版本代码clone'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');

                    // 判断当前是哪个端口正在服务
                    $lsofExec = "lsof -i:9501 {$shellLog}";
                    $lsofResult = exec($lsofExec);
                    $newPort = 9501;
                    $oldPort = 9502;
                    if ($lsofResult !== '') {
                        $newPort = 9502;
                        $oldPort = 9501;
                    }

                    // 将另一个闲置的端口,替换到新版本中
                    error_log('开始替换端口'.$newPort.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');

                    $devConfig = file_get_contents($newVersionPath.'/dev.php');
                    $devConfig = str_replace($oldPort, $newPort, $devConfig);
                    file_put_contents($newVersionPath.'/dev.php', $devConfig);

                    // 启动新服务(这一刻新旧服务是同时存在的)
                    error_log('新服务启动'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');
                    $startExec = "cd {$newVersionPath}; php easyswoole start d {$shellLog}";
                    exec($startExec);

                    // 替换nginx配置
                    error_log('开始替换ng端口'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');
                    $ngConfigPath = '/usr/local/etc/nginx/nginx.conf';
                    $ngConfig  = file_get_contents($ngConfigPath);
                    $ngConfig = str_replace($oldPort, $newPort, $ngConfig);
                    file_put_contents($ngConfigPath, $ngConfig);

                    // 重启Nginx
                    error_log('重启ng'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');
                    $reloadNgExec = "nginx -s reload {$shellLog}";
                    exec($reloadNgExec);

                    // 停掉旧服务
                    error_log('旧服务停掉'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');
                    $stopExec = "cd ".EASYSWOOLE_ROOT."; php easyswoole stop {$shellLog}";
                    exec($stopExec);

                    // 每30秒同步一次代码
                    Coroutine::sleep(30);
                } else {
                    error_log('无新版本'.PHP_EOL, 3, '/Users/xxx/sites/es-log.log');

                }

            }
        });

    }

}
进程注册
<?php
namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use App\Relase\Relase;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.1
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        $process = new Relase('Es-relase');
        ServerManager::getInstance()->getSwooleServer()->addProcess($process->getProcess());
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}

7. 测试

绑定host
127.0.0.1 easyswoole.relase.com
访问easyswoole.relase.com
image.png
查看Nginx配置的端口
➜  nginx cat nginx.conf | grep 950
           server    127.0.0.1:9501;
发布新版本

重新clone一份代码,更改内容提交。

查看Nginx配置的端口
➜  nginx cat nginx.conf | grep 950
           server    127.0.0.1:9502;

这样就简单实现了利用Nginx的反向代理+负载均衡功能实现了EasySwoole的服务不中断部署功能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容