最近项目使用自带的cluster,由于没有监听重启进程等功能,导致多个线程死掉后压力都到一个存活的线程上导致CPU负载过高,优化使用第三方cluster2(https://github.com/ql-io/cluster2)
1、 引用模块,package.json中进行引用
var Cluster = require('cluster2');//声明模块
var numCPUs = require('os').cpus().length-1;//获取CPU几核,起n-1个进程,留一个用来处理其它内容减少上下文切换。
var c = new Cluster({
port:8080,
cluster: true,
noWorkers:numCPUs
});
c.on('died', function(pid) {
console.log('Worker ' + pid + ' died');
});
c.on('forked', function(pid) {
console.log('Worker ' + pid + ' forked');
});
c.on('SIGKILL', function() {
console.log('Got SIGKILL');
});
c.on('SIGTERM', function(event) {
console.log('Got SIGTERM - shutting down');
});
c.on('SIGINT', function() {
console.log('Got SIGINT');
});
c.listen(function(cb){
cb(app);
});