Introduced
As we all know, node. js runs on Chrome's JavaScript runtime platform, and we call that platform an elegant V8 engine. Both the V8 engine and the later node. JS are run in a single-threaded fashion, so the maximum performance is not maximized in a multi-core processor-based system.
node. JS Cluster module
Fortunately, node. JS provides us with the cluster module, which can generate multiple worker threads to share the same TCP connection.
How does it work?
First, cluster creates a master and then copies multiple server apps (also known as worker threads) based on the number you specify. It communicates with the worker thread through the IPC channel and uses built-in load balancing to better handle the stress between threads, which uses the Round-robin algorithm (also known as the Loop algorithm).
When the Round-robin scheduling policy is used, Master accepts () All incoming connection requests, and then sends the corresponding TCP request processing to the selected worker thread (which still communicates through the IPC).
How to use it?
The following is a basic example:
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 has 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 the case of its a HTTP server) http.createserver (function (req, res) { res.writehead ($); Res.end ("Hello World") . Listen (8080);}
Of course, you can specify any number of worker threads, and the number of threads is not limited to the number of CPU cores, as it is just a child thread running on the CPU.
As you can see, to make it work, you need to encapsulate your code in cluster's processing logic and add some extra code to specify how to handle a thread after it is hung.
Built-in cluster using the PM2 method
The PM2 contains all of the above processing logic, so you don't have to make any changes to the code. We will restore the above code to the most primitive form:
var http = require (' http '); Http.createserver (function (req, res) { res.writehead ($); Res.end ("Hello World"). Listen (8080);
Then execute it on the console:
$ PM2 Start App.js-i 4
The-i <number of workers> parameter is used to tell PM2 to run your app in the form of Cluster_mode (called Fork_mode), followed by numbers indicating the number of worker threads to start. If the given number is 0,PM2, the corresponding worker thread is generated based on the number of cores in your CPU.
Whatever the case, keep your apps running
If any worker thread hangs up, don't worry, PM2 will restart it immediately. Of course, you can also manually restart these threads at any time:
Expand your cluster in real time
At any time, if you need to increase the number of worker threads, you can extend the cluster by PM2 scale <app name> <n>. Parameters <n> Specifies the number of worker threads that are used to increase or decrease the number of clusters. You can also specify how many worker threads you want to add by PM2 Scale app +3.
Zero downtime updates in the product environment
The Reload <app name> feature of PM2 will restart all worker threads in turn. Each thread waits for the new thread to be created before it is terminated, so when you deploy new code in the product environment, the server keeps running continuously.
Using the Gracefulreload function can achieve the same purpose, but instead of terminating the worker thread immediately, it sends a shutdown signal through the IPC to close all current connections and handle some custom tasks before gracefully exiting. As in the following code:
Process.on (' message ', function (msg) { if (msg = = = ' Shutdown ') { close_all_connections (); Delete_cache (); Server.close (); Process.exit (0); });
Configure PM2 to start automatically
To PM2 an app that runs before the server restarts, start your app with PM2 start, and then execute the following command:
PM2 Save
This will generate a dump.pm2 file in the ~/.PM2 directory that describes all the applications running on the current PM2. Then execute the command:
PM2 Startup [Platform]
Note It is necessary to add optional parameter platform to explicitly inform PM2 of the current system environment. This way, the next time the server restarts, PM2 will automatically run the previously saved app.
Conclusion
The cluster module is very powerful and makes it easier to use PM2. In the node 0.10.x era Cluster.js was still a pilot, but since node 0.11.x it has matured and is ready to be released, including the node 0.12.x version. The latest version of node. JS and PM2 are highly recommended, and contributors to these products have been working and making them better.
Enjoy the convenience of PM2 with the node. JS Cluster operation!
Original address: https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/
For more information about the installation and use of PM2, you can view the documentation here: http://pm2.keymetrics.io/docs/usage/quick-start/
node. JS uses PM2 clusters to become easier