master挂了的话pm2怎么处理 使用pm2方便开启node集群模式
本文为转载
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 < numCPUs; ++i) {
cluster.fork();
}
} else {
// Worker:
// Let's spawn a HTTP server
// (Workers can share any TCP connection.
// In this case its a HTTP server)
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world");
}).listen(8080);
}
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 <number of workers> 表示实例程序的个数。就是工作线程。
如果i为0表示,会根据当前CPU核心数创建

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会立即重启他们,当前你可以在任何时候重启,只需:

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 <app name> <n> where <n> 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 <app name> <n> 调整你的线程数量,
如 pm2 scale app +3 会在当前基础上加3个工作线程。

Updating your apps in production with zero downtime
PM2 reload <app name> 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 <app name> 命令会一个接一个的重启工作线程,在新的工作线程启动后才结束老的工作线程。
这种方式可以保持你的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。
作者:飞凡的陀螺
链接:https://www.jianshu.com/p/af789dbd5db8
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
master挂了的话pm2怎么处理 使用pm2方便开启node集群模式的更多相关文章
- Redis 3.2.8 集群模式+Sentinel多Master部署
环境准备CentOS 7.3redis1 172.18.1.101:7001 masterredis2 172.18.1.102:7002 masterredis3 172.18.1.103:7003 ...
- k8s 组件介绍__单Master集群部署
参考链接:https://github.com/opsnull/follow-me-install-kubernetes-cluster kubernetes 概述 1.kubernetes 是什么 ...
- Kubernetes集群搭建之Master配置篇
本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 今天终于到正题了~~ 生成kubernets证书与私钥 1. 制作kubernetes ca证 ...
- 【Spark-core学习之三】 Spark集群搭建 & spark-shell & Master HA
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...
- K8S入门系列之集群二进制部署-->master篇(二)
组件版本和配置策略 组件版本 Kubernetes 1.16.2 Docker 19.03-ce Etcd 3.3.17 https://github.com/etcd-io/etcd/release ...
- RocketMQ多master多salve集群搭建
一.RocketMQ集群模式简介 单Master方式 风险比较大, 一旦Broker重启或者宕机, 将导致整个环境不可用, 不建议线上使用. 多Master模式 一个集群中没有slave, 全是mas ...
- ActiveMQ使用Zookeeper+LevelDb配置Master/Slave集群
前言: 本文介绍的AMQ集群是Master-Slave模式的,官网介绍三种方案: (1)基于共享文件系统的,(2)基于JDBC,(3)基于可复制的LevelDB. 关于三种方式的对比网上已经有很多,本 ...
- 排查 k8s 集群 master 节点无法正常工作的问题
搭建的是 k8s 高可用集群,用了 3 台 master 节点,2 台 master 节点宕机后,仅剩的 1 台无法正常工作. 运行 kubectl get nodes 命令出现下面的错误 The c ...
- Node.js使用PM2的集群将变得更加容易
介绍 众所周知,Node.js运行在Chrome的JavaScript运行时平台上,我们把该平台优雅地称之为V8引擎.不论是V8引擎,还是之后的Node.js,都是以单线程的方式运行的,因此,在多核心 ...
随机推荐
- Centos 7 安装openjdk8
一.使用yum命令搜索支持jdk版本 yum search java|grep jdk 二.使用yum安装jdk8 yum install -y java--openjdk 三.检查是否成功 java ...
- Codeforces 1203F (贪心, DP)
题意:有n个任务,你的初始rating是m, 这n个任务有两个指标:完成这项任务所需的最低rating(a[i]),以及完成这项任务后rating的变化(可能为负)(b[i]).rating不能为负. ...
- java while循环
/* while 循环有一个标准格式,还有一个扩展格式 标准格式: while(条件表达式){ 循环体 } 扩展格式: 初始化语句; while(条件判断){ 循环体 步进表达式 } */ publi ...
- 最近开发的项目,遇到用户上传excel文件并导入数据到系统这个需求,而有excel中有的单元格是日期格式,本文介绍怎么从excel中读取日期格式的数据。
可以先判断单元格的类型,有的日期是字符串存储的,有的是按日期存储的(单元格按数字解析),代码如下: Cell cell = row.getCell(); Date date = null; if (c ...
- 工具类Collections、Arrays(传入的是数组)
Collections类: 1. Collections.sort(list) //对list集合进行排序前提是 list里面存储的对象已经实现了 comparable接口 2. Collecti ...
- Pydiction补全插件
Pydiction不需要安装,所有没有任何依赖包问题,Pydiction主要包含三个文件. python_pydiction.vim -- Vim plugin that autocompletes ...
- Monkeyrunner学习
可以写一个pyhon工程,安装在android进行测试,还可以截屏操作.Monkeyrunner为framework层开发.MonkeyRunner本身是Java做的,为了和Python连接,做了一个 ...
- 前端每日实战:80# 视频演示如何用纯 CSS 创作一个自行车车轮
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XBXEPK 可交互视频 此视频是可 ...
- spring MVC 返回值信息和ResponseBody的响应json数据
spring mvc的界面返回: 如果我们定义的返回类型是String 那么我们返回的时候直接写入 我们的界面的名字就可以了 springmvc会自动去找到我们的界面,如果是void类型的返回那么 ...
- 03 java语言基础逻辑运算符
03.01_Java语言基础(逻辑运算符的基本用法) A:逻辑运算符有哪些 &,|,^,! &&,|| B:案例演示 逻辑运算符的基本用法 注意事项: a:逻辑运算符一般用于连 ...