셈플
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
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
참고:
"JavaScript / Node.JS" 분류의 다른 글
[node.js] ajax로 타 도메인과 연동 문제 / CORS(Cross-Origin Resource Sharing) (0) | 2013/12/20 |
[node.js] traceback (0) | 2013/10/22 |
[node.js] log4js PatternLayout 사용하기 (0) | 2013/10/21 |
[node.js] Strict mode (0) | 2013/10/21 |