Table of Contents
Basic usage
Home Web Front-end JS Tutorial Let's talk about the Nodejs-cluster module and introduce how to use it.

Let's talk about the Nodejs-cluster module and introduce how to use it.

Dec 13, 2021 pm 06:44 PM
nodejs

This article will take you to understand the Nodejs-cluster module in node, and introduce the usage of the Nodejs-cluster module. I hope it will be helpful to everyone!

Let's talk about the Nodejs-cluster module and introduce how to use it.

The interviewer will sometimes ask you, please tell me nodejsHow to start multi-process, the cluster module should immediately appear in your mind , now let me take you to explore the use of the cluster module.

Basic usage

Node.js runs in a single process by default. For 32-bit systems, up to 512MB of memory can be used, and for 64-bit systems, up to 1GB of memory can be used. For computers with multi-core CPUs, this is very inefficient because only one core is running and the other cores are idle. The cluster module was proposed to solve this problem.

The cluster module allows the establishment of a main process and several worker processes, with the main process monitoring and coordinating the running of the worker processes. Workers use inter-process communication to exchange messages. The cluster module has a built-in load balancer and uses the Round-robin algorithm to coordinate the load between worker processes. When running, all newly established links are completed by the main process, and then the main process assigns the TCP connection to the designated worker process.

var cluster = require('cluster');
var os = require('os');

if (cluster.isMaster){
  for (var i = 0, n = os.cpus().length; i < n; i += 1){
    cluster.fork();
  }
} else {
  http.createServer(function(req, res) {
    res.writeHead(200);
    res.end("hello world\n");
  }).listen(8000);
}
Copy after login

The above code first determines whether the current process is the main process (cluster.isMaster). If so, create several new worker processes according to the number of CPU cores; if not, it means that the current process is a worker process. Then start a server program in this process.

One disadvantage of the above code is that once the work process hangs, the main process cannot know. In order to solve this problem, you can deploy listening functions for online events and exit events in the main process.

var cluster = require(&#39;cluster&#39;);

if(cluster.isMaster) {
  var numWorkers = require(&#39;os&#39;).cpus().length;
  console.log(&#39;Master cluster setting up &#39; + numWorkers + &#39; workers...&#39;);

  for(var i = 0; i < numWorkers; i++) {
    cluster.fork();
  }

  cluster.on(&#39;online&#39;, function(worker) {
    console.log(&#39;Worker &#39; + worker.process.pid + &#39; is online&#39;);
  });

  cluster.on(&#39;exit&#39;, function(worker, code, signal) {
    console.log(&#39;Worker &#39; + worker.process.pid + &#39; died with code: &#39; + code + &#39;, and signal: &#39; + signal);
    console.log(&#39;Starting a new worker&#39;);
    cluster.fork();
  });
}
Copy after login

In the above code, once the main process listens to the exit event of the worker process, it will restart a worker process. Once the worker process is started successfully and can run normally, an online event will be emitted.

worker object

The worker object is the return value of cluster.fork() and represents a worker process.

Its properties and methods are as follows.

(1) worker.id

worker.id returns the unique process number of the current worker. This number is also the index value in cluster.workers that points to the current process.

(2) worker.process

All worker processes are generated using child_process.fork(). The object returned by child_process.fork() is saved in worker.process. Through this attribute, you can get the process object where the worker is located.

(3) worker.send()

This method is used in the main process to send information to the child process.

if (cluster.isMaster) {
  var worker = cluster.fork();
  worker.send(&#39;hi there&#39;);
} else if (cluster.isWorker) {
  process.on(&#39;message&#39;, function(msg) {
    process.send(msg);
  });
}
Copy after login

The function of the above code is that the worker process echoes every message sent by the main process.

In the worker process, to send messages to the main process, use process.send(message); to listen to messages sent by the main process, use the following code.

process.on(&#39;message&#39;, function(message) {
  console.log(message);
});
Copy after login

The message sent can be a string or a JSON object. Below is an example of sending a JSON object.

worker.send({
  type: &#39;task 1&#39;,
  from: &#39;master&#39;,
  data: {
    // the data that you want to transfer
  }
});
Copy after login

cluster.workers object

This object is only available to the main process and includes all worker processes. The key value of each member is a worker process object, and the key name is the worker.id attribute of the worker process.

function eachWorker(callback) {
  for (var id in cluster.workers) {
    callback(cluster.workers[id]);
  }
}
eachWorker(function(worker) {
  worker.send(&#39;big announcement to all workers&#39;);
});
Copy after login

The above code is used to traverse all worker processes.

The data event of the current socket can also use the id attribute to identify the worker process.

socket.on(&#39;data&#39;, function(id) {
  var worker = cluster.workers[id];
});
Copy after login

Cluster module attributes and methods

isMaster, isWorker

isMaster property returns a A Boolean value indicating whether the current process is the main process. This attribute is determined by process.env.NODE_UNIQUE_ID. If process.env.NODE_UNIQUE_ID is undefined, it means that the process is the main process.

The isWorker attribute returns a Boolean value indicating whether the current process is a work process. It is exactly the opposite value of the isMaster attribute.

fork()

The fork method is used to create a new worker process, and the context is copied to the main process. Only the main process can call this method.

This method returns a worker object.

kill()

The kill method is used to terminate the worker process. It can accept a parameter representing a system signal.

If it is currently the main process, it will terminate the contact with worker.process, and then send the system signal method to the worker process. If it is currently a worker process, it will terminate communication with the main process, then exit, returning 0.

In previous versions, this method was also called worker.destroy().

listening event

After the worker process calls the listening method, the "listening" event is transmitted to the server of the process and then to the main process.

The callback function of this event accepts two parameters, one is the current worker object, and the other is the address object, including URL, port, address type (IPv4, IPv6, Unix socket, UDP) and other information. This is useful for Node applications that serve multiple URLs.

Restart Node service without interruption

重启服务需要关闭后再启动,利用cluster模块,可以做到先启动一个worker进程,再把原有的所有work进程关闭。这样就能实现不中断地重启Node服务。

首先,主进程向worker进程发出重启信号。

workers[wid].send({type: &#39;shutdown&#39;, from: &#39;master&#39;});
Copy after login

worker进程监听message事件,一旦发现内容是shutdown,就退出。

process.on(&#39;message&#39;, function(message) {
  if(message.type === &#39;shutdown&#39;) {
    process.exit(0);
  }
});
Copy after login

下面是一个关闭所有worker进程的函数。

function restartWorkers() {
  var wid, workerIds = [];
  for(wid in cluster.workers) {
    workerIds.push(wid);
  }

  workerIds.forEach(function(wid) {
    cluster.workers[wid].send({
      text: &#39;shutdown&#39;,
      from: &#39;master&#39;
     });
    setTimeout(function() {
      if(cluster.workers[wid]) {
        cluster.workers[wid].kill(&#39;SIGKILL&#39;);
      }
    }, 5000);
  });
};
Copy after login

PM2模块

PM2模块是cluster模块的一个包装层。它的作用是尽量将cluster模块抽象掉,让用户像使用单进程一样,部署多进程Node应用。

// app.js
var http = require(&#39;http&#39;);

http.createServer(function(req, res) {
  res.writeHead(200);
  res.end("hello world");
}).listen(8080);
Copy after login

用PM2从命令行启动这段代码

$ pm2 start app.js -i 4
Copy after login

上面代码的i参数告诉PM2,这段代码应该在cluster_mode启动,且新建worker进程的数量是4个。如果i参数的值是0,那么当前机器有几个CPU内核,PM2就会启动几个worker进程。

如果一个worker进程由于某种原因挂掉了,会立刻重启该worker进程。

# 重启所有worker进程
$ pm2 reload all
Copy after login

每个worker进程都有一个id,可以用下面的命令查看单个worker进程的详情。

$ pm2 show <worker id>
Copy after login

关闭worker进程的时候,可以部署下面的代码,让worker进程监听shutdown消息。一旦收到这个消息,进行完毕收尾清理工作再关闭

process.on(&#39;message&#39;, function(msg) {
  if (msg === &#39;shutdown&#39;) {
    close_all_connections();
    delete_logs();
    server.close();
    process.exit(0);
  }
});
Copy after login

更多node相关知识,请访问:nodejs 教程!!

The above is the detailed content of Let's talk about the Nodejs-cluster module and introduce how to use it.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Which one to choose between nodejs and java? Which one to choose between nodejs and java? Apr 21, 2024 am 04:40 AM

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.

See all articles