負載均衡的目的是爲了解決單個節點壓力過大,形成Web服務響應過慢,嚴重的狀況下致使服務癱瘓,沒法正常提供服務。php
假設 基於 本博客內 Node.js+MySQL 服務已經跑在 以下端口,並配置 Nginx 轉發:html
upstream ht-server {
server 0.0.0.0:3004;
server 0.0.0.0:3001;
server 0.0.0.0:3002;
server 0.0.0.0:3003;
}
server
{
listen 80;#監聽端口
#server_name 192.168.1.30;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/pages;#站點目錄
location /ht/ {
proxy_pass http://ht-server/;
}
}
複製代碼
倘若有如下 查詢用戶接口,利用 ab 測試工具 測試 負載均衡 ab -c 1000 -n 20000 http://localhost/ht/usersnode
本地環境測試nodeJS+Express+mysql, 設置鏈接池,4進程 Nginx負載均衡,得出結論:1000併發20000個請求 10.566秒完成 0失敗。mysql
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests
Server Software: nginx/1.6.2
Server Hostname: localhost
Server Port: 80
Document Path: /ht/users
Document Length: 692 bytes
Concurrency Level: 1000
Time taken for tests: 10.566 seconds
Complete requests: 20000
Failed requests: 0
Write errors: 0
Total transferred: 19960000 bytes
HTML transferred: 13840000 bytes
Requests per second: 1892.92 [#/sec] (mean)
Time per request: 528.283 [ms] (mean)
Time per request: 0.528 [ms] (mean, across all concurrent requests)
Transfer rate: 1844.86 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 7 14.0 0 73
Processing: 1 508 377.1 481 1492
Waiting: 0 506 376.9 478 1492
Total: 1 515 376.3 487 1492
Percentage of the requests served within a certain time (ms)
50% 487
66% 677
75% 793
80% 922
90% 1021
95% 1142
98% 1327
99% 1386
100% 1492 (longest request)
複製代碼