var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

// cluster.isMaster 判断是不是master节点
if (cluster.isMaster) {
    console.log('[master] ' + "start master...");

    for (var i = 0; i < numCPUs; 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);
    });

    // cluster.isWorker 判断是不是worker节点
} 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(8080);
}
分类: node.js

毛巳煜

高级软件开发全栈架构师

工信部备案号:辽ICP备17016257号-2