pomelo源码解析之组件Remote

来源:互联网 发布:sas数据分析大赛题目 编辑:程序博客网 时间:2024/05/19 03:20

首先提出问题:
1.组件Remote是什么?
2.Remote的作用是什么?

remote模块是远程通讯模块服务端监听模块,作用是作为各个模块间通讯对象的存在。

remote对象在app.components._remote_ 变量中保存.

/** * Get remote paths from application * * @param {Object} app current application context * @return {Array} paths * */var getRemotePaths = function(app) {  var paths = [];  var role;  // master server should not come here  if(app.isFrontend()) {    role = 'frontend';  } else {    role = 'backend';  }  var sysPath = pathUtil.getSysRemotePath(role), serverType = app.getServerType();  if(fs.existsSync(sysPath)) {    paths.push(pathUtil.remotePathRecord('sys', serverType, sysPath));  }  var userPath = pathUtil.getUserRemotePath(app.getBase(), serverType);  if(fs.existsSync(userPath)) {    paths.push(pathUtil.remotePathRecord('user', serverType, userPath));  }  return paths;};/** * Generate remote server instance * * @param {Object} app current application context * @param {Object} opts contructor parameters for rpc Server * @return {Object} remote server instance */var genRemote = function(app, opts) {  opts.paths = getRemotePaths(app);  opts.context = app;  if(!!opts.rpcServer) {    return opts.rpcServer.create(opts);  } else {    return RemoteServer.create(opts);  }};

remote使用rpc协议,作为rpc服务端存在。

/** * Remote component lifecycle function * * @param {Function} cb * @return {Void} */pro.start = function(cb) {  this.opts.port = this.app.getCurServer().port;  this.remote = genRemote(this.app, this.opts);  this.remote.start();  process.nextTick(cb);};

在start函数中,genRemote实现了rpc协议的配置和监听。

此外,需要注意的是Remote逻辑js设置有两个地方。
一个是作为系统调用,sys,对应了pomelo/lib/common/remote路径下的backend或者frontend,是哪一个路径取决于当前的服务器是前端服务器还是后端服务器,也就是说在server.json中配置好的每项中的frontend元素是true还是false。
还有一个是用户调用,user,对应了app/servers/服务器类型名/remote下的各js文件。