node. JS's cluster feature and the configuration in Express

Source: Internet
Author: User
Tags node server

node. JS has built-in cluster functionality under the v0.6.0 version, as a cluster module for multi-core processing of nodejs, and easier scripting for a load-balanced cluster.

The script references the other person's material, builds a server.js (because the virtual machine has only 1 cores, for the simulation multi-threading, so the use of numcpus+4)

var cluster = require(‘cluster‘);var http = require(‘http‘);var numCPUs = require(‘os‘).cpus().length;if (cluster.isMaster) {    console.log(‘[master] ‘ + "start master...");    for (var i = 0; i < numCPUs+4; i++) {         cluster.fork();    }    cluster.on(‘listening‘, function (worker, address) {        console.log(‘[master] ‘ + ‘listening: worker‘ + worker.id + ‘,pid:‘
+ worker.process.pid + ‘, Address:‘ + address.address + ":" + address.port); });} else if (cluster.isWorker) { console.log(‘[worker] ‘ + "start worker ..." + cluster.worker.id); http.createServer(function (req, res) { console.log(‘worker‘+cluster.worker.id); res.end(‘worker‘+cluster.worker.id+‘,PID:‘+process.pid); }).listen(3000);}

Start the server to see the log

$ node server.js[master] start master...[worker] start worker ...1[master] listening: worker1,pid:2925, Address:0.0.0.0:3000[worker] start worker ...3[master] listening: worker3,pid:2931, Address:0.0.0.0:3000[worker] start worker ...4[master] listening: worker4,pid:2932, Address:0.0.0.0:3000[worker] start worker ...2[master] listening: worker2,pid:2930, Address:0.0.0.0:3000worker4worker2worker1worker3worker4worker2worker1

With curl access you can see the route to a different process
curl 192.168.1.20:3000worker4,PID:2932curl 192.168.1.20:3000worker2,PID:2930

但如何和我们的Express框架结合起来呢,通过npm start实际上是启动了package.json上的script脚本
node ./bin/www

具体打开www文件,发现详细的创建Server的命令,所以需要直接修改这个文件.修改如下:

#!/usr/bin/env node

/**
* Module dependencies.
*/

var cluster = require (' cluster ');
var Numcpus = require (' OS '). CPUs (). length;
var app = require ('.. /app ');
var debug = require (' Debug ') (' myapp-express:server ');
var http = require (' http ');

if (cluster.ismaster) {
Console.log (' [Master] ' + ' Start master ... ');

for (var i = 0; i < numcpus+4; i++) {
Cluster.fork ();
}

Cluster.on (' Listening ', function (worker, address) {
Console.log (' [Master] ' + ' listening:worker ' + worker.id + ', pid: ' + Worker.process.pid + ', Address: ' + address.address + ":" + address.port);
});

Console.log (' [worker] ' + ' start worker ... ' + cluster.worker.id);
Http.createserver (APP). Listen (+);
/*
Http.createserver (APP) {
Console.log (' worker ' +cluster.worker.id);
Res.end (' worker ' +cluster.worker.id+ ', PID: ' +process.pid);

}). Listen (3000); */
}



启动后能正常访问网页

But it is not possible to display the called process information in the log in the background so we do not know if we are actually doing load balancing.

Study, the page or go to the routes directory under the Index.js module, so in index.js to add console.log information, so basically access to know when to go to which process implementation calls

Router.get ('/about ', function (req,res) {
Console.log (PROCESS.PID);
Res.render (' about ', {titile: "Introduction"});
});

In practice, the same Firefox request will be routed to the same process PID, and the shutdown will be routed to another PID on re-opening.




node. JS's cluster feature and the configuration in Express

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.