全局事件
bootstrap 事件
bootstrap
事件允許在框架未初始化之前,先進行初始化其他需要的業務代碼。該事件是在 EasySwoole 3.2.5版本之后
新增的。
在框架安裝之后產生的 easyswoole
啟動腳本文件中,將會自動判斷框架根目錄下是否有 bootstrap.php
文件,如果有則加載此文件。
目前框架最新版本的 bootstrap.php
(即 bootstrap
事件)會在框架安裝時在項目根目錄中自動生成。所以如果用戶想要執行自己需要的初始化業務代碼:如 注冊命令行支持
、全局通用函數
、啟動前調用協程 API
等功能,就可以在 bootstrap.php
中進行編寫實現。
注:
EasySwoole 3.4.x
版本之前bootstrap.php
文件需要用戶在項目根目錄下自行創建該文件bootstrap.php
。注:如果你是框架舊版升級到框架新版,需要刪除框架根目錄的
easyswoole
文件,然后重新運行php ./vendor/easyswoole/easyswoole/bin/easyswoole install
進行重新安裝(報錯或者其他原因請重新看 框架安裝章節-執行安裝步驟),重新安裝完成之后,即可正常使用bootstrap
事件
在框架啟用前(在 bootstrap 事件中)調用協程 API
開發者在 EasySwoole
主服務啟動前調用協程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調用協程API */
});
$scheduler->start();
// 清除全部定時器
\Swoole\Timer::clearAll();
具體使用示例如下:
<?php
// 全局 bootstrap 事件
date_default_timezone_set('Asia/Shanghai');
use Swoole\Coroutine\Scheduler;
$scheduler = new Scheduler();
$scheduler->add(function() {
/* 調用協程 API */
});
$scheduler->start();
// 清除全部定時器
\Swoole\Timer::clearAll();
initialize 事件
框架初始化事件,在執行 initialize
初始化事件時,EasySwoole
框架此刻已經完成了如下工作:
- 加載配置文件
- 初始化
Log/Temp
目錄,完成系統默認Log/Temp
目錄的定義
函數原型
public static function initialize(): void
{
}
開發者自定義處理
開發者可以在 initialize
事件可以進行如下修改:
- 修改框架默認使用的
error_report
級別,使用自定義的error_report
級別 - 修改框架默認使用的
Logger
處理器,使用自定義的Logger
處理器 - 修改框架默認使用的
Trigger
處理器,使用自定義的Trigger
處理器 - 修改框架默認使用的
Error
處理器,使用自定義的Error
處理器 - 修改框架默認使用的
Shutdown
處理器,使用自定義的Shutdown
處理器 - 修改框架默認使用的
HttpException
全局處理器,使用自定義的HttpException
全局處理器 - 設置
Http
全局OnRequest
及AfterRequest
事件 - 注冊數據庫、Redis 連接池
具體可查看 SysConst.php
使用示例代碼:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
// 開發者自定義設置 錯誤級別
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::ERROR_REPORT_LEVEL, E_ALL);
// 開發者自定義設置 日志處理類(該類需要實現 \EasySwoole\Log\LoggerInterface,開發者可自行查看并實現,方便開發者自定義處理日志)
$logDir = EASYSWOOLE_LOG_DIR; // 定義日志存放目錄
$loggerHandler = new \EasySwoole\Log\Logger($logDir); // 定義日志處理對象
\EasySwoole\Component\Di::getInstance()->set(SysConst::LOGGER_HANDLER, $loggerHandler);
// 開發者自定義設置 Trace 追蹤器(該類需要實現 \EasySwoole\Trigger\TriggerInterface,開發者可自行查看并實現,方便開發者自定義處理 Trace 鏈路)
// Trace 追蹤器需要依據上面的 logger_handler
\EasySwoole\Component\Di::getInstance()->set(SysConst::TRIGGER_HANDLER, new \EasySwoole\Trigger\Trigger($loggerHandler));
// 開發者自定義設置 error_handler
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::ERROR_HANDLER, function ($errorCode, $description, $file = null, $line = null) {
// 開發者對錯誤進行處理
});
// 開發者自定義設置 shutdown
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::SHUTDOWN_FUNCTION, function () {
// 開發者對 shutdown 進行處理
});
// 開發者自定義設置 HttpException 全局處理器
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_EXCEPTION_HANDLER, function ($throwable, Request $request, Response $response) {
$response->withStatus(\EasySwoole\Http\Message\Status::CODE_INTERNAL_SERVER_ERROR);
$response->write(nl2br($throwable->getMessage() . "\n" . $throwable->getTraceAsString()));
Trigger::getInstance()->throwable($throwable);
});
// 開發者自定義設置 onRequest v3.4.x+
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_ON_REQUEST, function (\EasySwoole\Http\Request $request, \EasySwoole\Http\Response $response) {
// v3.4.x 之前的版本 onRequest 事件在 EasySwoolEvent.php 中已定義,不必重新設置
});
// 開發者自定義設置 afterRequest v3.4.x+
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::HTTP_GLOBAL_AFTER_REQUEST, function (\EasySwoole\Http\Request $request, \EasySwoole\Http\Response $response) {
// v3.4.x 之前的版本 afterRequest 事件在 EasySwoolEvent.php 中已定義,不必重新設置
});
// 注冊數據庫連接及連接池(詳見:http://www.fe88.cn/Components/Orm/install.html)
// 注冊 Redis 連接及連接池(詳見:http://www.fe88.cn/Components/Redis/introduction.html)
}
public static function mainServerCreate(EventRegister $register)
{
}
}
啟用前(在 initialize 事件中)調用協程 API
開發者在 EasySwoole
主服務啟動前調用協程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調用協程API */
});
$scheduler->start();
// 清除全部定時器
\Swoole\Timer::clearAll();
具體使用示例:
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調用協程API */
});
$scheduler->start();
// 清除全部定時器
\Swoole\Timer::clearAll();
}
public static function mainServerCreate(EventRegister $register)
{
}
}
在 initialize 事件中調用連接池
initialize
事件在 EasySwoole
生命周期中屬于 主進程
,因此在主進程中創建了連接池可能會導致以下問題:
- 創建了全局的定時器
- 創建了全局的
EventLoop
- 創建的連接被跨進程公用,因此我們以服務啟動前調用數據庫
ORM
為例:
服務啟動前調用數據庫 ORM
:
下文
\EasySwoole\EasySwoole\Config::getInstance()->getConf('MYSQL')
獲取的 MYSQL 配置,詳細參考 配置文件
<?php
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
class EasySwooleEvent implements Event
{
public static function initialize()
{
// TODO: Implement initialize() method.
date_default_timezone_set('Asia/Shanghai');
$config = new \EasySwoole\ORM\Db\Config(\EasySwoole\EasySwoole\Config::getInstance()->getConf('MYSQL'));
\EasySwoole\ORM\DbManager::getInstance()->addConnection(new \EasySwoole\ORM\Db\Connection($config));
// 創建一個協程調度器
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function () {
$builder = new \EasySwoole\Mysqli\QueryBuilder();
$builder->raw('select version()');
\EasySwoole\ORM\DbManager::getInstance()->query($builder, true);
// 這邊重置 ORM 連接池的 pool,避免連接被克隆到子進程,造成連接跨進程公用。
// DbManager 如果有注冊多庫連接,請記得一起 getConnection($name) 獲取全部的 pool 去執行 reset
// 其他的連接池請獲取到對應的 pool,然后執行 reset() 方法
// ORM 1.4.31 版本之前請使用 getClientPool()
// DbManager::getInstance()->getConnection()->getClientPool()->reset();
\EasySwoole\ORM\DbManager::getInstance()->getConnection()->__getClientPool()->reset();
});
//執行調度器內注冊的全部回調
$scheduler->start();
//清理調度器內可能注冊的定時器,不要影響到swoole server 的event loop
\Swoole\Timer::clearAll();
}
public static function mainServerCreate(EventRegister $register)
{
}
}
mainServerCreate 事件(即主服務創建事件)
函數原型
/**
* @param \EasySwoole\EasySwoole\Swoole\EventRegister $register
*/
public static function mainServerCreate(EventRegister $register)
{
}
已完成工作
在執行主服務創建事件時,框架此時已經完成的工作有:
-
bootstrap/initialize
事件加載完成 - 主
SwooleServer
創建成功 - 主
SwooleServer
注冊了默認的onRequest/onWorkerStart/onWorkerStop/onWorkerExit
事件。
開發者可進行的操作有:
- 注冊主服務回調事件
- 添加子服務監聽
SwooleTable/Atomic
- 創建自定義進程
- 啟用前(在 mainServerCreate 事件中)調用協程 API
注冊主服務回調事件
例如:為主服務注冊 onWorkerStart
回調事件:
/** @var \EasySwoole\EasySwoole\Swoole\EventRegister $register **/
$register->add($register::onWorkerStart, function (\Swoole\Server $server,int $workerId){
var_dump($workerId . 'start');
});
例如:為主服務增加 onMessage
回調事件(前提是主服務類型為 WebSocket
服務):
// 給 server 注冊相關事件,在 WebSocket 服務模式下 message 事件必須注冊
/** @var \EasySwoole\EasySwoole\Swoole\EventRegister $register **/
$register->set($register::onMessage,function (\Swoole\WebSocket\Server $server, \Swoole\WebSocket\Frame $frame){
});
set
方法和 add
方法是不同的, set 將會覆蓋之前配置的事件回調, 而 add 是增加一個新的回調。
添加子服務監聽
例如:添加一個 tcp
子服務監聽
/** @var \Swoole\Server\Port $subPort **/
$subPort = \EasySwoole\EasySwoole\ServerManager::getInstance()->getSwooleServer()->addListener('0.0.0.0', 9503, SWOOLE_TCP);
$subPort->on('receive', function (\Swoole\Server $server, int $fd, int $reactor_id, string $data){
var_dump($data);
});
// 配置 具體查看 Swoole 文檔
$subPort->set([
]);
具體可參考 TCP
Table && Atomic
具體調用方式請看具體章節:
創建自定義進程
具體詳細操作可到 基礎使用 -> 自定義進程中查看
\EasySwoole\Component\Process\Manager::getInstance()->addProcess(new Test('test_process'));
Test
是EasySwoole\Component\Process\AbstractProcess
抽象類的子類
啟用前(在 mainServerCreate 事件中)調用協程 API
開發者在 EasySwoole
主服務啟動前調用協程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調用協程API */
});
$scheduler->start();
// 清除全部定時器
\Swoole\Timer::clearAll();