应用 Composer 装置
composer require easyswoole/easyswoole=3.x php vendor/bin/easyswoole install
启动框架
php easyswoole start
nginx转发
server { root /data/wwwroot/; server_name local.easyswoole.com; location / { proxy_http_version 1.1; proxy_set_header Connection "keep-alive"; proxy_set_header X-Real-IP $remote_addr; if (!-e $request_filename) { proxy_pass http://127.0.0.1:9501; } if (!-f $request_filename) { proxy_pass http://127.0.0.1:9501; } } }
proxy_set_header X-Real-IP $remote_addr; 猎取实在IP地点
运转你的hellword
project 名目部署目次 ---------------------------------- ├─App 使用目次 │ └─HttpController 使用的管制器目次 │ └─Index.php 默许管制器文件 ----------------------------------
Index.php
<?php namespace App\HttpController; use EasySwoole\Http\AbstractInterface\Controller; class Index extends Controller { function index() { // TODO: Implement index() method. $this->response()->write('hello world'); } }
编纂根目次下的 composer.json 文件,注册使用的定名空间
{ "autoload": { "psr-4": { "App\\": "App/" } }, "require": { "easyswoole/easyswoole": "3.x-dev" } }
意义就是设置主动加载
最初执行composer dumpautoload 饬令更新定名空间,能够开端编写营业逻辑
# 更新定名空间映照 composer dumpautoload # 启动框架 php easyswoole start 目次构造 project 名目部署目次 ├─App 使用目次(能够有多个) │ ├─HttpController 管制器目次 │ │ └─Index.php 默许管制器 │ └─Model 模子文件目次 ├─Log 日记文件目次 ├─Temp 暂时文件目次 ├─vendor 第三方类库目次 ├─composer.json Composer架构 ├─composer.lock Composer锁定 ├─EasySwooleEvent.php 框架全局事情 ├─easyswoole 框架治理剧本 ├─easyswoole.install 框架装置锁定文件 ├─dev.php 开发设置装备摆设文件 ├─produce.php 消费设置装备摆设文件
生命周期,也就是流程
设置装备摆设文件阐明
<?php /** * Created by PhpStorm. * User: yf * Date: 2019-01-01 * Time: 20:06 */ return [ 'SERVER_NAME' => "EasySwoole",//效劳名 'MAIN_SERVER' => [ 'LISTEN_ADDRESS' => '0.0.0.0',//监听地点 'PORT' => 9501,//监听端口 'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER 'SOCK_TYPE' => SWOOLE_TCP,//该设置装备摆设项当为SERVER_TYPE值为TYPE_SERVER时无效 'RUN_MODEL' => SWOOLE_PROCESS,// 默许Server的运转模式 'SETTING' => [// Swoole Server的运转设置装备摆设( 完好设置装备摆设可见[Swoole文档](https://wiki.swoole.com/wiki/page/274.html) ) 'worker_num' => 8,//运转的 worker过程数目 'max_request' => 5000,// worker 实现该数目的申请后将加入,避免内存溢出 'task_worker_num' => 8,//运转的 task_worker 过程数目 'task_max_request' => 1000,// task_worker 实现该数目的申请后将加入,避免内存溢出 'reload_async' => true,//设置异步重启开关。设置为true时,将启用异步平安重启特点,Worker过程会期待异步事情实现后再加入。 'task_enable_coroutine' => true//开启后主动正在onTask回调中创立协程 ] ], 'TEMP_DIR' => null,//暂时文件寄存的目次 'LOG_DIR' => null,//日记文件寄存的目次 'CONSOLE' => [//console管制台组件设置装备摆设 'ENABLE' => true,//能否开启 'LISTEN_ADDRESS' => '127.0.0.1',//监听地点 'PORT' => 9500,//监听端口 'USER' => 'root',//验权用户名 'PASSWORD' => '123456'//验权用户名 ], 'FAST_CACHE' => [//fastCache组件 'PROCESS_NUM' => 0,//过程数,年夜于0才开启 'BACKLOG' => 256,//数据行列步队缓冲区巨细 ], 'DISPLAY_ERROR' => true,//能否开启谬误显示 ];
设置装备摆设操作类
EasySwoole\Config 类
toArray 办法猎取全副设置装备摆设,load 办法重载全副设置装备摆设
假如设置了修正,需求更新设置装备摆设的意义
<?php $instance = \EasySwoole\EasySwoole\Config::getInstance(); // 猎取设置装备摆设 按层级用点号分隔 $instance->getConf('MAIN_SERVER.SETTING.task_worker_num'); // 设置设置装备摆设 按层级用点号分隔 $instance->setConf('DATABASE.host', 'localhost'); // 猎取全副设置装备摆设 $conf = $instance->getConf(); // 用一个数组笼罩以后设置装备摆设项 $conf['DATABASE'] = [ 'host' => '127.0.0.1', 'port' => 13306 ]; $instance->load($conf);
增加用户设置装备摆设项
'MYSQL' => [ 'host' => '192.168.75.1', 'port' => '3306', 'user' => 'root', 'timeout' => '5', 'charset' => 'utf8mb4', 'password' => 'root', 'database' => 'cry', 'POOL_MAX_NUM' => '20', 'POOL_TIME_OUT' => '0.1', ], /*################ REDIS CONFIG ##################*/ 'REDIS' => [ 'host' => '127.0.0.1', 'port' => '6379', 'auth' => '', 'POOL_MAX_NUM' => '20', 'POOL_MIN_NUM' => '5', 'POOL_TIME_OUT' => '0.1', ],
消费与开发设置装备摆设别离
默许为开发模式,加载 dev.php
天生
php easyswoole start produce
DI注入设置装备摆设
也就是依赖注入
<?php Di::getInstance()->set(SysConst::ERROR_HANDLER,function (){});//设置装备摆设谬误解决回调 Di::getInstance()->set(SysConst::SHUTDOWN_FUNCTION,function (){});//设置装备摆设剧本完结回调 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_NAMESPACE,'App\\HttpController\\');//设置装备摆设管制器定名空间 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_MAX_DEPTH,5);//设置装备摆设http管制器最年夜解析层级 Di::getInstance()->set(SysConst::HTTP_EXCEPTION_HANDLER,function (){});//设置装备摆设http管制器异样回调 Di::getInstance()->set(SysConst::HTTP_CONTROLLER_POOL_MAX_NUM,15);//http管制器工具池最年夜数目
静态设置装备摆设
每一次开端了,是上一次的过程,比方你关上了旧版,如今更新了新版,然而旧版仍是开着,不重启动,也就是不断旧版,如今有个静态设置装备摆设,示意能够滑润圆滑的修正
<?php Config::getInstance()->setDynamicConf('test_config_value', 0);//设置装备摆设一个静态设置装备摆设项 $test_config_value_1 = Config::getInstance()->getDynamicConf('test_config_value');//猎取一个设置装备摆设 Config::getInstance()->delDynamicConf('test_config_value');//删除了一个设置装备摆设
效劳治理剧本
php easyswoole install 装置easySwoole start 启动easySwoole stop 中止easySwoole(守护模式下应用) reload 重启easySwoole(守护模式下应用) help 查看饬令的协助信息 easyswoole help -start
守护模式启动
php easyswoole start d
线上
php easyswoole start produce
中止
php easyswoole stop
重启效劳
php easyswoole reload 只重启task过程 php easyswoole reload all 重启task + worker过程
文件热加载
因为 swoole 常驻内存的特点,修正文件后需求重启worker过程能力将被修正的文件从新载入内存中
处理:Process的形式完成文件变化主动进行效劳重载
新建文件 App/Process/HotReload.php 并增加以下内容,也能够放正在其余地位,请对报命名空间
<?php /** * Created by PhpStorm. * User: evalor * Date: 2018-11-26 * Time: 23:18 */ namespace App\Process; use EasySwoole\Component\Process\AbstractProcess; use EasySwoole\EasySwoole\ServerManager; use EasySwoole\Utility\File; use Swoole\Process; use Swoole\Table; use Swoole\Timer; /** * 暴力热重载 * Class HotReload * @package App\Process */ class HotReload extends AbstractProcess { /** @var \swoole_table $table */ protected $table; protected $isReady = false; protected $monitorDir; // 需求监控的目次 protected $monitorExt; // 需求监控的后缀 /** * 启动按时器进行轮回扫描 */ public function run($arg) { // 此处指定需求监督的目次 倡议只监督App目次下的文件变卦 $this->monitorDir = !empty($arg['monitorDir']) ? $arg['monitorDir'] : EASYSWOOLE_ROOT . '/App'; // 指定需求监控的扩大名 没有属于指定类型的的文件 正视变卦 没有重启 $this->monitorExt = !empty($arg['monitorExt']) && is_array($arg['monitorExt']) ? $arg['monitorExt'] : ['php']; if (extension_loaded('inotify') && empty($arg['disableInotify'])) { // 扩大可用 优先应用扩大进行解决 $this->registerInotifyEvent(); echo "server hot reload start : use inotify\n"; } else { // 扩大不成历时 进行暴力扫描 $this->table = new Table(512); $this->table->column('mtime', Table::TYPE_INT, 4); $this->table->create(); $this->runComparison(); Timer::tick(1000, function () { $this->runComparison(); }); echo "server hot reload start : use timer tick comparison\n"; } } /** * 扫描文件变卦 */ private function runComparison() { $startTime = microtime(true); $doReload = false; $dirIterator = new \RecursiveDirectoryIterator($this->monitorDir); $iterator = new \RecursiveIteratorIterator($dirIterator); $inodeList = array(); // 迭代目次全副文件进行反省 foreach ($iterator as $file) { /** @var \SplFileInfo $file */ $ext = $file->getExtension(); if (!in_array($ext, $this->monitorExt)) { continue; // 只反省指定类型 } else { // 因为修正文件称号 其实不需求从新载入 能够基于inode进行监控 $inode = $file->getInode(); $mtime = $file->getMTime(); array_push($inodeList, $inode); if (!$this->table->exist($inode)) { // 新建文件或修正文件 变卦了inode $this->table->set($inode, ['mtime' => $mtime]); $doReload = true; } else { // 修正文件 但未发作inode变卦 $oldTime = $this->table->get($inode)['mtime']; if ($oldTime != $mtime) { $this->table->set($inode, ['mtime' => $mtime]); $doReload = true; } } } } foreach ($this->table as $inode => $value) { // 迭代table寻觅需求删除了的inode if (!in_array(intval($inode), $inodeList)) { $this->table->del($inode); $doReload = true; } } if ($doReload) { $count = $this->table->count(); $time = date('Y-m-d H:i:s'); $usage = round(microtime(true) - $startTime, 3); if (!$this->isReady == false) { // 监测到需求进行热重启 echo "severReload at {$time} use : {$usage} s total: {$count} files\n"; ServerManager::getInstance()->getSwooleServer()->reload(); } else { // 初次扫描没有需求进行重启操作 echo "hot reload ready at {$time} use : {$usage} s total: {$count} files\n"; $this->isReady = true; } } } /** * 注册Inotify监听事情 */ private function registerInotifyEvent() { // 由于过程自力 且以后是自界说过程 全局变量只有该过程应用 // 正在确定没有会造成净化的状况下 也能够正当应用全局变量 global $lastReloadTime; global $inotifyResource; $lastReloadTime = 0; $files = File::scanDirectory(EASYSWOOLE_ROOT . '/App'); $files = array_merge($files['files'], $files['dirs']); $inotifyResource = inotify_init(); // 为以后一切的目次以及文件增加事情监听 foreach ($files as $item) { inotify_add_watch($inotifyResource, $item, IN_CREATE | IN_DELETE | IN_MODIFY); } // 退出事情轮回 swoole_event_add($inotifyResource, function () { global $lastReloadTime; global $inotifyResource; $events = inotify_read($inotifyResource); if ($lastReloadTime < time() && !empty($events)) { // 限度1s内不克不及进行反复reload $lastReloadTime = time(); ServerManager::getInstance()->getSwooleServer()->reload(); } }); } public function onShutDown() { // TODO: Implement onShutDown() method. } public function onReceive(string $str) { // TODO: Implement onReceive() method. } }
增加好后正在全局的 EasySwooleEvent.php 中,注册该自界说过程
public static function mainServerCreate(EventRegister $register) { $swooleServer = ServerManager::getInstance()->getSwooleServer(); $swooleServer->addProcess((new HotReload('HotReload', ['disableInotify' => false]))->getProcess()); }
以上就是EasySwoole 根底入门的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学 Swoole easyswoole
抱歉,评论功能暂时关闭!