简体   繁体   English

使用线程的NodeJS .fork()

[英]NodeJS .fork() using threads

I'm needing to run Node servers for the development of several independent projects on a single box. 我需要在单个盒子上运行Node服务器来开发多个独立项目。

With Nginx in front routing based on virtual hosts, running 15+ node instances working separately isn't normally much of a problem. 使用基于虚拟主机的前端路由Nginx时,运行15个以上的node实例分别工作通常不是什么大问题。 The catch is that just starting node that many times eats all my RAM with the overhead of just the internal libraries. 问题是,刚开始的node被很多次占用了我所有的RAM,而只是内部库的开销。

So does there exist a solution for this, allowing me to run several largely-independent Node servers while sharing the core libraries? 那么是否存在针对此问题的解决方案,允许我在共享核心库的同时运行几台很大程度上独立的Node服务器? Here are what I've considered: 这是我考虑过的:

  • Threads instead of processes—there would exist a master control process that would be able to create a new thread for each instance. 线程而不是进程-将存在一个主控制进程,该进程能够为每个实例创建一个新线程。 I know Node is built to not require threads, but is there a way to utilize them in order to save memory? 我知道Node的构建不需要线程,但是有没有一种方法可以利用它们来节省内存呢?
  • Some library or addition to Node allowing me to do the above. 一些库或Node的新增功能允许我执行上述操作。
  • Shared core memory between processes—is this a possibility for Node? 进程之间共享核心内存-Node是否有可能?
  • Just require () ing in each server onto the same Node instance—I can't think how killing or reloading those servers could work. 只需在每台服务器上将require ()输入到同一Node实例上,我想不出如何杀死或重新加载这些服务器。 Is there a way to unrequire and restart an external module? 有没有办法unrequire并重新启动外部模块? This method could be ideal by allowing other libraries (Socket.IO for example) to be shared. 通过允许共享其他库(例如Socket.IO),此方法可能是理想的。

Anyway, this is probably an edge case, so I'm not surprised that a solution isn't obvious. 无论如何,这可能是一个极端的情况,所以对于解决方案并不明显我并不感到惊讶。

Does anyone know of a way to implement this? 有人知道实现此方法的方法吗?

The existing thread libraries limit your access to I/O pretty severely and are intended for you to run your own CPU-bound Javascript code, not serve out data. 现有的线程库非常严格地限制了对I / O的访问,旨在让您运行自己的CPU绑定Javascript代码,而不提供数据。 Shared core is certainly possible , but also a bit of a pain. 共享核心当然是可能的 ,但也有些痛苦。 I think your last option is the most viable. 我认为您最后的选择是最可行的。 You can put together a virtualization system for node pretty easily. 您可以很容易地为节点组合一个虚拟化系统。

Here are a couple standalone Express apps. 这是几个独立的Express应用程序。

app-one.js: app-one.js:

var app = require('express')();

app.get('/', function (req, res) {
  res.send('This is app one.');
});

app.listen(3000);

app-two.js: app-two.js:

var app = require('express')();

app.get('/', function (req, res) {
  res.send('This is app two.');
});

app.listen(3001);

Change those by replacing the listen lines with assignments to the module: 通过用模块分配替换listen线来更改它们:

app-one-virtual.js: app-one-virtual.js:

var app = require('express')();

app.get('/', function (req, res) {
  res.send('This is app one.');
});

module.exports = app;

app-two-virtual.js: app-two-virtual.js:

var app = require('express')();

app.get('/', function (req, res) {
  res.send('This is app two.');
});

module.exports = app;

Then write a master app that requires each of those and delegates requests to them based on the incoming host header: 然后编写一个需要每个应用程序的主应用程序,并根据传入的host头将请求委托给他们:

app-master.js: app-master.js:

var http = require('http');
var appOne = require('./app-one-virtual');
var appTwo = require('./app-two-virtual');

http.createServer(function (req, res) {
  if (req.headers.host === 'one.example.com:3000') {
    return appOne(req, res)
  }
  if (req.headers.host === 'two.example.com:3000') {
    return appTwo(req, res)
  }
  res.writeHead(404)
  res.end('Site not found.')
}).listen(3000);

Now just node app-master.js and you're set. 现在,只需设置node app-master.js Repeat for as many servers as you like. 重复任意多的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM