pomelo 框架第二部分
项目地址: https://github.com/NetEase/chatofpomelo-websocket.git
分支:tutorial-protobuf
如果发现有错误的地方,请在评论中及时指出,谢谢
一、pomelo调用各模块start接口
channel、connector、dictionary、master、monitor、proxy、remote、server中包含start
1.onnector.js
this.server = this.app.components.__server__;
this.session = this.app.components.__session__;
this.connection = this.app.components.__connection__;
把server、session和connection模块绑定到connector上。
2.dictionary.js
读取 handler 里面的接口和dictionary.json,给this.abbrs
和this.dict
赋值
3.master.js
this.master.start(cb);
调用master/master.js的start方法
4.monitor.js
this.monitor.start(cb);
调用monitor/monitor.js的start方法
5.remote.js
pro.start = function(cb) {
this.opts.port = this.app.getCurServer().port;
this.remote = genRemote(this.app, this.opts); // require('pomelo-rpc').server
this.remote.start();
};
对this.remote
进行require('pomelo-rpc').server
初始化赋值,然后调用this.remote.start
方法,即require('pomelo-rpc').server
的start方法。
6.server.js
this.server.start();
调用server/server.js的start方法
二、master/master.js的start执行步骤
1.在初始化master.js时对master/master.js进行初始化操作。
var Server = function (app, opts) {
this.app = app;
this.masterInfo = app.getMaster(); // 获取master信息
this.registered = {};
this.modules = [];
opts = opts || {};
opts.port = this.masterInfo.port;
opts.env = this.app.get(Constants.RESERVED.ENV);
this.closeWatcher = opts.closeWatcher;
this.masterConsole = admin.createMasterConsole(opts); // require('pomelo-admin');
};
对this.masterConsole
进行赋值,调用require('pomelo-admin');
的createMasterConsole方法,即为pomelo-admin/lib/consoleService.js
,此处的masterConsole为pomelo-admin的上层入口,也是上层代理,通过masterConsole调用pomelo-admin中的方法。
2.master/master.js中的start方法。
Server.prototype.start = function (cb) {
moduleUtil.registerDefaultModules(true, this.app, this.closeWatcher);
moduleUtil.loadModules(this, this.masterConsole);
var self = this;
// start master console
this.masterConsole.start(function (err) {
if (err) {
process.exit(0);
}
moduleUtil.startModules(self.modules, function (err) {
if (err) {
utils.invokeCallback(cb, err);
return;
}
if (self.app.get(Constants.RESERVED.MODE) !== Constants.RESERVED.STAND_ALONE) {
starter.runServers(self.app);
}
utils.invokeCallback(cb);
});
});
this.masterConsole.on('error', function (err) {});
this.masterConsole.on('reconnect', function (info) {
self.app.addServers([info]);
});
// monitor servers disconnect event
this.masterConsole.on('disconnect', function (id, type, info, reason) {});
// monitor servers register event
this.masterConsole.on('register', function (record) {
starter.bindCpu(record.id, record.pid, record.host);
});
this.masterConsole.on('admin-log', function (log, error) {});
};
3.moduleUtil.registerDefaultModules方法
registerDefaultModules 方法把 modules/masterwatcher(master观察者)、admin.modules.watchServer(admin观察服务) 、modules/console 添加到app.modules 中。
如果开启了systemMonitor,则注册相应的模块。
pro.registerDefaultModules = function (isMaster, app, closeWatcher) {
if (!closeWatcher) {
if (isMaster) {
app.registerAdmin(require('../modules/masterwatcher'), {app: app});
} else {
app.registerAdmin(require('../modules/monitorwatcher'), {app: app});
}
}
app.registerAdmin(admin.modules.watchServer, {app: app});
app.registerAdmin(require('../modules/console'), {app: app, starter: starter});
if (app.enabled('systemMonitor')) {
if (os.platform() !== Constants.PLATFORM.WIN) {
app.registerAdmin(admin.modules.systemInfo);
app.registerAdmin(admin.modules.nodeInfo);
}
app.registerAdmin(admin.modules.monitorLog, {path: pathUtil.getLogPath(app.getBase())});
app.registerAdmin(admin.modules.scripts, {app: app, path: pathUtil.getScriptPath(app.getBase())});
if (os.platform() !== Constants.PLATFORM.WIN) {
app.registerAdmin(admin.modules.profiler);
}
}
};
4.moduleUtil.loadModules方法
loadModules 方法根据app.get(Constants.KEYWORDS.MODULE);
获取相应模块,此处为masterwatcher、watchServer,console模块,如果record.module
的type类型为function则初始化此方法,把相应组件注册到consoleService中,并保存到self.modules
中。
pro.loadModules &