我正在使用节点cluster
我的群集启动文件是典型的
'use strict';
(function() {
/// node clustering
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
var clusterConfig=require('./config/clusterconfig.json');
if (cluster.isMaster) { // master node
// process=master
console.log(`Master ${process.pid} is running`);
// Fork workers.
var maxCPUs = clusterConfig.master.workers;
maxCPUs=(maxCPUs>=numCPUs)?numCPUs:maxCPUs;
for (let i = 0; i < maxCPUs; i++) {
const worker=cluster.fork();
}
var masterConfig=require('./config/masterconfig.json');
// configuration override
if( process.env.MASTER_PORT ) { // override master port
masterConfig.http.options.port = process.env.MASTER_PORT;
}
if( process.env.NODE_ID ) { // override node identifier
masterConfig.pubsub.node = process.env.NODE_ID;
}
var MasterNode=require('./lib/master');
var master= new MasterNode(masterConfig);
master.start()
.then(done=> {
console.log(`master ${process.pid} started`);
})
.catch(error=> {
console.error("master error",error);
});
}
else if (cluster.isWorker) { // worker node
// process=worker
console.log(`Worker ${process.pid} started`);
var workerConfig=require('./config/workerconfig.json');
// configuration override
if( process.env.WORKER_PORT ) { // override worker port
workerConfig.http.options.port = process.env.WORKER_PORT;
}
if( process.env.NODE_ID ) { // override node identifier
workerConfig.pubsub.node = process.env.NODE_ID;
}
var WorkerNode=require('./lib/worker');
var worker= new WorkerNode(workerConfig);
worker.start()
.then(done=> {
console.log(`worker ${process.pid} started`);
})
.catch(error=> {
console.error(`worker ${process.pid} error`,error);
});
}
}).call(this);
我根据服务器上的cpus数量和配置运行指定数量的worker。
在我的Web应用程序逻辑中,master
节点加载一些共享资源,而worker
节点处理api,即传入的响应将从worker api进程路由到主节点当需要这些共享资源时,通过本地呼叫进行处理。
使用StrongLoop时,流程管理器中的群集逻辑为implemented,但我不确定如何将共享资源逻辑从我的节点cluster
模型迁移到StrongLoop模型。