扩展Node.js [英] Scaling Node.js

查看:98
本文介绍了扩展Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于大规模服务器端开发,我还是一个新手.我想使用Node.js编写服务器,但是在我继续前进之前,我想知道将节点扩展到每秒20个查询的一般原则是什么.

I'm fairly new to large-scale server-side development. I want to write a server using Node.js, but before I forge ahead I'd like to know what the general principles are for scaling node up to, say, 20 queries per second.

我正在编写的服务将主要是数据库的接口,以及输入数据的身份验证和确认.

The service I'm writing will largely be an interface to a database, plus authentication and validation of input data.

推荐答案

负载平衡

对于大多数最简单的网站,您几乎根本不需要扩展.只需一个盒子就能让您覆盖.之后,您应该进行负载平衡,就像您提到的那样,每种架构几乎都是相同的(就像您所说的,您可以先启动多个节点进程.但是,当您变得很大时,您需要更多的盒子).

Load balancing

Most probably for the most simple sites you don't need any scaling at all. Just one single box will get you covered. After that you should do load balancing like you are mentioning which is almost the same for every architecture(like you are saying you could start multiple node processes first. But when you get really big you need more boxes).

Nginx负载平衡示例:

http {
  upstream myproject {
    server 127.0.0.1:8000 weight=3;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;    
    server 127.0.0.1:8003;
  }

  server {
    listen 80;
    server_name www.domain.com;
    location / {
      proxy_pass http://myproject;
    }
  }
}

Redis

每秒20条查询

20 queries per second

node.js不费吹灰之力.您应该使用redis作为您的数据存储,因为它非常快:).当您使用 node_redis 时,甚至还有一个用于节点的c库.

No sweat for node.js. You should use redis as your datastore because it is insane fast :). There is even a c library for node when you use node_redis.

npm install hiredis redis

Hiredis之所以能够为您带来无与伦比的性能,是因为它可以编译为节点内部的C代码.这是与hiredis一起使用时redis的一些基准.

Hiredis is what gives you kickass performance because it compiles to C code inside node. Here are some benchmarks from redis when used with hiredis.

PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287

当您查看这些数字时,则20/s是什么都没有:).

When you look at those numbers then 20/s is NOTHING :).

更新:

openid

我说的很多,但是为了上帝的爱,请不要尝试实现自己的身份验证系统.这可能是不安全的(很多事情可能会出错),很多工作.对于身份验证,您应该使用出色的 connect-auth 图书馆.然后,您就可以放心了,因为他们有专家测试那里的登录系统是否有漏洞,并且也不会通过纯文本传输密码,但是感谢上帝使用https.我还为想要使用

I am telling this a lot but for the love of god please don't try to implement your own authentication-system. It is probably going to be unsafe(a lot can go wrong), a lot of work. For authentication you should use facebook-connect, twitter single sign-in, etc using the excellent connect-auth library. Then you are covered safe because they have experts testing there login-systems for holes and the also don't transmit passwords via plain-text but thank for god use https. I also have answered a topic for a user who wanted to use facebook-connect.

要验证输入,您可以使用 node-validator .

To validate input you could use node-validator.

var check = require('validator').check,
    sanitize = require('validator').sanitize

//Validate
check('test@email.com').len(6, 64).isEmail();       //Methods are chainable
check('abc').isInt();                               //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt();      //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);

//Sanitize / Filter
var int = sanitize('0123').toInt();                  //123
var bool = sanitize('true').toBoolean();             //true
var str = sanitize(' \s\t\r hello \n').trim();      //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a');        //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('&lt;a&gt;').entityDecode();     //'<a>'

还有一个表单库,可帮助您创建表单.

There also is this forms library to help you create forms.

这篇关于扩展Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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