Pm2
GitbookBackend2020-12-29
📑 使用 pm2 进行进程管理
启动和管理一个网站的办法,会有很多种:
- pm2
- forever
- nodemon
- concurrency
测试机我们使用concurrency
方便快速发布, 产品机我们使用pm2
,保证任何时候不 down 机。
启动脚本在bin/pm2.sh
中。
使用的 pm2 另外的一大好处是,它提供强大和多种辅助功能,比如自动管理日志及备份,自动进行负载均衡。
pm2
解决 down 机问题。- 服务端
gateway
,ms-api
用pm2
管理。
📑 用 pm2 管理服务启动
$ pm2 start yarn -- start
$ pm2 start yarn -- run <scriptname>
$ pm2 start yarn --watch --name <taskname> -- run start
$ pm2 logs [--raws]
$ pm2 reloadLogs
$ pm2 flush #清空
$ pm2 stop all # Stop all processes
$ pm2 restart all # Restart all processes
$ pm2 delete 0 # Will remove process from pm2 list
$ pm2 delete all # Will remove all processes from pm2 list
$ pm2 monit
📑 pm2:不 down 机运行 JS 服务器
因为 NodeJS 的 Express 服务器是单线程的,容易引起 down 机。开发用concurrency
无法防止这种情况,所有该用pm2
。
如图,这样可以很方便的管理,监控多个 JS 微服务。
- PM2 is really great at handling crash and monitoring Node apps as well as load balancing. PM2 immediately starts the Node app whenever it crashes, stops for any reason or even when server restarts. So, if someday even after managing our code, app crashes, PM2 can restart it immediately.
- . Production process manager for Node.js apps with a built-in load balancer. please avoid uncaughtException for this problem. https://github.com/Unitech/pm2