通过扫描指定的要扫描的目录,把所有文件找出来,分别md5 连接字符串,最后再md5返回

启动定时器,扫描,当前的加密值和以前一样不管,否则就重启服务,把当前赋值给旧值 。

httpServer.php

<?php

namespace Wang\Core\Server;

use Wang\Core\Bean\BeanFactory;
use Wang\Core\Route\Annotation\Mapping\RequestMapping;
use Wang\Core\Route\Annotation\Parser\RequestMappingParser;
use Wang\Core\Route\Route; class HttpServer
{ public $server; public function run()
{
$config = BeanFactory::make("config")->get("http-server");
$this->server = new \Swoole\Http\Server($config['host'], $config['port']);
$this->server->set($config['setting']);
$this->server->on("request", [$this, 'httpRequest']);
$this->server->on("workerstart", [$this, "workerStart"]);
$this->server->on("start", [$this, "start"]); $this->server->start();
} public function start($server)
{
echo "main worker 启动" . PHP_EOL;
$dirs = BeanFactory::make("config")->get('reload_dirs');
$reload = bean("reload");
\Swoole\Timer::Tick(3000, function () use ($reload, $dirs) {
//echo "定时器" . PHP_EOL;
$newMd5 = $reload->md5Dirs($dirs);
if ($reload->needReload($newMd5)) {
$this->server->reload();
}
});
} /**
* onworker start 回调
* @param $server
* @param $workerId
* author: brady
* date: 2020/7/23 16:03
*/
public function workerStart($server, $workerId)
{
echo "worker " . $workerId . " 启动" . PHP_EOL;
$this->loadAnnotations();
} /**
* http 请求接受
* @param $request
* @param $response
* author: brady
* date: 2020/7/23 16:03
*/
public function httpRequest($request, $response)
{
if ($request->server['request_uri'] != "/favicon.ico") {
echo "client connect fd:" . $request->fd . PHP_EOL;
$path_info = $request->server['path_info'];
$method = $request->server['request_method']; $content = Route::dispatch($method, $path_info); $response->header("Content-Type", "text/html; charset=utf-8");
$response->end($content);
} } /**
* 获取 apppath的路由 下面所有文件,得到注解路由 放到路由属性$routes 对象里面
*/
public function loadAnnotations()
{
get_files_by_tree(APP_PATH, $dirs, $filter = "controller");
if (!empty($dirs)) {
foreach ($dirs as $file) {
// 根据绝对路径文件名 获取带命名空间的类
$className = getClassNameByFilePath($file);
$obj = new $className;
$reflect = new \ReflectionObject($obj);
$methods = $reflect->getMethods();
if (!empty($methods)) {
foreach ($methods as $method) {
if ($method->getName() != '__construct') {
// 注解对象 设置注解路由相关信息
$annotation = new RequestMapping($reflect, $method); // 解析 将上一步收集到的信息组装后添加到路由数组里面 $routes
(new RequestMappingParser())->parse($annotation);
}
}
}
}
}
}
}

  

reload.php

<?php

namespace Wang\Core\Reload;

use Wang\Core\Bean\BeanFactory;

class Reload
{ protected $oldMd5 = ''; public function __construct()
{
$dirs = BeanFactory::make("config")->get('reload_dirs');
$this->oldMd5 = $this->md5Dirs($dirs);
} /**
* 是否需要重启
* author: brady
* date: 2020/7/23 16:42
*/
public function needReload($newMd5)
{
if($newMd5 != $this->oldMd5){
$this->oldMd5 = $newMd5;
return true;
} else {
return false;
}
} /**
* 对传入的目录所有文件进行递归加密
* @param array $dirs
* author: brady
* date: 2020/7/23 16:42
*/
public function md5Dirs($dirs)
{
$files = [];
foreach($dirs as $dir){
$files += get_files_by_tree($dir);
} $md5File = '';
foreach($files as $file){
$md5File .= md5_file($file);
} return md5($md5File);
} }

  

每当代码改动 会重启服务

[2020-07-24 10:29:46 $2346.0]	INFO	Server is reloading all workers now
worker 0 启动
client connect fd:1
client connect fd:1
[2020-07-24 10:30:43 $2346.0] INFO Server is reloading all workers now
worker 0 启动
[2020-07-24 10:31:43 $2346.0] INFO Server is reloading all workers now
worker 0 启动

  

代码完整地址

https://github.com/brady-wang/WangFrm.git

swoole热启动的更多相关文章

  1. 在Laravel中使用swoole来取代nginx作为http服务器

    1.是什么限制Laravel框架的速度? Laravel框架启动的时候需要加载很多文件,再加上其出了名的生态环境好,所以在开发过程中我们就会发现有非常多的已经造好的轮子,这也就使得Laravel的一次 ...

  2. swoole扩展安装

    1Swoole扩展的编译安装 Swoole扩展是按照php标准扩展构建的.使用phpize来生成php编译配置,./configure来做编译配置检测,make和make install来完成安装. ...

  3. 使用Swoole加速Laravel(正式环境中)

    1 Laravel的速度瓶颈在哪? 1.1 已有的一些优化方法 1.1.1 laravel官方提供了一些优化laravel的优化方法 php artisan optimize php artisan ...

  4. swoole运行模式加速laravel应用的详细介绍

    本篇文章给大家带来的内容是关于swoole运行模式加速laravel应用的详细介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 一.Swoole Swoole号称重新定义了PHP, ...

  5. 编译安装PHP7并安装Redis扩展Swoole扩展

    编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机器上已经有编译安装过php5.3以上的版本,从而依赖库都有了 本php7是编译成fpm-php 使用的, 如果是apache那么 ...

  6. 使用php+swoole对client数据实时更新(下)

    上一篇提到了swoole的基本使用,现在通过几行基本的语句来实现比较复杂的逻辑操作: 先说一下业务场景.我们目前的大多数应用都是以服务端+接口+客户端的方式去协调工作的,这样的好处在于不论是处在何种终 ...

  7. 使用php+swoole对client数据实时更新(上)

    如果想对一个列表做实时的更新,传统的做法是采用轮询的方式.以web为例,通过Ajax定时请求服务端然后获取数据显示在页面.这种方式实现简单,缺点就是浪费资源. HTTP1.1新增加了对websocke ...

  8. [Linux][PHP]安装swoole扩展

    1.下载swoole 2.解压并配置 /usr/local/php/bin/phpize ./configure --enable-swoole-debug --enable-sockets --en ...

  9. 被swoole坑哭的PHP程序员

    被swoole坑哭的PHP程序员 2015-09-16 09:57 文帅营 博客园 字号:T | T 首先说一下对swoole的理解:披着PHP外衣的C程序.很多PHPer朋友看到swoole提供的强 ...

随机推荐

  1. Jenkins下Vue自动部署(一)

    1,服务器上安装docker http://www.runoob.com/docker/ubuntu-docker-install.html?tdsourcetag=s_pctim_aiomsg 2, ...

  2. 蓝奏网盘API

    蓝奏云网盘API 2.0 基于Python3实现,最强的蓝奏云API~ 蓝奏云注册 更新说明 修复了登录时 formhash 错误的问题 解决了多次上传大文件被限制的问题 #3 细化 API 接口的功 ...

  3. 一文解开java中字符串编码的小秘密

    目录 简介 Unicode的发展史 Unicode详解 UTF-8 UTF-16 UTF-32 Null-terminated string 和变种UTF-8 简介 在本文中你将了解到Unicode和 ...

  4. 如何将tensorflow1.x代码改写为pytorch代码(以图注意力网络(GAT)为例)

    之前讲解了图注意力网络的官方tensorflow版的实现,由于自己更了解pytorch,所以打算将其改写为pytorch版本的. 对于图注意力网络还不了解的可以先去看看tensorflow版本的代码, ...

  5. 我告诉你一个 AtomicInteger 的惊天大秘密

    i++ 不是线程安全的操作,因为它不是一个原子性操作. 那么,如果我想要达到类似 i++ 的这种效果,我应该使用哪些集合或者说工具类呢? 在 JDK1.5 之前,为了确保在多线程下对某基本数据类型或者 ...

  6. NuxtJS快速入门

    服务器端渲染(SSR) 知识储备 ES6 Nodejs Vue React Angular 什么是服务器端渲染 前端渲染:html页面作为静态文件存在,前端请求时后端不对该文件做任何内容上的修改,直接 ...

  7. 【转】Locust 性能测试-小案例(1)-环境搭建

    说在前面的话:从这节课开始,将讲解Locust作为一款测试工具,要怎么去应用.首先是"小案例"的系列文章,主要是给大家讲解locustfile也就是场景模拟的一些模式和方法.等到& ...

  8. linux学习(二)认识Linux

    一.Linux系统的组成 linux内核(linus 团队管理) shell:用户与内核交互的接口 文件系统:ext3.ext4等.windows 有 fat32 .ntfs 第三方应用软件 二.Li ...

  9. 解析形如(k,v)(k,v)(k,v)字符串

    有时根据需要会将map数据格式化成(k,v)(k,v)(k,v)--字符串,之后需要还原,下面代码实现了还原过程 1 void SplitString(const string& s, vec ...

  10. Spring--AOP的见解

    AOP是指面向切面编程,与JAVA中的动态代理有很深的渊源. 在使用Spring框架时,AOP编程能简化很多繁杂的步骤,精简代码. 切面:横切关注点(跨越程序中多个模块的功能),被模块化的特殊对象,也 ...