셈플

var express = require("express" );
var http    = require("http"    );
var cluster = require("cluster" );
var os      = require("os"      );

if( cluster.isMaster){
    var cpu = os.cpus().length;

    for(var i=0; i<cpu; i++){
        cluster.fork();
    }

    cluster.on("exit"     , function(worker, code, signal){
        //process가 종료 되었을경우 다시 시작
        console.log("worker : exit #%s %s", worker.id, signal);
        console.log("worker restarting ...");
        cluster.fork();
    });
    cluster.on("online"   , function(worker){
        console.log("worker : online #%s, pid: %d", worker.id, 
                           worker.process.pid);
    });
    cluster.on("listening", function(worker, address){
        console.log("worker : listening %s", worker.id);
        console.log("  address: %s, port: %d", address.address, address.port);
    });
}else{
    var app = express();
    var server = http.createServer(app);

    server.listen(8090, function(){
        console.log("server listening on worker id : #"+cluster.worker.id);
    });    
}

실행결과

worker : online #1, pid: 32577
worker : online #2, pid: 32578
server listening on worker id : #1
worker : listening 1
   address: 0.0.0.0, port: 8090
server listening on worker id : #2
worker : listening 2
   address: 0.0.0.0, port: 8090

참고:

2013/10/22 13:12 2013/10/22 13:12
글 걸기 주소 : 이 글에는 트랙백을 보낼 수 없습니다