pomelo原生自帶的pomelo start等命令可以很好的在開發(fā)過程中使用,但是到產(chǎn)品部署階段就顯得有些力不從心倾贰。在pomelo集群部署方面,推薦使用pm2來運維部署pomelo服務器組。用過才知道真的比pomelo start等命令爽快很多挟冠,墻裂推薦鹏漆。
本文主要分享一個將pomelo config解析為pm2 config的腳本巩梢。
parse_pomelo_to_pm2_config.js
/**
* 將pomelo默認配置servers.json轉(zhuǎn)為pm2配置
* Created by kikoroc on 16/4/26.
*/
varenv ='production';
varservers =require('../servers.json')[env];
varmaster =require('../master.json')[env];
varmasterha =require('../masterha.json').masterha;
varfs =require('fs');
/**
* 一臺服務器可能有多個網(wǎng)絡類型的ip地址(電信移動聯(lián)通局域網(wǎng)等)创泄,這里主要配置將服務器多個ip解析為同一個ip
* 這樣就可以將同服務器的pomelo config配置到一個pm2 config json文件中。
*/
var_SERVER_IPMAP = {
"your_server_telecom_ip":"your_server_telecom_ip",
"mobile_ip":"your_server_telecom_ip",
"unicom_ip":"your_server_telecom_ip",
"local_ip":"your_server_telecom_ip"
};
var_RESULT = {};
for(varserverTypeinservers){
servers[serverType].forEach(function(conf){
varprocess = {
"name":"",
"cwd":"/data/xxx/game-server/",
"script":"app.js",
"args":null,
"exec_mode":"fork",
"watch":false,
"env": {
"NODE_ENV":"production"
? ? ? ? ? ? },
"merge_logs":true,
"error_file":"",
"out_file":"",
"autorestart":true,
"max_memory_restart":"1G"
? ? ? ? };
? ? ? ? process.name = conf.id;
process.error_file ="/data/xxx/game-server/logs/"+conf.id+"_error.log";
process.out_file ="/data/xxx/game-server/logs/"+conf.id+"_normal.log";
varargs = [];
args.push("env=production");
args.push("serverType="+serverType);
for(varkinconf){
args.push(k+"="+conf[k]);
? ? ? ? }
? ? ? ? process.args = args;
vardestHost = _SERVER_IPMAP[conf.host];
if(_RESULT.hasOwnProperty(destHost)){
? ? ? ? ? ? _RESULT[destHost].apps.push(process);
}else{
? ? ? ? ? ? _RESULT[destHost] = {};
_RESULT[destHost]['apps'] = [];
? ? ? ? ? ? _RESULT[destHost].apps.push(process);
? ? ? ? }
? ? });
}
masterha.forEach(function(conf){
fs.writeFileSync('./out/masterha@'+_SERVER_IPMAP[conf.host]+'.json',JSON.stringify({
"apps": [{
"name": conf.id,
"cwd":"/data/xxx/game-server/",
"script":"app.js",
"args": [
"serverType=master",
"id="+conf.id,
"host="+conf.host,
"port="+conf.port,
"env=production",
"mode=stand-alone",
"masterha=true"
? ? ? ? ? ? ],
"exec_mode":"fork",
"watch":false,
"env": {
"NODE_ENV":"production"
? ? ? ? ? ? },
"merge_logs":true,
"error_file":"/data/xxx/game-server/logs/"+conf.id+"_error.log",
"out_file":"/data/xxx/game-server/logs/"+conf.id+"_normal.log",
"autorestart":true,
"max_memory_restart":"1G"
? ? ? ? }]
},null,2), {
flag:'w'
? ? });
});
//write master config
fs.writeFileSync('./out/master@'+_SERVER_IPMAP[master.host]+'.json',JSON.stringify({
"apps": [{
"name": master.id,
"cwd":"/data/xxx/game-server/",
"script":"app.js",
"args": [
"serverType=master",
"id="+master.id,
"host="+master.host,
"port="+master.port,
"env=production",
"mode=stand-alone"
? ? ? ? ],
"exec_mode":"fork",
"watch":false,
"env": {
"NODE_ENV":"production"
? ? ? ? },
"merge_logs":true,
"error_file":"/data/xxx/game-server/logs/"+master.id+"_error.log",
"out_file":"/data/xxx/game-server/logs/"+master.id+"_normal.log",
"autorestart":true,
"max_memory_restart":"1G"
? ? }]
},null,2), {
flag:'w'
});
for(varhostin_RESULT){
fs.writeFileSync('./out/'+host+'.json',JSON.stringify(_RESULT[host],null,2), {
flag:'w'
? ? });
}
運行
node parse_pomelo_to_pm2_config.js
pm2的json config文件就會生成在./out/目錄下括蝠。然后分發(fā)到對應的服務器執(zhí)行pm2 start xxx.json即可验烧。
在實際運維過程中,可能需要重啟某些serverType的服務器進程又跛,這里再分享一個根據(jù)serverType生成對應服務器進程重啟的腳本碍拆。
gen_restart_shell_by_serverType.js
/**
* 生成重啟業(yè)務服務進程的pm2代碼
* Created by kikoroc on 16/4/27.
*/
varserverType ='chat';
varservers =require('../../servers.json')['production'][serverType];
if(servers && servers.length >0){
servers.forEach(function(svr){
console.log('ssh '+svr.host+' /usr/local/bin/pm2 restart '+svr.id);
? ? });
}else{
console.error('no server of '+serverType);
}
運行
node gen_restart_shell_by_serverType.js > restart_chat.sh
bash restart_chat.sh
即可完成chat servers的重啟。:)