热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用pm2方便开启node集群模式

这篇文章 写的不错,通过这篇文章你可以了解node集群的原理及如果使用pm2快速方便的开启集群模式。

Introduction

As you would probably know, Node.js is a platform built on Chrome's Javascript runtime, gracefully named V8.
The V8 engine, and hence Node.js, runs in a single-threaded way, therefore, doesn't take advantage of multi-core systems capabilities.

介绍

你应该知道,Node.js是一个运行在名叫V8的Javascript引擎的平台系统。V8本身是单线程运行的,并没有充分利用多核系统能力。
(注:Node执行JS代码运行在V8上,是单线程,但并非真正的单线程架构)

Node.js cluster module

Luckily enough, Node.js offers the cluster module, which basically will spawn some workers which can all share any TCP connection.

How does it work ?

Cluster module will set up a master and then fork your server app as many times as you want it to (also called a worker).
It communicates with workers via IPC channels and comes with an embedded load-balancer which uses Round-robin algorithm to better distribute load among the workers.
When using Round-robin scheduling policy, the master accepts() all incoming connections and sends the TCP handle for that particular connection to the chosen worker (still via IPC).

How to use it ?

The most basic example is the following :

Node.js的集群模式

幸运的是,Node.js提供了集群模块,简单讲就是复制一些可以共享TCP连接的工作线程。

工作原理

集群模块会创建一个master主线程,然后复制任意多份程序并启动,这叫做工作线程。
工作线程通过 IPC 频道进行通信并且使用了 Round-robin algorithm 算法进行工作调度以此实现负载均衡。
Round-robin调度策略主要是master主线程负责接收所有的连接并派发给下面的各个工作线程。

如何使用

下面是一个很常见的例子:

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

var numCPUs = os.cpus().length;

if (cluster.isMaster) {  
  // Master:
  // Let's fork as many workers as you have CPU cores

  for (var i = 0; i 

Of course, you can spawn as many workers as you wish. You're not limited by the CPU cores number since a worker is nothing more but a child process.
As you can see, to make it work, you have to wrap your code inside some cluster handling logic and then add some more code to specify the expected behaviour in case your worker dies unexpectedly.

你可以不受CPU核心限制的创建任意多个工作线程。
使用原生方法有些麻烦而且你还需要处理如果某个工作线程挂掉了等额外的逻辑。
(注:通过fork()复制的进程都是独立的进程,有着全新的V8实例)

The PM2 way
Built-in clustering

PM2 internally handles all of the above logic for you so you don't have to change anything in your code.
The previous code becomes :

PM2的方式
PM2内置了处理上述的逻辑,你不用再写这么多繁琐的代码了。
只需这样一行:
$ pm2 start app.js -i 4
-i 表示实例程序的个数。就是工作线程。
如果i为0表示,会根据当前CPU核心数创建

使用pm2方便开启node集群模式
image.png

Keeping your apps running no matter what

If any of your workers happens to die, PM2 will restart them immediatly so you don't have to worry about that either.
Or, of course, you can, at any time, restart them manually as follows :

保持你的程序不中断运行

如果有任何工作线程意外挂掉了,PM2会立即重启他们,当前你可以在任何时候重启,只需:


使用pm2方便开启node集群模式
image.png

Scaling your cluster in realtime

If you consider that you don't have enough workers or more than needed, you can scale your cluster anytime by hitting pm2 scale where can be a consistent number which the cluster will scale up or down to.
It can also be an addition such as pm2 scale app +3 in which case 3 more workers will be added to the cluster.

实时调整集群数量

你可以使用命令 pm2 scale 调整你的线程数量,
如 pm2 scale app +3 会在当前基础上加3个工作线程。

使用pm2方便开启node集群模式
image.png

Updating your apps in production with zero downtime

PM2 reload feature will restart your workers one by one, and for each worker, wait till the new one has spawned before killing the old one.
This way, your server keeps running even when you are deploying the new patch straight to production.

You can also use gracefulReload feature which does pretty much the same thing but instead of immediatly killing the worker it will send it a shutdown signal via IPC so it can close ongoing connections or perform some custom tasks before exiting gracefully.
Example :

在生产环境让你的程序永不中断

PM2 reload 命令会一个接一个的重启工作线程,在新的工作线程启动后才结束老的工作线程。
这种方式可以保持你的Node程序始终是运行状态。即使在生产环境下部署了新的代码补丁。

也可以使用gracefulReload命令达到同样的目的,它不会立即结束工作线程,而是通过IPC向它发送关闭信号,这样它就可以关闭正在进行的连接,还可以在退出之前执行一些自定义任务。这种方式更优雅。

process.on('message', function(msg) {  
  if (msg === 'shutdown') {
    close_all_connections();
    delete_cache();
    server.close();
    process.exit(0);
  }
});

Conclusion

Cluster module is a powerful tool. It gets even better and easy to use along with PM2.
Cluster.js was experimental on Node 0.10.x and is considered to be mature and production-ready since Node 0.11.x latest releases and of course Node 0.12.x.
We strongly suggest you to always use the latest version of Node.js and PM2 since both of these projects' contributors are working hard every day to make them better.

Enjoy Node.js' clustering with PM2 !

结论

Cluster集群模式非常强悍有用,此功能是在Node 0.10.x 是实验功能,在0.11.x 之后才作为正式发布。
强烈建议你使用最新版本的Node.js和PM2。


推荐阅读
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 高并发网站架构的核心原则其实就一句话“把所有的用户访问请求都尽量往前推“,即:能缓存在用户电脑本地的,就不要让他去访问CDN。能缓存CDN服务器上的,就不要让CDN去访问源(静态服 ... [详细]
  • 方舟等级生成工具_关于方舟编译器的几个小疑问
    之前了解了一下方舟编译器,但是很粗,今天在知乎上看了各种大神关于方舟编译器的说法,其实一直以来就有几个问题,我看了知乎上的说 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • JavaScript简介及语言特点
    本文介绍了JavaScript的起源和发展历程,以及其在前端验证和服务器端开发中的应用。同时,还介绍了ECMAScript标准、DOM对象和BOM对象的作用及特点。最后,对JavaScript作为解释型语言和编译型语言的区别进行了说明。 ... [详细]
  • 每次用到v-charts我都一阵头疼,因为明明是相同的功能,但是我好像每次用到的解决方法都不一样??每次都是在api中各种查,各种尝试…直到做了个各种数据图形的需求,决定还是好好整 ... [详细]
  • SpringBoot与缓存使用及原理(上),Go语言社区,Golang程序员人脉社 ... [详细]
  • 本文整理了Java中java.lang.Iterable.iterator()方法的一些代码示例,展示了Iterable.iterator() ... [详细]
  • 4种分布式Session的实现方式!老大直呼666...
    前言公司有一个Web管理系统,使用Tomcat进行部署。由于是后台管理系统,所有的网页都需要登录授权之后才能进行相应的操作。起初这个系统的用的人也不多& ... [详细]
  • 闲话少说,直接切入主题,之前也是用一下其他的IDE,但是总是在vi和IDE之间来回切换,比较麻烦,于是乎,找了几个插件亲身体验,功能挺不错;1、安装vi7.0vim的官方网站是:www.vmuni ... [详细]
  • 微服务应用性能如何?APM监控工具来告诉你
    当微服务系统越来越庞大,各个服务间的调用关系也变得越来越复杂,需要一个工具来帮忙理清请求调用的服务链路。之前使用的是Sleuth+Zipkin的解决方案,最近发现应 ... [详细]
  • 本文介绍了网页播放视频的三种实现方式,分别是使用html5的video标签、使用flash来播放以及使用object标签。其中,推荐使用html5的video标签来简单播放视频,但有些老的浏览器不支持html5。另外,还可以使用flash来播放视频,需要使用object标签。 ... [详细]
  • 本文讨论了在使用PHP cURL发送POST请求时,请求体在node.js中没有定义的问题。作者尝试了多种解决方案,但仍然无法解决该问题。同时提供了当前PHP代码示例。 ... [详细]
  • 前言:原本纠结于Web模板,选了Handlebars。后来发现页面都是弱逻辑的,不支持复杂逻辑表达式。几乎要放弃之际,想起了Javascript中ev ... [详细]
author-avatar
_韩文清_霸图V
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有