PM2是否可以与Node.js网络API一起使用? [英] Will PM2 work with Node.js net API?

查看:140
本文介绍了PM2是否可以与Node.js网络API一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要制作一个侦听多个TCP连接的服务器应用程序.此应用程序必须轻巧,TCP连接将来自GPS设备(而不是来自浏览器,因此我不能使用WebSockets).

I need to make a server app that listens to several TCP connections. This app must be lightweight and the TCP connections will come from GPS devices ( not from browsers, so I can't use WebSockets for example ).

要确保此方法可以正确扩展到数千个设备,我需要充分利用所有计算机的CPU.根据我的研究,我发现了两种实现方法:

To make sure this scales properly to thousands of devices I need to take full advantage of all the machines CPUs. According to my research I found 2 ways of doing it:

  1. 创建 net 服务器,并使用本机

根据我的理解,这些选项是互斥的.如果选择选项1,则无法使用PM2,反之亦然.

According to my understanding, these options are mutually exclusive. If I choose option 1, I can't use PM2, and vice-versa.

我的团队到处都使用PM2,因此,为了保持一致性,我也希望使用PM2.这里的问题是PM2与Node.js套接字应用程序有关.例如,我知道要使用socket.io,我们需要安装额外的模块(sticky-session),但是由于我使用的是本机API,因此没有任何信息 我需要做什么适应.

My team uses PM2 everywhere, so for consistency sake I would like to use PM2 as well. The problem here is that PM2 has issues with Node.js socket applications. I know for instance that to use socket.io we need to install extra modules ( sticky-session ) but since I am using the native API there is no information on what adaptations I need to do whatsoever.

使用本机net API,我不知道PM2是否会在CPU之间均匀分配连接,也无法找到任何信息,如果时间到了,数据是否将发送给正确的工作人员.

Using the Native net API I have no idea if PM2 will distribute the connections evenly among the CPUs nor can I find any information if the data will go to the right worker when the time comes.

为了演示我的目标,我使用了cluster本机Node.js API制作了一个小型应用程序:

To demonstrate my objective I made a small app using the cluster native Node.js API:

const cluster = require("cluster");
const net = require("net");
const numCPUs = require("os").cpus().length;

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on("exit", (worker, code, signal) => {
        console.log(`worker ${worker.process.pid} died`);
    });

} else {
    // Workers can share any TCP connection
    const server = net.createServer( connection => {
        console.log(`Client connected to ${process.pid}`);
        connection.on( "end", () => console.log( `Client disconnected from ${process.pid}` ) );
        connection.on( "data", data =>console.log(`${process.pid} received ${data.toString("ascii")}`) );
        connection.on( "close", () => console.log(`Client closed connection with ${process.pid}`) );
    } );

    server.listen( 8124, () => console.log(`Worker ${process.pid} Bound`) );

    console.log(`Worker ${process.pid} started`);
}

根据我对文档的了解,此服务器可以平衡所有CPU的负载并重定向连接.

According to what I understand of the documentation, this sever balances load through all the CPUs and redirects the connections as well.

您可以使用telnet尝试以下示例:telnet localhost 8124

You can try this example using telnet: telnet localhost 8124

  1. PM2是否可能出现这种现象?如果是这样(代码是什么样的?)?

推荐答案

答案

所以,这里的答案很棘手...

Answer

So, the answer here is tricky ...

因此,PM2集群模式实际上使用Node.js本机API.这意味着使用Node中的netcluster本机API几乎只是复制PM2已经完成的工作,除非您想以一种截然不同的方式来做.

So, PM2 cluster mode actually uses the Node.js native API. This means that using the net and cluster native APIs from Node would pretty much just be replicating work already done by PM2, unless you want to do it in a radically different way.

PM2将使用cluster功能(使用PM2我称之为分叉"应用程序)正确缩放.

PM2 will scale correctly using the cluster functionality ( what I refer to as "fork" the app using PM2 ) provided that your application is stateless.

如果不是,那么PM2无法保证任何事情.

If it is not, PM2 cannot guarantee anything.

http://pm2.keymetrics.io/docs/usage/cluster-mode/#statelessify-your-application

是的,但是您的应用必须是无状态的.
您需要保存套接字连接以及Redis或MongoDB或其他内容中的套接字连接.

Yes, but your app needs to be stateless.
You need to save your socket connections and what be in Redis or MongoDB or something else.

这篇关于PM2是否可以与Node.js网络API一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆