节点js是否应该表达服务器集群进程在多个套接字上监听?

时间:2019-06-10 22:36:15

标签: javascript node.js sockets nginx multiprocessing

我正在使用nginx作为Node JS Express服务器前面的反向代理。可以使用节点集群模块创建多个服务器进程,这些服务器进程像这样监听单个unix域套接字,

const cluster = require('cluster');
const http = require('http')

function start() {
  const expressServer = require('./main')
  const server = http.createServer(expressServer)
  server.listen(`/tmp/nginx.socket`)
}


if(cluster.isMaster) {
  // Master process: fork our child processes.
  const numWorkers = process.env.WEB_CONCURRENCY || 1;
  for (var i = 0; i < numWorkers; i += 1) {
    console.log('** Booting new worker **');
    cluster.fork();
  }

  // Respawn any child processes that die
  cluster.on('exit', function(worker, code, signal) {
    console.log('process %s died (%s). restarting...', worker.id, signal || code);
    cluster.fork();
  });

} else {

  start()

}

或者,我可以生成每个进程,让它像这样在自己的域套接字上侦听,

const cluster = require('cluster');
const http = require('http')

function start() {
  const expressServer = require('./main')
  const server = http.createServer(expressServer)
  server.listen(`/tmp/nginx-${process.env['WORKER_ID']}.socket`)
}


if(cluster.isMaster) {
  // Master process: fork our child processes.
  const numWorkers = process.env.WEB_CONCURRENCY || 1;
  for (var i = 0; i < numWorkers; i += 1) {
    console.log('** Booting new worker **');
    cluster.fork({'WORKER_ID': (i + 1)});
  }

  // Respawn any child processes that die
  cluster.on('exit', function(worker, code, signal) {
    console.log('process %s died (%s). restarting...', worker.id, signal || code);
    cluster.fork();
  });

} else {
  start()
}

在这里使用多个域套接字是否有令人信服的理由?

0 个答案:

没有答案