node redis集群

by Michele Riva

由Michele Riva

如何使用集群扩展Node.js服务器 (How to scale your Node.js server using clustering)

Scalability is a hot topic in tech, and every programming language or framework provides its own way of handling high loads of traffic.

可伸缩性是技术上的热门话题,每种编程语言或框架都提供了自己的方式来处理高流量。

Today, we’re going to see an easy and straightforward example about Node.js clustering. This is a programming technique which will help you parallelize your code and speed up performance.

今天,我们将看到一个有关Node.js集群的简单明了的示例。 这是一种编程技术,可帮助您并行执行代码并提高性能。

“A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.”

“ Node.js的单个实例在单个线程中运行。 为了利用多核系统,用户有时会希望启动Node.js进程集群来处理负载。”

- Node.js Documentation

-Node.js文档

We’re gonna create a simple web server using Koa, which is really similar to Express in terms of use.

我们将使用Koa创建一个简单的Web服务器,它在使用方面确实类似于Express 。

The complete example is available in this Github repository.

该Github存储库中提供了完整的示例。

我们要建立的 (What we’re gonna build)

We’ll build a simple web server which will act as follows:

我们将构建一个简单的Web服务器,其作用如下:

  1. Our server will receive a POST request, we’ll pretend that user is sending us a picture.

    我们的服务器将收到POST请求,并假装用户正在向我们发送图片。

  2. We’ll copy an image from the filesystem into a temporary directory.我们将图像从文件系统复制到一个临时目录中。
  3. We’ll flip it vertically using Jimp, an image processing library for Node.js.我们将使用Node.js的图像处理库Jimp垂直翻转它。
  4. We’ll save it to the file system.我们将其保存到文件系统中。
  5. We’ll delete it and we’ll send a response to the user.我们将其删除,并将响应发送给用户。

Of course, this is not a real world application, but is pretty close to one. We just want to measure the benefits of using clustering.

当然,这不是真实的应用程序,但是非常接近一个应用程序。 我们只想衡量使用群集的好处。

设置项目 (Setting up the project)

I’m gonna use yarn to install my dependencies and initialize my project:

我将使用yarn来安装我的依赖项并初始化我的项目:

Since Node.js is single threaded, if our web server crashes, it will remain down until some other process will restarts it. So we’re gonna install forever, a simple daemon which will restart our web server if it ever crashes.

由于Node.js是单线程的,因此如果我们的Web服务器崩溃,它将保持关闭状态,直到其他进程将其重新启动为止。 因此,我们将永久安装一个简单的守护程序,该守护程序将在崩溃时重新启动我们的Web服务器。

We’ll also install Jimp, Koa and Koa Router.

我们还将安装Jimp ,Koa和Koa Router。

Koa入门 (Getting started with Koa)

This is the folder structure we need to create:

这是我们需要创建的文件夹结构:

We’ll have an src folder which contains two JavaScript files: cluster.js and standard.js .

我们将有一个src文件夹,其中包含两个JavaScript文件: cluster.jsstandard.js

The first one will be the file where we’ll experiment with the cluster module. The second is a simple Koa server which will work without any clustering.

第一个将是我们将在cluster模块上进行实验的文件。 第二个是简单的Koa服务器,它将在没有任何群集的情况下运行。

In the module directory, we’re gonna create two files: job.js and log.js.

module目录中,我们将创建两个文件: job.jslog.js

job.js will perform the image manipulation work. log.js will log every event that occurs during that process.

job.js将执行图像处理工作。 log.js将记录该过程中发生的每个事件。

日志模块 (The Log module)

Log module will be a simple function which will take an argument and will write it to the stdout (similar to console.log).

日志模块将是一个简单的函数,它将接受一个参数并将其写入stdout (类似于console.log )。

It will also append the current timestamp at the beginning of the log. This will allow us to check when a process started and to measure its performance.

它还会将当前时间戳附加在日志的开头。 这将使我们能够检查流程何时开始并评估其性能。

作业模块 (The Job module)

I’ll be honest, this is not a beautiful and super-optimized script. It’s just an easy job which will allow us to stress our machine.

老实说,这不是一个漂亮且超级优化的脚本。 这是一项轻松的工作,它将使我们能够对机器施加压力。

Koa Web服务器 (The Koa Webserver)

We’re gonna create a very simple webserver. It will respond on two routes with two different HTTP methods.

我们将创建一个非常简单的Web服务器。 它将使用两种不同的HTTP方法在两条路由上做出响应。

We’ll be able to perform a GET request on http://localhost:3000/. Koa will respond with a simple text which will show us the current PID (process id).

我们将能够在http://localhost:3000/上执行GET请求。 Koa将以简单的文本作为响应,该文本将向我们显示当前的PID(进程ID)。

The second route will only accept POST requests on the /flip path, and will perform the job that we just created.

第二条路由将仅接受/flip路径上的POST请求,并将执行我们刚刚创建的作业。

We’ll also create a simple middleware which will set an X-Response-Time header. This will allow us to measure the performance.

我们还将创建一个简单的中间件,该中间件将设置X-Response-Time标头。 这将使我们能够评估性能。

Great! We can now start our server typing node ./src/standard.js and test our routes.

大! 现在,我们可以启动服务器键入node ./src/standard.js并测试我们的路由。

问题 (The problem)

Let’s use my machine as a server:

让我们将我的机器用作服务器:

  • Macbook Pro 15-inch 2016Macbook Pro 15英寸2016
  • 2.7GHz Intel Core i72.7GHz英特尔酷睿i7
  • 16GB RAM16GB RAM

If I make a POST request, the script above will send me a response in ~3800 milliseconds. Not so bad, given that the image I am currently working on is about 6.7MB.

如果我发出POST请求,上面的脚本将在3800毫秒内给我发送响应。 考虑到我当前正在处理的映像约为6.7MB,还不错。

I can try making more requests, but the response time won’t decrease too much. This is because the requests will be performed sequentially.

我可以尝试发出更多请求,但是响应时间不会减少太多。 这是因为请求将顺序执行。

So, what would happen if I tried to make 10, 100, 1000 concurrent requests?

那么,如果我尝试发出10、100、1000个并发请求,将会发生什么?

I made a simple Elixir script which performs multiple concurrent HTTP requests:

我制作了一个简单的Elixir脚本,该脚本执行多个并发HTTP请求:

I chose Elixir because it’s really easy to create parallel processes, but you can use whatever you prefer!

我选择Elixir是因为创建并行流程非常容易,但是您可以使用自己喜欢的任何东西!

测试十个并发请求-无需集群 (Testing ten concurrent requests — without clustering)

As you can see, we spawn 10 concurrent processes from our iex (an Elixir REPL).

如您所见,我们从iex(Elixir REPL)生成10个并发进程。

The Node.js server will immediately copy our image and start to flip it.The first response will be logged after 16 seconds and the last one after 40 seconds.

Node.js服务器将立即复制我们的图像并开始翻转它。第一个响应将在16秒后记录下来,最后一个响应将在40秒后记录下来。

Such a dramatic performance decrease! With just 10 concurrent requests, we decreased the webserver performance by 950%!

如此惊人的性能下降! 只需10个并发请求, 我们将网络服务器性能降低了950%!

集群介绍 (Introducing clustering)

Remember what I mentioned at the beginning of the article?

还记得我在文章开头提到的内容吗?

To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.

为了利用多核系统,用户有时会希望启动Node.js进程集群来处理负载。

Depending on which server we’re gonna run our Koa application, we could have a different number of cores.

根据我们要运行Koa应用程序的服务器,我们可以具有不同数量的内核。

Every core will be responsible for handling the load individually. Basically, each HTTP request will be satisfied by a single core.

每个核心将负责单独处理负载。 基本上,每个HTTP请求都将由一个内核满足。

So for example — my machine, which has eight cores, will handle eight concurrent requests.

例如,我的机器有八个内核,将处理八个并发请求。

We can now count how many CPUs we have thanks to the os module:

现在,我们可以计算出由于os模块而拥有的CPU数量:

The cpus() method will return an array of objects that describe our CPUs. We can bind its length to a constant which will be called numWorkers, ’cause that’s the number of workers that we’re gonna use.

cpus()方法将返回描述我们CPU的对象数组。 我们可以将其长度绑定到一个常量,该常量将被称为numWorkers ,因为这就是我们要使用的worker数量。

We’re now ready to require the cluster module.

现在,我们已经准备好需要cluster模块了。

We now need a way of splitting our main process into N distinct processes.We’ll call our main process master and the other processes workers.

我们现在需要分裂的方式我们的主要过程分为N不同processes.We'll拨打我们的主要Craft.iomaster和其他进程workers

Node.js cluster module offers a method called isMaster. It will return a boolean value that will tell us if the current process is directed by a worker or master:

Node.js cluster模块提供了一种称为isMaster的方法。 它将返回一个布尔值,该值将告诉我们当前进程是由工作程序还是主程序控制:

Great. The golden rule here is that we don’t want to serve our Koa application under the master process.

大。 这里的黄金法则是,我们不想在主流程下为Koa应用程序提供服务。

We want to create a Koa application for each worker, so when a request comes in, the first free worker will take care of it.

我们想为每个工作人员创建一个Koa应用程序,因此,当有请求进入时,第一个免费工作人员将处理它。

The cluster.fork() method will fit our purpose:

cluster.fork()方法将符合我们的目的:

Ok, at first that may be a little tricky.

好的,起初可能有些棘手。

As you can see in the script above, if our script has been executed by the master process, we’re gonna declare a constant called workers. This will create a worker for each core of our CPU, and will store all the information about them.

正如您在上面的脚本中看到的那样,如果我们的脚本已由主进程执行,我们将声明一个名为workers的常量。 这将为我们CPU的每个核心创建一个工作线程,并将存储有关它们的所有信息。

If you feel unsure about the adopted syntax, using […Array(x)].map() is just the same as:

如果您不确定采用的语法,则使用[…Array(x)].map()与以下方法相同:

I just prefer to use immutable values while developing a high-concurrency app.

我只喜欢在开发高并发应用程序时使用不可变值。

添加Koa (Adding Koa)

As we said before, we don’t want to serve our Koa application under the master process.

如前所述,我们不想在主流程下提供Koa应用程序。

Let’s copy our Koa app structure into the else statement, so we will be sure that it will be served by a worker:

让我们将我们的Koa应用程序结构复制到else语句中,以便确保它可以由工作人员提供:

As you can see, we also added a couple of event listeners in the isMaster statement:

如您所见,我们还在isMaster语句中添加了几个事件侦听器:

The first one will tell us that a new worker has been spawned. The second one will create a new worker when one other worker crashes.

第一个告诉我们,已经产生了一个新工人。 第二个将在其他工人崩溃时创建一个新工人。

That way, the master process will only be responsible for creating new workers and orchestrating them. Every worker will serve an instance of Koa which will be accessible on the :3000 port.

这样,主流程将仅负责创建新工作人员并进行编排。 每个工作人员都会提供一个Koa实例,该实例可以在:3000端口上访问。

测试十个并发请求-使用集群 (Testing ten concurrent requests — with clustering)

As you can see, we got our first response after about 10 seconds, and the last one after about 14 seconds. It’s an amazing improvement over the previous 40 second response time!

如您所见,大约10秒钟后,我们得到了第一个响应,大约14秒钟后,我们得到了最后一个响应。 与之前的40秒响应时间相比,这是一个了不起的改进!

We made ten concurrent requests, and the Koa server took eight of them immediately. When the first worker has sent its response to the client, it took one of the remaining requests and processed it!

我们发出了十个并发请求,而Koa服务器立即接受了其中八个请求。 当第一个工作人员将响应发送给客户端时,它接受了其余请求中的一个并进行了处理!

结论 (Conclusion)

Node.js has an amazing capacity of handling high loads, but it wouldn’t be wise to stop a request until the server finishes its process.

Node.js具有处理高负载的惊人能力,但是在服务器完成其过程之前停止请求不是明智的。

In fact, Node.js webservers can handle thousands of concurrent requests only if you immediately send a response to the client.

实际上,仅当您立即向客户端发送响应时,Node.js Web服务器才能处理数千个并发请求。

A best practice would be to add a pub/sub messaging interface using Redis or any other amazing tool. When the client sends a request, the server starts a realtime communication with other services. This takes charge of expensive jobs.

最佳实践是使用Redis或任何其他出色的工具添加发布/订阅消息传递界面。 当客户端发送请求时,服务器开始与其他服务的实时通信。 这负责昂贵的工作。

Load balancers would also help a lot splitting out high traffic loads.

负载平衡器还有助于将高流量负载分开。

Once again, technology is giving us endless possibilities, and we’re sure to find the right solution to scale our application to infinity and beyond!

再一次,技术为我们提供了无限的可能性,我们一定会找到正确的解决方案,以将我们的应用范围扩展到无限甚至更大!

翻译自: https://www.freecodecamp.org/news/how-to-scale-your-node-js-server-using-clustering-c8d43c656e8f/

node redis集群

node redis集群_如何使用集群扩展Node.js服务器相关推荐

  1. hadoop小型集群_小型Hadoop集群的Ganglia配置和一些故障排除

    hadoop小型集群 Ganglia是一个针对大型集群的开源,可扩展且分布式的监视系统. 它收集,汇总并提供数十种与计算机相关的指标(例如CPU,内存,存储,网络使用情况)的时序视图. 您可以在UC ...

  2. 树莓派 mysql集群_多树莓派集群服务器

    树莓派使用实例之:2 Pi R 第二篇:Web服务器 在我的上一篇文章中讲过如何做一个高可用系统:两个树莓派布署上 GlusterFS 集群文件系统,就变成一个容错文件服务器了.在这篇文章中我们会基于 ...

  3. python集群_使用Python集群文档

    python集群 Natural Language Processing has made huge advancements in the last years. Currently, variou ...

  4. kafka 集群_单机版kafka集群部署

    前言 分布式消息队列是大型分布式系统不可缺少的中间件,主要解决应用耦合.异步消息.流量削锋等问题.实现高性能.高可用.可伸缩和最终一致性架构. 今天跟大家讲解下如何部署单机Kafka集群,希望大家喜欢 ...

  5. 二进制安装mysql集群_实战mysql集群搭建(一)--centos7下二进制安装mysql-5.6

    在超哥的帮助下,完成了基于InnoDb数据引擎的mysql数据库集群搭建,实现了主从复制的功能,本篇博文介绍如何使用二进制安装mysql的方法,具体实现步骤如下: 软件使用说明: Liunx系统:ce ...

  6. php配置mysql集群_【mysql集群】mysql集群配置

    http://topic.csdn.net/u/20100621/16/c9436c17-374c-4c3a-b075-a18670143fd1.html MySql-Cluster安装 系统 cen ...

  7. auot lisp 选择集处理_第64集 python机器学习:用预处理进行参数选择

    在机器学习过程中,大多数机器学习应用不仅需要应该用单个算法,而且还需要将许多不同的处理步骤和机器学习模型链接在一起. 接下来我们将引入本章学习的内容--Pipeline类:该类可以简化构造变换和模型链 ...

  8. electron 打包把node代理服务打包进去_专题:让C++给node做技术加持(三)编译electron本地模块踩坑记

    欢迎来到我的专题文章:<让C++给node做技术加持>系列.更多内容,持续更新中,欢迎关注. 往期目录: 专题:让C++给node做技术加持(一)环境搭建,项目运行 专题:让C++给nod ...

  9. java 自考qq群_学生建QQ群 成绩好的同学作业答案发群共享

    班里建一个QQ群,由成绩好.做作业认真的同学先做完作业,拍张照片发至群里,班上其他同学都来抄答案.家长发现后,觉得不好向老师举报,很是纠结. 据刘女士介绍,儿子小军(化名)在一家中学读初一,最近一段时 ...

最新文章

  1. linux svn missingargument -m,缺少參數-m在windows powershell中使用svn。
  2. 桌面虚拟化之资源分配
  3. android 判断ip地址合法
  4. async-await原理解析
  5. python用于声明类的关键字_python的with关键字
  6. python随机函数random要导入哪个包_python学习之随机函数random
  7. threejs 源码解析_threejs demo
  8. java 杭电3784继续xxx定律 栈的使用
  9. STM32F103xxx大容量增强型芯片之间有什么区别?(来自STM32神舟系列)
  10. python控制窗口_Python : Turtle窗口控制
  11. FireMonkey Control的Paint顺序
  12. ubuntu创建服务程序
  13. 【深度学习】猫狗识别TensorFlow2实验报告
  14. C/C++框架和第三方库
  15. 18年ACM赛后总结
  16. php wps导入数据库,wps excel表格怎么导入数据库-如何把excel表格导入wps
  17. 2019年 支付宝集福攻略
  18. 人工智能实训室解决方案
  19. 使用Redis缓存优化
  20. python输出水仙花数_python打印水仙花数(基础版)

热门文章

  1. Kubernetes-负载均衡器Load Balancer(十八)
  2. 华科计算机考研复试机试【零散大汇总】
  3. 安装fastdfs依赖包
  4. linux-查看文件类型-看本质-file
  5. Ajax 和 PHP 实现验证码的实时验证
  6. ERROR: source database template1 is being accessed by other users解决方法
  7. 构建高并发高可用的电商平台架构实践 转载
  8. [洛谷2357] 守墓人
  9. urllib的实现---请求响应and请求头处理
  10. php使用PHPMailer发送邮件示例