[关闭]
@duanyubin 2016-04-12T03:06:04.000000Z 字数 4168 阅读 289

通过nodejs 学习后端

NODE


谈谈后端

Node.js

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Node.js 基础架构

事件驱动与事件循环

非阻塞I/O

不同介质下的I/O操作耗时

I/O CPU Cycle
L1-cache 3
L2-cache 14
RAM 250
Disk 41000000
Network 240000000

同步阻塞式I/O

阻塞I/O

非阻塞I/O

此处输入图片的描述

优点

解决并发常见的模型

单进程多线程

每个 request 都在单独的线程中处理,即使某一个请求发生很严重的错误也不会影响到其它请求

单线程多进程

通过fork的方法,依据CPU核心数,创建对应的进程数

nodejs 解决单进程问题

1. 多进程监听同一端口

多个进程之间会竞争 accpet 一个连接,产生惊群现象,效率比较低。
由于无法控制一个新的连接由哪个进程来处理,必然导致各 worker 进程之间的负载非常不均衡。

2. 多进程监听不同端口

通过nginx处理负载均衡

  1. http {
  2. upstream cluster {
  3. server 127.0.0.1:3000;
  4. server 127.0.0.1:3001;
  5. server 127.0.0.1:3002;
  6. server 127.0.0.1:3003;
  7. }
  8. server {
  9. listen 80;
  10. server_name www.domain.com;
  11. location / {
  12. proxy_pass http://cluster;
  13. }
  14. }
  15. }

3. cluster

  1. var cluster = require('cluster');
  2. var http = require('http');
  3. var os = require('os');
  4. var numCPUs = os.cpus().length;
  5. if (cluster.isMaster) {
  6. // Master:
  7. // Let's fork as many workers as you have CPU cores
  8. for (var i = 0; i < numCPUs; ++i) {
  9. cluster.fork();
  10. }
  11. } else {
  12. // Worker:
  13. // Let's spawn a HTTP server
  14. // (Workers can share any TCP connection.
  15. // In this case its a HTTP server)
  16. http.createServer(function(req, res) {
  17. res.writeHead(200);
  18. res.end("hello world");
  19. }).listen(8080);
  20. }

4. pm2

  1. pm2 start app.js -i 0

性能测试

测试代码

  1. const http = require('http')
  2. const hostname = '127.0.0.1'
  3. const port = 3000
  4. const server = http.createServer((req, res) => {
  5. res.statusCode = 200
  6. res.setHeader('Content-Type', 'text/plain')
  7. res.end('Hello World\n')
  8. })
  9. server.listen(port, hostname, () => {
  10. console.log(`Server running at http://${hostname}:${port}/`)
  11. })


测试工具

  1. naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/

单进程

  1. naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/
  2. [Mon Apr 11 2016 14:11:43 GMT+0800 (CST)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
  3. [Mon Apr 11 2016 14:11:48 GMT+0800 (CST)] INFO Requests: 6378 (64%), requests per second: 1275, mean latency: 60 ms
  4. [Mon Apr 11 2016 14:11:53 GMT+0800 (CST)] INFO Requests: 8148 (81%), requests per second: 353, mean latency: 340 ms
  5. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO
  6. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Target URL: http://127.0.0.1:3000/
  7. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Max requests: 10000
  8. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Concurrency level: 100
  9. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Agent: none
  10. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO
  11. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Completed requests: 10000
  12. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total errors: 0
  13. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total time: 12.325297339 s
  14. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Requests per second: 811
  15. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Total time: 12.325297339 s
  16. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO
  17. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO Percentage of the requests served within a certain time
  18. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 50% 60 ms
  19. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 90% 83 ms
  20. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 95% 98 ms
  21. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 99% 4052 ms
  22. [Mon Apr 11 2016 14:11:55 GMT+0800 (CST)] INFO 100% 4103 ms (longest request)

多进程

  1. naughtyDog:~ dyb$ loadtest -n 10000 -c 100 http://127.0.0.1:3000/
  2. [Mon Apr 11 2016 14:11:19 GMT+0800 (CST)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
  3. [Mon Apr 11 2016 14:11:24 GMT+0800 (CST)] INFO Requests: 7812 (78%), requests per second: 1560, mean latency: 60 ms
  4. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO
  5. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Target URL: http://127.0.0.1:3000/
  6. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Max requests: 10000
  7. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Concurrency level: 100
  8. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Agent: none
  9. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO
  10. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Completed requests: 10000
  11. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total errors: 0
  12. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total time: 7.234391098 s
  13. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Requests per second: 1382
  14. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Total time: 7.234391098 s
  15. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO
  16. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO Percentage of the requests served within a certain time
  17. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 50% 62 ms
  18. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 90% 85 ms
  19. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 95% 96 ms
  20. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 99% 624 ms
  21. [Mon Apr 11 2016 14:11:26 GMT+0800 (CST)] INFO 100% 627 ms (longest request)

Node.js不适用场景

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注