全局事件
bootstrap 事件
bootstrap
事件允許在框架未初始化之前,先進(jìn)行初始化其他需要的業(yè)務(wù)代碼。該事件是在 EasySwoole 3.2.5版本之后
新增的。
在框架安裝之后產(chǎn)生的 easyswoole
啟動(dòng)腳本文件中,將會(huì)自動(dòng)判斷框架根目錄下是否有 bootstrap.php
文件,如果有則加載此文件。
目前框架最新版本的 bootstrap.php
(即 bootstrap
事件)會(huì)在框架安裝時(shí)在項(xiàng)目根目錄中自動(dòng)生成。所以如果用戶想要執(zhí)行自己需要的初始化業(yè)務(wù)代碼:如 注冊(cè)命令行支持
、全局通用函數(shù)
、啟動(dòng)前調(diào)用協(xié)程 API
等功能,就可以在 bootstrap.php
中進(jìn)行編寫實(shí)現(xiàn)。
注:
EasySwoole 3.4.x
版本之前bootstrap.php
文件需要用戶在項(xiàng)目根目錄下自行創(chuàng)建該文件bootstrap.php
。注:如果你是框架舊版升級(jí)到框架新版,需要?jiǎng)h除框架根目錄的
easyswoole
文件,然后重新運(yùn)行php ./vendor/easyswoole/easyswoole/bin/easyswoole install
進(jìn)行重新安裝(報(bào)錯(cuò)或者其他原因請(qǐng)重新看 框架安裝章節(jié)-執(zhí)行安裝步驟),重新安裝完成之后,即可正常使用bootstrap
事件
在框架啟用前(在 bootstrap 事件中)調(diào)用協(xié)程 API
開發(fā)者在 EasySwoole
主服務(wù)啟動(dòng)前調(diào)用協(xié)程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調(diào)用協(xié)程API */
});
$scheduler->start();
// 清除全部定時(shí)器
\Swoole\Timer::clearAll();
具體使用示例如下:
<?php
// 全局 bootstrap 事件
date_default_timezone_set('Asia/Shanghai');
use Swoole\Coroutine\Scheduler;
$scheduler = new Scheduler();
$scheduler->add(function() {
/* 調(diào)用協(xié)程 API */
});
$scheduler->start();
// 清除全部定時(shí)器
\Swoole\Timer::clearAll();
initialize 事件
框架初始化事件,在執(zhí)行 initialize
初始化事件時(shí),EasySwoole
框架此刻已經(jīng)完成了如下工作:
- 加載配置文件
- 初始化
Log/Temp
目錄,完成系統(tǒng)默認(rèn)Log/Temp
目錄的定義
函數(shù)原型
public static function initialize(): void
{
}
開發(fā)者自定義處理
開發(fā)者可以在 initialize
事件可以進(jìn)行如下修改:
- 修改框架默認(rèn)使用的
error_report
級(jí)別,使用自定義的error_report
級(jí)別 - 修改框架默認(rèn)使用的
Logger
處理器,使用自定義的Logger
處理器 - 修改框架默認(rèn)使用的
Trigger
處理器,使用自定義的Trigger
處理器 - 修改框架默認(rèn)使用的
Error
處理器,使用自定義的Error
處理器 - 修改框架默認(rèn)使用的
Shutdown
處理器,使用自定義的Shutdown
處理器 - 修改框架默認(rèn)使用的
HttpException
全局處理器,使用自定義的HttpException
全局處理器 - 設(shè)置
Http
全局OnRequest
及AfterRequest
事件 - 注冊(cè)數(shù)據(jù)庫、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');
// 開發(fā)者自定義設(shè)置 錯(cuò)誤級(jí)別
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::ERROR_REPORT_LEVEL, E_ALL);
// 開發(fā)者自定義設(shè)置 日志處理類(該類需要實(shí)現(xiàn) \EasySwoole\Log\LoggerInterface,開發(fā)者可自行查看并實(shí)現(xiàn),方便開發(fā)者自定義處理日志)
$logDir = EASYSWOOLE_LOG_DIR; // 定義日志存放目錄
$loggerHandler = new \EasySwoole\Log\Logger($logDir); // 定義日志處理對(duì)象
\EasySwoole\Component\Di::getInstance()->set(SysConst::LOGGER_HANDLER, $loggerHandler);
// 開發(fā)者自定義設(shè)置 Trace 追蹤器(該類需要實(shí)現(xiàn) \EasySwoole\Trigger\TriggerInterface,開發(fā)者可自行查看并實(shí)現(xiàn),方便開發(fā)者自定義處理 Trace 鏈路)
// Trace 追蹤器需要依據(jù)上面的 logger_handler
\EasySwoole\Component\Di::getInstance()->set(SysConst::TRIGGER_HANDLER, new \EasySwoole\Trigger\Trigger($loggerHandler));
// 開發(fā)者自定義設(shè)置 error_handler
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::ERROR_HANDLER, function ($errorCode, $description, $file = null, $line = null) {
// 開發(fā)者對(duì)錯(cuò)誤進(jìn)行處理
});
// 開發(fā)者自定義設(shè)置 shutdown
\EasySwoole\Component\Di::getInstance()->set(\EasySwoole\EasySwoole\SysConst::SHUTDOWN_FUNCTION, function () {
// 開發(fā)者對(duì) shutdown 進(jìn)行處理
});
// 開發(fā)者自定義設(shè)置 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);
});
// 開發(fā)者自定義設(shè)置 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 中已定義,不必重新設(shè)置
});
// 開發(fā)者自定義設(shè)置 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 中已定義,不必重新設(shè)置
});
// 注冊(cè)數(shù)據(jù)庫連接及連接池(詳見:http://www.jrrswxmm.cn/Components/Orm/install.html)
// 注冊(cè) Redis 連接及連接池(詳見:http://www.jrrswxmm.cn/Components/Redis/introduction.html)
}
public static function mainServerCreate(EventRegister $register)
{
}
}
啟用前(在 initialize 事件中)調(diào)用協(xié)程 API
開發(fā)者在 EasySwoole
主服務(wù)啟動(dòng)前調(diào)用協(xié)程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調(diào)用協(xié)程API */
});
$scheduler->start();
// 清除全部定時(shí)器
\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() {
/* 調(diào)用協(xié)程API */
});
$scheduler->start();
// 清除全部定時(shí)器
\Swoole\Timer::clearAll();
}
public static function mainServerCreate(EventRegister $register)
{
}
}
在 initialize 事件中調(diào)用連接池
initialize
事件在 EasySwoole
生命周期中屬于 主進(jìn)程
,因此在主進(jìn)程中創(chuàng)建了連接池可能會(huì)導(dǎo)致以下問題:
- 創(chuàng)建了全局的定時(shí)器
- 創(chuàng)建了全局的
EventLoop
- 創(chuàng)建的連接被跨進(jìn)程公用,因此我們以服務(wù)啟動(dòng)前調(diào)用數(shù)據(jù)庫
ORM
為例:
服務(wù)啟動(dòng)前調(diào)用數(shù)據(jù)庫 ORM
:
下文
\EasySwoole\EasySwoole\Config::getInstance()->getConf('MYSQL')
獲取的 MYSQL 配置,詳細(xì)參考 配置文件
<?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));
// 創(chuàng)建一個(gè)協(xié)程調(diào)度器
$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,避免連接被克隆到子進(jìn)程,造成連接跨進(jìn)程公用。
// DbManager 如果有注冊(cè)多庫連接,請(qǐng)記得一起 getConnection($name) 獲取全部的 pool 去執(zhí)行 reset
// 其他的連接池請(qǐng)獲取到對(duì)應(yīng)的 pool,然后執(zhí)行 reset() 方法
// ORM 1.4.31 版本之前請(qǐng)使用 getClientPool()
// DbManager::getInstance()->getConnection()->getClientPool()->reset();
\EasySwoole\ORM\DbManager::getInstance()->getConnection()->__getClientPool()->reset();
});
//執(zhí)行調(diào)度器內(nèi)注冊(cè)的全部回調(diào)
$scheduler->start();
//清理調(diào)度器內(nèi)可能注冊(cè)的定時(shí)器,不要影響到swoole server 的event loop
\Swoole\Timer::clearAll();
}
public static function mainServerCreate(EventRegister $register)
{
}
}
mainServerCreate 事件(即主服務(wù)創(chuàng)建事件)
函數(shù)原型
/**
* @param \EasySwoole\EasySwoole\Swoole\EventRegister $register
*/
public static function mainServerCreate(EventRegister $register)
{
}
已完成工作
在執(zhí)行主服務(wù)創(chuàng)建事件時(shí),框架此時(shí)已經(jīng)完成的工作有:
-
bootstrap/initialize
事件加載完成 - 主
SwooleServer
創(chuàng)建成功 - 主
SwooleServer
注冊(cè)了默認(rèn)的onRequest/onWorkerStart/onWorkerStop/onWorkerExit
事件。
開發(fā)者可進(jìn)行的操作有:
- 注冊(cè)主服務(wù)回調(diào)事件
- 添加子服務(wù)監(jiān)聽
SwooleTable/Atomic
- 創(chuàng)建自定義進(jìn)程
- 啟用前(在 mainServerCreate 事件中)調(diào)用協(xié)程 API
注冊(cè)主服務(wù)回調(diào)事件
例如:為主服務(wù)注冊(cè) onWorkerStart
回調(diào)事件:
/** @var \EasySwoole\EasySwoole\Swoole\EventRegister $register **/
$register->add($register::onWorkerStart, function (\Swoole\Server $server,int $workerId){
var_dump($workerId . 'start');
});
例如:為主服務(wù)增加 onMessage
回調(diào)事件(前提是主服務(wù)類型為 WebSocket
服務(wù)):
// 給 server 注冊(cè)相關(guān)事件,在 WebSocket 服務(wù)模式下 message 事件必須注冊(cè)
/** @var \EasySwoole\EasySwoole\Swoole\EventRegister $register **/
$register->set($register::onMessage,function (\Swoole\WebSocket\Server $server, \Swoole\WebSocket\Frame $frame){
});
set
方法和 add
方法是不同的, set 將會(huì)覆蓋之前配置的事件回調(diào), 而 add 是增加一個(gè)新的回調(diào)。
添加子服務(wù)監(jiān)聽
例如:添加一個(gè) tcp
子服務(wù)監(jiān)聽
/** @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
具體調(diào)用方式請(qǐng)看具體章節(jié):
創(chuàng)建自定義進(jìn)程
具體詳細(xì)操作可到 基礎(chǔ)使用 -> 自定義進(jìn)程中查看
\EasySwoole\Component\Process\Manager::getInstance()->addProcess(new Test('test_process'));
Test
是EasySwoole\Component\Process\AbstractProcess
抽象類的子類
啟用前(在 mainServerCreate 事件中)調(diào)用協(xié)程 API
開發(fā)者在 EasySwoole
主服務(wù)啟動(dòng)前調(diào)用協(xié)程 api
,必須使用如下操作:
$scheduler = new \Swoole\Coroutine\Scheduler();
$scheduler->add(function() {
/* 調(diào)用協(xié)程API */
});
$scheduler->start();
// 清除全部定時(shí)器
\Swoole\Timer::clearAll();