Nodejs Expressjs Cluster 正确的集群方式 app.js
Nodejs Expressjs Cluster Proper way to cluster app.js
有谁知道集群 nodejs express 应用程序的正确方法是什么?为什么?
选项 A:创建一次应用程序实例,并在创建的每个分支上调用监听。
var app = require('express')();
if (cluster.isMaster) {
//fork once per cpu
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
//if a worker is online
cluster.on('online', function WorkerOnline(worker) {
console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
});
//if a worker dies, log that and create a new one
cluster.on('exit', function workerDed(worker, code, signal) {
cluster.fork();
});
} else {
//non-master forks listen on the ip/port specified in the config
app.listen(config, function () {
console.log('%s Express server listening on ip %s:%s ', new Date().toISOString(), config.ip, config.port);
});
}
选项 B:创建应用程序并在每次有分叉时调用 listen。
if (cluster.isMaster) {
//fork once per cpu
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
//if a worker is online
cluster.on('online', function workeronline(worker) {
console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
});
//if a worker dies, log that and create a new one
cluster.on('exit', function workeronline(worker, code, signal) {
cluster.fork();
});
} else {
//non-master forks listen on the ip/port specified in the config
var app = require('express')();
app.listen(8000, function() {
console.log('Process ' + process.pid + ' is listening to all incoming requests');});
}
两者都可以。当 Node.js 调用 cluster.fork()
时,它会运行一个新的节点实例并再次调用您的 app.js 文件。因此,行 var app = express();
可以在任何地方调用,并且您可以保证它与在其他实例上实例化的 Express 对象不同(即您的主进程和从进程不共享 app
变量)。
但是,选项 B 更清楚地表明您每次 fork 时都在创建一个新的 Express 实例。另外,在方案A中,你在主和从进程上创建了一个Express对象,但是主进程并没有使用你创建的Express对象。
注意这个块:
} else {
app.listen(config, function () {
console.log('%s Express server listening on ip %s:%s ',
new Date().toISOString(), config.ip, config.port);
});
}
如果 Express 对象是一个子进程,您只需要让 Express 对象监听一个端口即可;在选项 A 中的 else
块之外调用 var app = express();
是毫无意义的,因为您创建了一个 Express 对象,但它不在主进程中使用。这让人质疑为什么你会想要使用选项 A。
有谁知道集群 nodejs express 应用程序的正确方法是什么?为什么?
选项 A:创建一次应用程序实例,并在创建的每个分支上调用监听。
var app = require('express')();
if (cluster.isMaster) {
//fork once per cpu
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
//if a worker is online
cluster.on('online', function WorkerOnline(worker) {
console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
});
//if a worker dies, log that and create a new one
cluster.on('exit', function workerDed(worker, code, signal) {
cluster.fork();
});
} else {
//non-master forks listen on the ip/port specified in the config
app.listen(config, function () {
console.log('%s Express server listening on ip %s:%s ', new Date().toISOString(), config.ip, config.port);
});
}
选项 B:创建应用程序并在每次有分叉时调用 listen。
if (cluster.isMaster) {
//fork once per cpu
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
//if a worker is online
cluster.on('online', function workeronline(worker) {
console.log('%s Worker is running on %s pid: ', new Date().toISOString(), worker.process.pid);
});
//if a worker dies, log that and create a new one
cluster.on('exit', function workeronline(worker, code, signal) {
cluster.fork();
});
} else {
//non-master forks listen on the ip/port specified in the config
var app = require('express')();
app.listen(8000, function() {
console.log('Process ' + process.pid + ' is listening to all incoming requests');});
}
两者都可以。当 Node.js 调用 cluster.fork()
时,它会运行一个新的节点实例并再次调用您的 app.js 文件。因此,行 var app = express();
可以在任何地方调用,并且您可以保证它与在其他实例上实例化的 Express 对象不同(即您的主进程和从进程不共享 app
变量)。
但是,选项 B 更清楚地表明您每次 fork 时都在创建一个新的 Express 实例。另外,在方案A中,你在主和从进程上创建了一个Express对象,但是主进程并没有使用你创建的Express对象。
注意这个块:
} else {
app.listen(config, function () {
console.log('%s Express server listening on ip %s:%s ',
new Date().toISOString(), config.ip, config.port);
});
}
如果 Express 对象是一个子进程,您只需要让 Express 对象监听一个端口即可;在选项 A 中的 else
块之外调用 var app = express();
是毫无意义的,因为您创建了一个 Express 对象,但它不在主进程中使用。这让人质疑为什么你会想要使用选项 A。