[英]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:
这是我考虑过的:
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. 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.