这篇文章主要介绍了关于利用Swoole同时更新多台服务器的代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
一个小型网站的架构, 前面一台负载均衡, 后面几台web服务器. 更新代码成了难题, 一个一个FTP传不现实, 而且容易漏传,导致两个WEB服务器的代码都不一致.
一个简单的想法:
利用Websocket Server发送更新指令, Websocket Client 接收到更新指令, 运行git pull
更新代码.
WebSocket Client有几个角色:
Solider: 接收命令, 不能发送命令
Commander: 发送命令
流程图:
部分代码实现:
<?php //Server.php require_once './Table.php'; use Swoole\WebSocket\Server as WebSocketServer; class Server { protected $server; protected $table; public function __construct($config) { $this->table = new Table(); $this->server = new WebSocketServer($config['host'], $config['port']); $this->server->set($config['configuration']); $this->addEventListener(); } public function addEventListener() { $this->server->on('open', Closure::fromCallable([$this, 'onOpen'])); $this->server->on('message', Closure::fromCallable([$this, 'onMessage'])); $this->server->on('close', Closure::fromCallable([$this, 'onClose'])); } private function onOpen($server, $request) { if ($request->get['role'] == 'commander') { $this->table->commander = $request->fd; } else { $soliders = $this->table->soliders; $soliders[] = $request->fd; $this->table->soliders = $soliders; } } private function onMessage($server, $frame) { if ($frame->fd == $this->table->commander) { $command = $frame->data; foreach ($this->table->soliders as $solider) { $this->server->push($solider, $command); } } else { $this->server->push($frame->fd, "You don not have any right to send message"); } } private function onClose($server, $fd) { $soliders = $this->table->soliders; if (in_array($fd, $soliders)) { unset($soliders[array_search($fd, $soliders)]); } } public function run() { $this->server->start(); } } $server = new Server([ 'host' => '0.0.0.0', 'port' => 8015, 'configuration' => [ 'daemonize' => 1, ] ]); $server->run();
<?php //Client.php use Swoole\Http\Client as WebSocketClient; class Client { protected $protocol; protected $host; protected $port; protected $query; protected $client; protected $allow_events = ['onOpen', 'onMessage', 'onClose']; public function __construct($url) { list('scheme' => $this->protocol, 'host' => $this->host, 'port' => $this->port, 'query' => $this->query) = parse_url($url); if ($this->protocol == 'wss') { echo 'unsupport protocol'; } $this->client = new WebSocketClient($this->host, $this->port); } public function start(Callable $callback) { $this->client->upgrade('/?' . $this->query, $callback); } public function __set($field, $value) { if (in_array($field, $this->allow_events) && is_callable($value)) { $this->client->on(strtolower(substr($field, 2)), $value); } else { echo 'Unsupport Event'; } } }
<?php //Solider.php require_once './Client.php'; function parseCommand($data) { return json_decode($data, true); } function updateCommand() { //you can do something here exec('git pull'); // exec('composer update'); // exec('npm install'); } $ws = new Client('ws://192.168.1.142:8015?role=solider'); $ws->onMessage = function($client, $frame) { list('command' => $command, 'params' => $params) = parseCommand($frame->data); echo $command; switch ($command) { case 'update': updateCommand(); break; } }; $ws->onClose = function($client) { }; $ws->start(function ($client) { }); \Swoole\Process::daemon();
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <button class="btn btn-primary" onclick="update();">更新</button> <script type="text/javascript"> function update() { var ws = new WebSocket("ws://192.168.1.142:8015?role=commander"); ws.onopen = function() { // Web Socket 已连接上,使用 send() 方法发送数据 ws.send(JSON.stringify({"command": "update", "params": {}})); }; ws.onmessage = function (evt) { var received_msg = evt.data; alert(received_msg); }; ws.onclose = function() { // 关闭 websocket alert("连接已关闭..."); }; } </script> </body> </html>
完整代码:
https://gitee.com/shuizhuyu/P...
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是利用Swoole同时更新多台服务器的代码的详细内容。更多信息请关注PHP中文网其他相关文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具