别在生产环境中直接用node(用pm2
)
假如我们有一个 nodejs的script,例如main.js
,别在生产环境(production)中直接用
// package.json
"scripts": {
"start": "node main.js",
}
来调用这个服务。因为main.js
中间有一个地方出错,整个service都down了。
为了容错性(robustness)和扩展性(scalability),推荐使用 pm2
来作为生产环境的nodejs程序的进程管理器。
例如
// package.json
"scripts": {
"start": "pm2 start main.js",
},
它对出错可以自动重启,另外pm2
还支持其他的配置文件,例如提供多线程或者多进程的
原作者Burke Holland是站在Azure的角度讨论的,并提到了朋友Tierney Cyren推荐了Linux的新的systemd
来管理。
以上笔记基于原文:You should never ever run directly against Node.js in production. Maybe.,作者 Burke Holland
Note: 我想到Heroku用的Procfile
替我管理了进程的scale和restart等问题。