nodejs集群可以使用cluster模块,
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}
或者使用 第三方pm2模块
pm2 start app.js -i max
pm2会好点,挂掉会自动重启
pm2内部也是使用 nodejs的 cluster模块 负载均衡