什么是集群?

Node.js 集群(Cluster)是一种技术,用于在多核系统上创建多个 Node.js 进程,以充分利用系统的所有 CPU 核心,从而提高应用的性能和可用性。Node.js 本身是单线程的,集群模块提供了一种方式,通过使用多个进程来并行处理工作负载。

基本概念

单线程限制:Node.js 是基于单线程的事件驱动模型,默认情况下只能使用一个 CPU 核心。

多进程架构:通过集群模块,可以创建多个 Node.js 进程(称为工作进程),每个进程可以处理部分流量,从而利用多个 CPU 核心。

Nodejs 第八十五章(集群)_前端

案例演示

Node.js 提供了内置的 cluster 模块来创建和管理集群。使用这个模块,可以轻松地创建多个工作进程并共享同一个服务器端口

index.js

import cluster from 'node:cluster'
import http from 'node:http'
import os from 'node:os'
const cpus = os.cpus().length

// 主进程
if (cluster.isPrimary) {
    for (let i = 0; i < cpus; i++) {
        cluster.fork() // 创建子进程
    }
}
// 子进程
else {
   http.createServer((req, res) => {
        res.writeHead(200)
        res.end('cluster is running')
    }).listen(3000,()=>{
        console.log('http://127.0.0.1:3000')
    })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

使用 ps node 查询进程

Nodejs 第八十五章(集群)_多核_02

集群运行跟非集群运行对比

编写一个非集群的服务,index2.js 非集群运行

import http from 'node:http'

http.createServer((req, res) => {
    res.writeHead(200)
    res.end('cluster is running')
}).listen(6000,()=>{
    console.log('http://127.0.0.1:6000')
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

安装测试工具

npm install -g loadtest
  • 1.

执行命令

loadtest http://localhost:3000 -n 50000 -c 100
loadtest http://localhost:6000 -n 50000 -c 100
  • 1.
  • 2.

-c表示并发用户数或并发连接数。在这种情况下,-c 100表示在进行负载测试时,同时模拟100个并发用户或建立100个并发连接

-n表示总请求数或总请求数量。在这种情况下,-n 50000表示在进行负载测试时,将发送总共50000个请求到目标网站

Nodejs 第八十五章(集群)_前端_03

我们可以看到集群的平均延迟是161ms 非集群是 234.7ms ,基于这些测试,可以看到集群并没有给应用程序的性能带来太大的改善