pomelo之master服务器的启动

写完前面的两篇文章,一直走的都是master服务器的流程,那么这一篇就真正涉及到master服务器的启动过程了,在真正开始之前,先回顾一下前面的两篇文章。。

(1)创建app的过程,这部分主要要完成的功能是读入用户定义的配置参数,并保存和处理这些配置参数。

(2)启动app的过程,这部分主要要完成的功能是load组件,完成对组件的包装(前面已经对master组件进行了说明,在真正的master外面还有一个master的包装,代理它的启动等过程)


新来看看那个master的代理吧:

/**
 * Component for master.
 */
var Master = require('../master/master');

/**
 * Component factory function
 *
 * @param  {Object} app  current application context
 * @return {Object}      component instances
 */
 //相当于require之后得到的就是这个函数
module.exports = function (app) {
  return new Component(app);
};

/**
* Master component class
*
* @param {Object} app  current application context
*/
var Component = function (app) {
  this.master = new Master(app);   //创建爱你master
};

var pro = Component.prototype;

/**
 * Component lifecycle function
 *
 * @param  {Function} cb
 * @return {Void}
 */
pro.start = function (cb) {
  this.master.start(cb);
};

/**
 * Component lifecycle function
 *
 * @param  {Boolean}   force whether stop the component immediately
 * @param  {Function}  cb
 * @return {Void}
 */
pro.stop = function (force, cb) {
  this.master.stop(cb);
};
就像前面说的一样,它就是一个外包装而已。。。

那么我们来看真正的master,首先来看看真正的master的构造:

//用于创建master
var Server = function(app) {
  this.app = app;
  this.masterInfo = app.getMaster();   //master的配置实在loadMaster的配置的时候读取的,这个时候获取master的配置
  this.registered = {};   //
  this.modules = [];  //所有的模块

  //Default is the main master
  this.isSlave = false;

  this.masterConsole = admin.createMasterConsole({   //创建console
    port: this.masterInfo.port   //master的端口
  });

  if(app.enabled('masterHA')){ //Zookeeper这部分暂时先搁置,因为以前完全不了解它
    this.zookeeper = Zookeeper.getClient(app);
  }
};
珍格格构造的过程看起来还是比较的简单,这里对于zookeeper的部分就先搁置,以后再来看,因为以前完全没有涉及到过zookeeper。

前面的代码要是读入master的配置信息,然后创建master的console,那么接下来来看这个console是怎么创建的吧:

module.exports.createMasterConsole = function(opts) {
	opts = opts || {};
	opts.master = true;   //用于表示当前是master服务器
	return new ConsoleService(opts);  //创建并返回console
};
好像这部分代码也没啥意思,好吧接着来看:
var ConsoleService = function(opts) {
	EventEmitter.call(this);   //让当前的对象可以处理事件
	this.port = opts.port;   //获取端口号
	this.values = {};
	this.master = opts.master;   //当前是否是master服务器

	this.modules = {};

	if(this.master) {
		
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值