以下內容需要結合es的源碼,不然可能會覺得跳躍。先描述下es啟動的大致流程。es啟動的時候注冊異常處理函數以及加載配置文件。根據位置文件的設置選擇啟動哪種swoole服務。然后用一個事件注冊類,注冊swoole服務需要的回調函數handler。
在啟動前根據配置文件是否啟動consoleTcpserver子服務。其他服務可以在EasySwooleEvent::mainServerCreate中注冊。es3和es2的區別,es3支持協程以及更加組件化。分為了http組件rpc組件等
根路徑的執行路徑其實是require ./vendor/easyswoole/easyswoole/bin/easyswoole.php
<?php require
'
./vendor/easyswoole/easyswoole/bin/easyswoole.php
';
我們查看這個文件分析start方法,vendor/easyswoole/easyswoole/bin/easyswoole.php::165
1 //如果是 php easyswoole start produce的指令,就是生產環境,會加載 produce.env 2 if(in_array('produce',$commandList)){ 3 \EasySwoole\EasySwoole\Core::getInstance()->setIsDev(false); 4 } 5 \EasySwoole\EasySwoole\Core::getInstance()->initialize(); 6 //這個代碼需要跳入 \EasySwoole\EasySwoole\Core::45
跳入EasySwoole\EasySwoole\Core::45
//檢查全局文件是否存在. $file = EASYSWOOLE_ROOT . '/EasySwooleEvent.php'; if(file_exists($file)){ require_once $file; try{ $ref = new \ReflectionClass('EasySwoole\EasySwoole\EasySwooleEvent'); if(!$ref->implementsInterface(Event::class)){ die('global file for EasySwooleEvent is not compatible for EasySwoole\EasySwoole\EasySwooleEvent'); } unset($ref); }catch (\Throwable $throwable){ die($throwable->getMessage()); } }else{ die('global event file missing'); } //上面的代碼是為了判斷EasySwooleEvent存在並且確保實現了Event::class接口 //執行框架初始化事件,demo中創建了協程數據庫連接池 EasySwooleEvent::initialize(); //根據是否是生產環境加載配置文件 $this->loadEnv(); //創建臨時目錄,主要是記錄swoole.log 和 主服務的pid $this->sysDirectoryInit(); //注冊錯誤回調 $this->registerErrorHandler();
回到vendor/easyswoole/easyswoole/bin/easyswoole.php::170
1 $conf = \EasySwoole\EasySwoole\Config::getInstance();//獲取配置的內容 2 //是否含有d或者daemonize(示例 php easyswoole start d)指令,如果有,則服務設置為daemonize模式 3 if(in_array("d",$commandList) || in_array("daemonize",$commandList)){ 4 $conf->setConf("MAIN_SERVER.SETTING.daemonize", true); 5 } 6 7 //創建主服務 8 \EasySwoole\EasySwoole\Core::getInstance()->createServer(); 9 跳入\EasySwoole\EasySwoole\Core::74
跳入\EasySwoole\EasySwoole\Core::74
//獲取配置的主服務的類型以及相關配置,創建swoole服務,dev.env 里服務是WEB_SOCKET_SERVER就是swoole_websocket_server形式創建的服務 $conf = Config::getInstance()->getConf('MAIN_SERVER'); ServerManager::getInstance()->createSwooleServer( $conf['PORT'],$conf['SERVER_TYPE'],$conf['HOST'],$conf['SETTING'],$conf['RUN_MODEL'],$conf['SOCK_TYPE'] ); /*下面是注冊基本的時間回調,如果swoole服務是SERVER類型會注冊onReceive回調事件然后回調是交由EasySwooleEvent的onReceive處理。demo中這個onReceive是空函數沒有實現方法。但是如果想開啟server的話這里記得補完處理邏輯 否則就注冊默認的request回調,這個會調用es3的http組件(es3最大的變化就是實現了各個模塊的組件化以及添加了協程),不過任何的模式都會注冊task和finish,用來處理異步任務 pipe通訊是為了對task進程通信 注冊默認的worker start,對work和task進程更名*/ $this->registerDefaultCallBack(ServerManager::getInstance()->getSwooleServer(),$conf['SERVER_TYPE']); //EasySwooleEvent的mainServerCreate函數中,對主服務設置其他的回調函數。示例demo中的rpc留給后面分析 EasySwooleEvent::mainServerCreate(ServerManager::getInstance()->getMainEventRegister()); /*創建主服務后,創建Tcp子服務,我們在前文已經配置了CONSOLE為enable 可以做推送日志等信息。換句話說提供了一個server來用Tcp控制服務器,可以從任意機器控制某個遠程的實例*/ (new TcpService(Config::getInstance()->getConf('CONSOLE')));
之后的都是一些啟動時輸出相關信息
回到vendor/easyswoole/easyswoole/bin/easyswoole.php::209
//給主進程也命名然后啟動swoole服務 \EasySwoole\EasySwoole\Core::getInstance()->start();
對比之前分析的2.0流程上其實差別不大。就是再讀一遍比起2.0要清晰很多。
