使用PM2部署pomelo服务器

pomelo服务器部署问题

pemelo可以使用 'pomelo start -e production' 命令在服务器运行生产环境的服务器代码。pomelo自身缺少进程监控,运行过程中不方便获取到进程的运行信息。而且pomelo没有进程保护,当进程崩溃之后,不能自动重启进程。pomelo提供的单服务器重启动功能不能使用,至少目前的2.2.5版本中不能使用单个服务器重启功能。当完成某个服务器的代码的修改之后,只能把全部服务器关掉,之后再全部启动一次。只修改后端服务器的代码,要让代码生效只能全部重启一次,会导致全部玩家断线重新连接。如果只是单独重启一个后端服务器,则不会出现全部客户端断线重连的问题。

pm2配置文件

使用PM2部署项目,需要一份部署配置文件,用于描述pomelo服务器的进程信息。pomelo的进程主要包括master进程和各个功能服务器。

master进程的配置,配置文件需要指定 log文件的目录,项目代码的目录。还有重要的一点是要指定进程的模式mode=stand-alone,如果不指定master进程的模式,导致pomelo多次启动服务器进程,然后会看到系统里有不少重复进程。

{
  "name": "master",
  "script": "app.js",
  "args": [
    "serverType=master",
    "id=master-server-1",
    "host=47.100.96.55",
    "port=4060",
    "env=production",
    "mode=stand-alone"
  ],
  "watch": false,
  "out_file": "./logs/master-server-1_app.log",
  "error_file": "./logsmaster-server-1_error.log",
  "cwd": "/root/pomelo/PirateClashPomeloServer/game-server",
  "merge_logs": true,
  "exec_mode": "fork_mode"
}

其他功能服务器,需要配置各自的端口号码和host地址。具体的配置信息根据server.json的配置来。


{
  "name": "gamehttp",
  "script": "app.js",
  "args": [
    "env=production",
    "id=gamehttp",
    "port=4050",
    "host=127.0.0.1",
    "serverType=gamehttp"
  ],
  "watch": false,
  "out_file": "./logs/gamehttp_app.log",
  "error_file": "./logs/gamehttp_error.log",
  "cwd": "/root/pomelo/PirateClashPomeloServer/game-server",
  "merge_logs": true,
  "exec_mode": "fork_mode"
}
 

使用脚本生成PM2配置文件

当需要修改项目中master.json或者server.json文件时,需要同步去修改 pm2的配置问题,会相当麻烦。于是写了一个node.js脚本,读取master.json 和 server.json的文件内容生成对应的PM2配置文件generatePm2Config.js。 根据实际情况修改变量cwd,envType, 生成的配置文件放到脚本的父目录。


/**
 * Created by superzhan on 2018/3/19.
 *
 * 根据pomelo 的 servers.json 生成 pm2 启动文件
 */

var masterJsonFile = require('../game-server/config/master.json');
var serversJosnFile = require('../game-server/config/servers.json');

//pomelo 源码目录
var cwd='/root/pomelo/PirateClashPomeloServer/game-server';

//配置运行环境 development production
var envType= 'production';

//模板数据
var processConfigType =   {
    "name"        : "",
    "script"      : "app.js",
    "args"        :  [] ,
    "watch": false,
    "out_file": "./logs/app.log",
    "error_file": "./logs/err.log",
    "cwd": "",
    "merge_logs": true,
    "exec_mode": "fork_mode",
};


//最后的结果数据
var resultJson={};
resultJson.apps=new Array();


var  clone = function(origin) {
    if(!origin) {
        return;
    }
    var obj = {};
    for(var f in origin) {
        obj[f] = origin[f];
    }
    return obj;
};

//
var masterConfig = masterJsonFile[envType];
var serversConfig = serversJosnFile[envType];

//生成master 的配置
var pm2Master = clone( processConfigType );
pm2Master.name="master";
pm2Master.args = new Array();
pm2Master.args.push('serverType=master');
pm2Master.args.push('id='+masterConfig.id);
pm2Master.args.push('host='+masterConfig.host);
pm2Master.args.push('port='+masterConfig.port);
pm2Master.args.push('env='+envType);
pm2Master.args.push('mode=stand-alone');
pm2Master.cwd= cwd;
pm2Master.out_file = './logs/'+masterConfig.id+"_app.log";
pm2Master.error_file='./logs'+masterConfig.id+'_error.log';
resultJson.apps.push(pm2Master);

//生成当个服务器的配置
for(serverType in serversConfig)
{
    var servers = serversConfig[serverType];
    for(var i=0;i<servers.length;++i)
    {
        var singleServer= servers[i];

        var appPm2Config =  clone(processConfigType);
        appPm2Config.name=singleServer.id;
        appPm2Config.args= new Array();
        appPm2Config.args.push('env='+envType);
        appPm2Config.args.push('id='+singleServer.id);
        appPm2Config.args.push('port='+singleServer.port);
        appPm2Config.args.push('host='+singleServer.host);
        appPm2Config.args.push('serverType='+serverType);

        if(singleServer.frontend !=null)
        {
            appPm2Config.args.push('frontend='+ singleServer.frontend);
            appPm2Config.args.push('clientPort='+singleServer.clientPort);
        }

        appPm2Config.cwd= cwd;
        appPm2Config.out_file = './logs/'+ singleServer.id+'_app.log';
        appPm2Config.error_file = './logs/'+singleServer.id+'_error.log';

        resultJson.apps.push(appPm2Config);
    }
}



//生成结果数据
var resultFileStr = JSON.stringify(resultJson);
//console.log(resultFileStr);


var fs = require('fs');

fs.writeFile('../pomeloPm2Start.json', resultFileStr, function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('finish genereate Server pm2 config');
    }
});

PM2管理

启动服务器使用命令 pm2 start pomeloPm2Start.json,pomeloPm2Start.json是上面生成的配置文件。

监控命令 pm2 monit 查看各个进程的运行状态, pm2 log 输出工程的log, pm2 list 列出所有的服务器进程

单个服务器的重启 pm2 restart processId, processId 对应服务器进程的Id.

21 Replies to “使用PM2部署pomelo服务器”

  1. I¡¯ve been wondering what I could try. Honestly all these big companies make me nervous so I wanted to go with a lesser-known brand. So I went with LifeCell. I¡¯ve been using it for about a month now and my skin is hydrated and looks younger than it has in a while. I¡¯ve been trying to reduce the effects that the sun, and smoking, and drinking have had on my skin. I¡¯ve been sober and smoke free for 93 days now and this has helped me feel even better about that decision. My wrinkles have decreased somewhat but it wasn¡¯t what I was looking for honestly, I was looking to be more radiant. Now it¡¯s a bit pricey but I have a good income so I don¡¯t mind spending that kind of money. However if this is something out of your price range, try something else, but if you do try this you won¡¯t be disappointed.

  2. I used to be very happy to seek out this web-site.I wanted to thanks on your time for this wonderful read!! I positively enjoying every little little bit of it and I have you bookmarked to check out new stuff you blog post.

  3. I want to get across my love for your generosity in support of people who absolutely need help on in this niche. Your very own commitment to passing the message all through appeared to be extraordinarily significant and has all the time helped many people like me to get to their goals. Your new invaluable help and advice signifies a whole lot to me and even more to my colleagues. Thanks a ton; from all of us.

  4. I am also commenting to make you understand what a wonderful discovery our girl enjoyed studying your webblog. She even learned too many details, most notably what it’s like to have a very effective giving spirit to let folks without hassle thoroughly grasp chosen impossible topics. You really exceeded our own expected results. I appreciate you for coming up with these interesting, healthy, edifying as well as fun tips on that topic to Ethel.

  5. I not to mention my friends came reading the nice advice from the blog and so unexpectedly I had a horrible feeling I had not expressed respect to the web site owner for them. These men became stimulated to see them and have certainly been having fun with them. Thanks for really being simply helpful and also for figuring out such awesome subject areas most people are really desirous to be informed on. My very own honest apologies for not saying thanks to you sooner.

  6. I am only writing to let you be aware of what a nice experience my daughter encountered viewing yuor web blog. She noticed too many details, which include what it’s like to possess a great coaching style to let many people effortlessly gain knowledge of selected problematic subject matter. You actually surpassed our own desires. Many thanks for churning out these interesting, healthy, informative and in addition unique guidance on your topic to Julie.

  7. I am also commenting to let you understand what a remarkable experience my cousin’s girl experienced using your web page. She realized plenty of pieces, which included what it is like to have an excellent giving mindset to get the others with ease grasp a number of very confusing topics. You actually exceeded our own expectations. Many thanks for coming up with such good, healthy, revealing and easy tips about the topic to Sandra.

  8. I want to show my gratitude for your kindness in support of those individuals that really want guidance on this particular question. Your very own dedication to passing the message around had become exceptionally interesting and have truly empowered others much like me to get to their aims. Your amazing invaluable instruction indicates much to me and far more to my office colleagues. Many thanks; from each one of us.

  9. I must express my respect for your generosity supporting individuals that have the need for help on the situation. Your personal commitment to getting the solution all over had been really beneficial and has consistently allowed employees just like me to arrive at their pursuits. This warm and friendly suggestions indicates so much a person like me and far more to my office colleagues. Many thanks; from each one of us.

  10. I simply had to say thanks all over again. I do not know what I would have followed without those pointers contributed by you directly on such a area. It had been a real traumatic setting in my opinion, however , finding out a specialized form you dealt with that forced me to weep for fulfillment. Extremely grateful for the work and even hope you realize what a great job you are undertaking training the others thru a site. More than likely you have never encountered all of us.

  11. Thanks , I have just been searching for information about this topic for ages and yours is the best I have discovered so far. But, what about the conclusion? Are you sure about the source?

  12. There are definitely a variety of particulars like that to take into consideration. That could be a great point to convey up. I provide the ideas above as normal inspiration but clearly there are questions just like the one you deliver up the place an important thing will probably be working in trustworthy good faith. I don?t know if finest practices have emerged around issues like that, however I’m positive that your job is clearly recognized as a good game. Both girls and boys really feel the affect of just a moment抯 pleasure, for the remainder of their lives.

  13. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I get four emails with the identical comment. Is there any manner you can take away me from that service? Thanks!

  14. Oh my goodness! a tremendous article dude. Thank you Nonetheless I am experiencing concern with ur rss . Don抰 know why Unable to subscribe to it. Is there anyone getting an identical rss drawback? Anybody who knows kindly respond. Thnkx

  15. After study a few of the weblog posts in your web site now, and I really like your means of blogging. I bookmarked it to my bookmark website listing and can be checking again soon. Pls try my web page as nicely and let me know what you think.

  16. After looking into a number of the blog articles on your web site, I really like your technique of blogging. I added it to my bookmark website list and will be checking back in the near future. Take a look at my web site too and let me know your opinion.

jolvera进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注