var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
var worker = cluster.fork();
addListener(worker);
}
cluster.on('setup', function(settings) { //配置完成后
console.log('[master] setup: settings: ' + JSON.stringify(settings));
});
cluster.on('fork', function(worker) {//fork完成后
console.log('[master] fork: worker' + worker.id + ',pid:' + worker.process.pid);
});
cluster.on('online', function(worker) {//worker运行后
console.log('[master] online: worker' + worker.id + ',pid:' + worker.process.pid);
});
cluster.on('listening', function(worker, address) {//worker进入监听状态
console.log('[master] listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
});
cluster.on('disconnect', function(worker) {//worker断开
console.log('[master] disconnect: worker' + worker.id + ',pid:' + worker.process.pid);
});
cluster.on('exit', function(worker, code, signal) {//worker退出
console.log('[master] exit: worker' + worker.id + ',pid:' + worker.process.pid + ', code:' + code + ', signal:'+signal);
var worker = cluster.fork(); //重新启动一个worker
addListener(worker);
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
console.log('[worker] ' + "start worker ..." + cluster.worker.id);
http.createServer(function(req, res) {
res.writeHead(200);
console.log('worker'+cluster.worker.id);
res.end('worker'+cluster.worker.id+',PID:'+process.pid);
}).listen(8000);
process.send('hi master, I am worker' + cluster.worker.id + '.');//向master发送消息
process.on('message', function(msg) { //监听master发过来的消息
console.log('[worker] message: got message from master [' + msg +']');
});
}
function addListener(worker){
// worker.on('online', function() {
// console.log('[worker] online: worker' + worker.id + ',pid:' + worker.process.pid);
// });
// worker.on('listening', function(address) {
// console.log('[worker] listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
// });
worker.on('message', function(msg) {// 监听worker发过来的消息
console.log('[master] message: got message from worker [' + msg +']');
worker.send('hello, worker'+worker.id);//向worker发送消息
});
// worker.on('disconnect', function() {
// console.log('[worker] disconnect: worker' + worker.id + ',pid:' + worker.process.pid);
// });
// worker.on('exit', function(code, signal) {
// console.log('[worker] exit: worker' + worker.id + ',pid:' + worker.process.pid + ', code:' + code + ', signal:'+signal);
// });
}
引用http://www.infoq.com/cn/articles/nodejs-cluster-round-robin-load-balancing的解释
写道
Windows上的注意事项
MS Windows是默认使用老办法的唯一平台。为了达到性能最优,Node.js在Windows上使用了IOCP。尽管这在大多数情况下都不错,但这样将HANDLE对象(连接)发送给其它进程代价十分高昂。 尽管有可能在libuv中解决这个问题,但我们还不清楚是否真的有必要这么做:Windows的端口几乎不会受到负载均衡问题的影响,而Linux和Solaris的端口确实会受影响。
MS Windows是默认使用老办法的唯一平台。为了达到性能最优,Node.js在Windows上使用了IOCP。尽管这在大多数情况下都不错,但这样将HANDLE对象(连接)发送给其它进程代价十分高昂。 尽管有可能在libuv中解决这个问题,但我们还不清楚是否真的有必要这么做:Windows的端口几乎不会受到负载均衡问题的影响,而Linux和Solaris的端口确实会受影响。