简体   繁体   English

为什么 NodeJs 集群模块不起作用?

[英]Why is NodeJs cluster module not working?

So basically I am building an express server and I recently looked into the topic of scaling NodeJS apps.所以基本上我正在构建一个express服务器,我最近研究了扩展 NodeJS 应用程序的主题。 One of the first things I saw was the built in cluster module to take advantage of all the threads of the machine's processor.我看到的第一件事是内置cluster模块,以利用机器处理器的所有线程。 Here is my code of the implementation I've made:这是我所做的实现代码:

import cluster from "cluster";
import { cpus } from "os";

import dotenv from "dotenv";
dotenv.config();

import express, { Express } from "express";
import log from "./logger";
import database from "./database";
import router from "./router";

const app: Express = express();

// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

const port = <number>(<unknown>process.env.PORT);
const host = <string>process.env.HOST;

const numCPU = cpus().length;
if (cluster.isPrimary) {
   for (let i = 0; i < numCPU; i++) {
      cluster.fork();
   }

   cluster.on("listening", (worker) => {
      // Not printing anything
      console.log(worker.process.pid);
   });

   cluster.on("exit", (worker, code, signal) => {
      log.error(`Worker ${worker.process.pid} died. Starting a new worker...`);
      cluster.fork();
   });
} else {
   app.listen(port, host, () => {
      // Only prints this once.
      log.info(`Server ${process.pid} listening at http://${host}:${port}`);
      database();
      router(app);
   });
}

The problem is that the cluster.isPrimary block never runs meaning of course that no other processes are forked by the cluster.问题是cluster.isPrimary从不运行当然意味着集群没有其他进程分叉。 However when I use cluster.isMaster , everything works exactly as intended.但是,当我使用cluster.isMaster时,一切都按预期工作。 But since isMaster is deprecated and is replaced by isPrimary, thats what I'm trying to use but it just doesn't work.但是由于 isMaster 已被弃用并被 isPrimary 取代,这就是我想要使用的,但它不起作用。

Solutions I've tried that don't work:我尝试过但不起作用的解决方案:

  1. Kill all node processes on my machine.杀死我机器上的所有节点进程。
  2. Remove everything from this main file leaving just the barebone app and cluster configuration.从此主文件中删除所有内容,只留下准系统应用程序和集群配置。

What am I doing wrong?我究竟做错了什么?

Its most likely the version of node you are running.它最有可能是您正在运行的节点版本。 isPrimary is only available in versions 16+ and you most likely have the LTS version which is v14 isPrimary 仅在 16+ 版本中可用,您很可能拥有 LTS 版本,即 v14

Some time ago, cluster.isMaster added in NODE version v0.8.1 is deprecated since version 16.0.0.前段时间,在 NODE 版本 v0.8.1 中添加的cluster.isMaster从 16.0.0 版本开始被弃用。 Simply replace deprecated cluster.isMaster by cluster.isPrimary (as you did) only if your NODE version is at least 16.0.0.仅当您的 NODE 版本至少为 16.0.0 时,只需将已弃用的cluster.isMaster替换为cluster.isPrimary (就像您所做的那样)。

If you don't know your version, enter node -v and hit enter in your terminal.如果您不知道您的版本,请输入node -v并在终端中按回车键。 If you need to update your NODE version, you have different options depending on your operating system.如果您需要更新您的 NODE 版本,您有不同的选项,具体取决于您的操作系统。 In case you run Windows or mac on your PC, you can - for instance - go to the official NODE website and download and install the newest release: https://nodejs.org/en/download/current/如果您在 PC 上运行 Windows 或 mac,您可以 - 例如 - 访问 NODE 官方网站并下载并安装最新版本: https ://nodejs.org/en/download/current/

Background cluster.isPrimary returns true if the process is a primary.如果进程是主进程,则后台cluster.isPrimary返回 true。 In contrast, you can use cluster.isWorker to return true if the process is not a primary.相反,如果进程不是主进程,您可以使用cluster.isWorker返回 true。 So it is the negation for cluster.isPrimary .所以这是对cluster.isPrimary的否定。

Many blog posts, articles and tutorials out there are still applying the deprecated cluster.isMaster .许多博客文章、文章和教程仍在应用已弃用的cluster.isMaster This leads to irritations amongst node user dealing first time with NODE clustering.这会导致第一次处理 NODE 集群的节点用户感到恼火。

Official information are provided by NODE here: https://nodejs.org/api/cluster.html#clusterismaster官方信息由 NODE 提供: https ://nodejs.org/api/cluster.html#clusterismaster

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM