Pomelo热更新刷新handler和remote 以及 pomelo使用bearcat进行热更新

一. 开启 原生 pomelo 的hotreload支持

  1. pomelo版本: 2.2.5 , 编辑脚本 app.js 加入如下代码

    1 //全局配置 2 app.configure('production|development', function() { 3 //让所有服务器 都支持 handle 和remote 热更新 4 let serverConfig = { 5 'reloadHandlers':true, 6 'reloadRemotes':true, 7 }; 8 app.set('serverConfig',serverConfig); 9 });
  2. 原理:监听文件改动,在文件变化以后重新加载,只能更新 remote rpc 和 handler

    1//监听 handler 2var watchHandlers = function(app, handlerMap) { 3 var p = pathUtil.getHandlerPath(app.getBase(), app.serverType); 4 if (!!p){ 5 fs.watch(p, function(event, name) { 6 if(event === 'change') { 7 handlerMap[app.serverType] = Loader.load(p, app); 8 } 9 }); 10 } 11};

注意: 在 remote和handler 文件里不要保存局部数据,否则刷新以后会丢失.

二 .使用bearcat 热更新

  1. 参考项目 https://github.com/NetEase/treasures

  2. 根据 treasures 配置好 context.json

    { "name": "bearcat", "scan": "app", "beans": [] }

scan 就是要检测的目录 .

  1. 修改app.js,添加如下内容

    //BEARCAT_HOT 一定要配置成 on //BEARCAT_LOGGER 如果不关闭,则pomelo的日志输出 会将所有的日志都输出到 pomelo-undefined.log 里面. var contextPath = require.resolve('./context.json'); bearcat.createApp([contextPath], { BEARCAT_HOT: 'on',// 开启热更新,如果是off 那么不会热更新 BEARCAT_LOGGER: 'off',//setup 'off' to turn off bearcat logger configuration, } );

    // 启动APP // app.start(); bearcat.start(function() { app.set('bearcat', bearcat); // start app app.start(); });

通过上面简单的操作,bearcat 就已经配置好了.试一试修改文件,控制台就会有提示bearcat重新加载.

三. bearcat更新示例

entryHandler.js
1Handler.prototype.enter = function (msg, session, next) { 2 //这行如果写在函数外面,则不能热更新,每次修改文件要生效,都必须重新加载文件 3 var Student = require('./student'); 4 var _stu = new Student(); 5 _stu.say(); 6 7 next(null,0); 8 return; 9 }
student.js
1var Student = function(){ 2 3} 4Student.prototype.say = function(){ 5 console.log("i am old say"); 6 console.log("i am old say"); 7 console.log("i am old say"); 8 console.log("i am old say"); 9} 10 11module.exports = function(){ 12 return new Student(); 13}

测试过程:

  1. 开启服务器 node app.js
  2. 调用 connector.entryHandler.enter , 会打印 i am old say
  3. 修改 i am old say ==> i am new say
  4. 控制台输出正在 reload ,等待reload 完毕.
  5. 调用 connector.entryHandler.enter , 会打印 i am new say

注意:

  1. 这行如果写在函数外面,则不能热更新,每次修改文件要生效,都必须重新加载文件 var Student = require('./student');

  2. 说明 bearcat 更新主要是更新 新创建的对象! 以前的对象是使用以前的代码,这种情况是更新不了的!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

KVM调整cpu和内存

一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid