master掛了的話pm2怎麽處理 使用pm2方便開啟node集群模式
本文為轉載
Introduction
As you would probably know, Node.js is a platform built on Chrome‘s JavaScript runtime, gracefully named V8.
The V8 engine, and hence Node.js, runs in a single-threaded way, therefore, doesn‘t take advantage of multi-core systems capabilities.
介紹
你應該知道,Node.js是一個運行在名叫V8的JavaScript引擎的平臺系統。V8本身是單線程運行的,並沒有充分利用多核系統能力。
Node.js cluster module
Luckily enough, Node.js offers the cluster module, which basically will spawn some workers which can all share any TCP connection.
How does it work ?
Cluster module will set up a master and then fork your server app as many times as you want it to (also called a worker).
When using Round-robin scheduling policy, the master accepts() all incoming connections and sends the TCP handle for that particular connection to the chosen worker (still via IPC).
How to use it ?
The most basic example is the following :
Node.js的集群模式
幸運的是,Node.js提供了集群模塊,簡單講就是復制一些可以共享TCP連接的工作線程。
工作原理
集群模塊會創建一個master主線程,然後復制任意多份程序並啟動,這叫做工作線程。
工作線程通過 IPC 頻道進行通信並且使用了 Round-robin algorithm 算法進行工作調度以此實現負載均衡。
Round-robin調度策略主要是master主線程負責接收所有的連接並派發給下面的各個工作線程。
如何使用
下面是一個很常見的例子:
var cluster = require(‘cluster‘);
var http = require(‘http‘);
var os = require(‘os‘);
var numCPUs = os.cpus().length;
if (cluster.isMaster) {
// Master:
// Let‘s fork as many workers as you have CPU cores
for (var i = 0; i < numCPUs; ++i) {
cluster.fork();
}
} else {
// Worker:
// Let‘s spawn a HTTP server
// (Workers can share any TCP connection.
// In this case its a HTTP server)
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world");
}).listen(8080);
}
Of course, you can spawn as many workers as you wish. You‘re not limited by the CPU cores number since a worker is nothing more but a child process.
As you can see, to make it work, you have to wrap your code inside some cluster handling logic and then add some more code to specify the expected behaviour in case your worker dies unexpectedly.
你可以不受CPU核心限制的創建任意多個工作線程。
使用原生方法有些麻煩而且你還需要處理如果某個工作線程掛掉了等額外的邏輯。
(註:通過fork()復制的進程都是獨立的進程,有著全新的V8實例)
The PM2 way
Built-in clustering
PM2 internally handles all of the above logic for you so you don‘t have to change anything in your code.
The previous code becomes :
PM2的方式
PM2內置了處理上述的邏輯,你不用再寫這麽多繁瑣的代碼了。
只需這樣一行:
$ pm2 start app.js -i 4
-i <number of workers>
表示實例程序的個數。就是工作線程。
如果i
為0表示,會根據當前CPU核心數創建
Keeping your apps running no matter what
If any of your workers happens to die, PM2 will restart them immediatly so you don‘t have to worry about that either.
Or, of course, you can, at any time, restart them manually as follows :
保持你的程序不中斷運行
如果有任何工作線程意外掛掉了,PM2會立即重啟他們,當前你可以在任何時候重啟,只需:
image.png
Scaling your cluster in realtime
If you consider that you don‘t have enough workers or more than needed, you can scale your cluster anytime by hitting pm2 scale <app name> <n> where <n> can be a consistent number which the cluster will scale up or down to.
It can also be an addition such as pm2 scale app +3 in which case 3 more workers will be added to the cluster.
實時調整集群數量
你可以使用命令 pm2 scale <app name> <n>
調整你的線程數量,
如 pm2 scale app +3 會在當前基礎上加3個工作線程。
Updating your apps in production with zero downtime
PM2 reload <app name> feature will restart your workers one by one, and for each worker, wait till the new one has spawned before killing the old one.
This way, your server keeps running even when you are deploying the new patch straight to production.
You can also use gracefulReload feature which does pretty much the same thing but instead of immediatly killing the worker it will send it a shutdown signal via IPC so it can close ongoing connections or perform some custom tasks before exiting gracefully.
Example :
在生產環境讓你的程序永不中斷
PM2 reload <app name>
命令會一個接一個的重啟工作線程,在新的工作線程啟動後才結束老的工作線程。
這種方式可以保持你的Node程序始終是運行狀態。即使在生產環境下部署了新的代碼補丁。
也可以使用gracefulReload命令達到同樣的目的,它不會立即結束工作線程,而是通過IPC向它發送關閉信號,這樣它就可以關閉正在進行的連接,還可以在退出之前執行一些自定義任務。這種方式更優雅。
process.on(‘message‘, function(msg) {
if (msg === ‘shutdown‘) {
close_all_connections();
delete_cache();
server.close();
process.exit(0);
}
});
Conclusion
Cluster module is a powerful tool. It gets even better and easy to use along with PM2.
Cluster.js was experimental on Node 0.10.x and is considered to be mature and production-ready since Node 0.11.x latest releases and of course Node 0.12.x.
We strongly suggest you to always use the latest version of Node.js and PM2 since both of these projects‘ contributors are working hard every day to make them better.
Enjoy Node.js‘ clustering with PM2 !
結論
Cluster集群模式非常強悍有用,此功能是在Node 0.10.x 是實驗功能,在0.11.x 之後才作為正式發布。
強烈建議你使用最新版本的Node.js和PM2。
作者:飛凡的陀螺
鏈接:https://www.jianshu.com/p/af789dbd5db8
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並註明出處。
master掛了的話pm2怎麽處理 使用pm2方便開啟node集群模式