Node.js를 멀티 코어에서 동작하도록 해주는 방법에 대해서 소개하도록 하겠습니다.

 

http://learnboost.github.com/cluster/ - Github Home

http://learnboost.github.com/cluster/docs/api.html - API Document

https://github.com/learnboost/cluster - Github Source

 

 

# npm install cluster

 

 

--------------------------------------------------------------------------------

server.js 파일

var http = require('http');

var server = http.createServer(function(req, res){
    res.writeHead(200);
    res.end('Hello World');
});

// 서버 시작
if (module === require.main)
    server.listen(4500);
else
    module.exports  = server;

console.log('listen on port 4500');

 

---------------------------------------------------------------------------------

cluster.js 파일

var cluster = require('cluster');
cluster('server')
    .use(cluster.repl('/var/run/cluster.sock'))
    .listen(4500);

 

 

node cluster.js – node.js 시작

listen on port 4500

listen on port 4500

listen on port 4500

listen on port 4500

listen on port 4500

listen on port 4500

listen on port 4500

listen on port 4500

 

# telnet /var/run/cluster.sock

cluster> help()

Commands

help(): Display help information

spawn(n): Spawn one or more additional workers

pids(): Output process ids

kill(id, signal): Send signal or SIGTERM to the given worker

shutdown(): Gracefully shutdown server

stop(): Hard shutdown

restart(): Gracefully restart all workers

echo(msg): echo the given message

stats(): Display server statistics

 

cluster> pids()

master: xxxxx

worker #0: xxxxx

worker #1: xxxxx

worker #2: xxxxx

worker #3: xxxxx

worker #4: xxxxx

worker #5: xxxxx

worker #6: xxxxx

worker #7: xxxxx

 

 

다른 예제

---------------------------------------------------------------------------------

app.js

var http = require('http');

module.exports = http.createServer(function(req, res){
  console.log('%s %s', req.method, req.url);
  var body = 'Hello World';
  res.writeHead(200, { 'Content-Length': body.length });
  res.end(body);
});

---------------------------------------------------------------------------------

server.js

var cluster = require('cluster')
  , app = require('./app');

cluster(app)
  .use(cluster.logger('logs'))
  .use(cluster.stats())
  .use(cluster.pidfiles('pids'))
  .use(cluster.cli())
  .use(cluster.repl(8888))
  .listen(3000);

---------------------------------------------------------------------------------

 

 

참고 URL : http://www.screenr.com/X8v Video

+ Recent posts