![](/img/trans.png)
[英]How to Use Node Cluster module within Express Generator App Skeleton
[英]Node express app using cluster is not working
我有一個使用 node.js (express) 作為后端的應用程序。 我現在正在嘗試使用 node.js 集群來提高應用程序性能,但沒有運氣......
這是我的工作 app.js:
import { Server } from "./server";
import { Debug } from "./utils/Debug";
const cluster = require("cluster");
const http = require("http");
const numCPUs = require("os").cpus().length;
const httpPort = 3000;
var app = Server.bootstrap().app;
app.set("port", httpPort);
var httpServer = http.createServer(app);
httpServer.listen(httpPort);
這是使用集群的代碼(不起作用):
import { Server } from "./server";
import { Debug } from "./utils/Debug";
const cluster = require("cluster");
const http = require("http");
const numCPUs = require("os").cpus().length;
const httpPort = 3000;
Debug.log(numCPUs + " CPU(s)!");
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
// If a worker dies, log it to the console and start another worker.
cluster.on("exit", function (worker, code, signal) {
console.log("Worker " + worker.process.pid + " died.");
cluster.fork();
});
cluster.on("online", function (worker) {
console.log("Worker " + worker.process.pid + " is online");
});
// Log when a worker starts listening
cluster.on("listening", function (worker, address) {
console.log("Worker started with PID " + worker.process.pid + ".");
});
} else {
/**
* Create HTTP server.
*/
var app = Server.bootstrap().app;
app.set("port", httpPort);
var httpServer = http.createServer(app);
httpServer.listen(httpPort);
}
當我運行上面的內容時,我的 localhost:3000 沒有在聽。
這里有什么問題?
謝謝你。
我決定讓 PM2 管理集群的事情。 所以我安裝了它並使用這樣的生態系統.config.js 文件來管理我的應用程序:
module.exports = {
apps: [
{
name: "myApp",
script: "./dist/app.js",
instances: "max",
exec_mode: "cluster",
max_memory_restart: "300M",
error_file: 'err.log',
out_file: 'out.log',
log_file: 'combined.log',
time: true,
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
},
},
],
};
我希望這將有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.