码迷,mamicode.com
首页 > 其他好文 > 详细

分析easyswoole3.0源码,服务启动为例(二)

时间:2018-10-26 17:50:07      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:最大   throwable   not   很多   获取   ble   形式   receive   oba   

以下内容需要结合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要清晰很多。



 

 

分析easyswoole3.0源码,服务启动为例(二)

标签:最大   throwable   not   很多   获取   ble   形式   receive   oba   

原文地址:https://www.cnblogs.com/gavinjunftd/p/9857270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!