1. 程式人生 > >master掛了的話pm2怎麼處理 使用pm2方便開啟node叢集模式

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程式碼執行在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).
It communicates with workers via

IPC channels and comes with an embedded load-balancer which uses Round-robin algorithm to better distribute load among the workers.
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核心數建立

  image.png

 

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個工作執行緒。

  image.png

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
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。