负载均衡想必不用我跟大家解释了吧。其中主要的负载均衡器主要有四种:
-硬件级别:
F5
-软件级别:
Nginx
LVS
HAProxy
Nginx/LVS/HAProxy
是目前使用最广泛的三种负载均衡软件
目前关于网站架构一般比较合理流行的架构方案:Web
前端采用Nginx/HAProxy+Keepalived
作负载均衡器;后端采用MySQL
数据库一主多从和读写分离,采用LVS+Keepalived
的架构。
Nginx常见配置
worker_processes 5;
worker_rlimit_nofile 10000;
events {
use epoll;
worker_connections 10240;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 0;
gzip on;
upstream myapp {
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name myapp;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://myapp;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
压测实验
命令:
这表示并发量为500,然后一共发5000条请求。
ab -c 500 -n 5000 -k http://47.101.xx.xx:80/
结果为:
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 47.101.xx.xx (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/1.14.0
Server Hostname: 47.101.xx.xx
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 500
Time taken for tests: 65.457 seconds
Complete requests: 5000
Failed requests: 8
(Connect: 0, Receive: 0, Length: 8, Exceptions: 0)
Keep-Alive requests: 4992
Total transferred: 4245104 bytes
HTML transferred: 3055104 bytes
Requests per second: 76.39 [#/sec] (mean)
Time per request: 6545.663 [ms] (mean)
Time per request: 13.091 [ms] (mean, across all concurrent requests)
Transfer rate: 63.33 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 103 501.1 0 11813
Processing: 8 3885 7624.5 1072 63399
Waiting: 7 1153 2298.8 449 37516
Total: 8 3989 7708.1 1145 63622
Percentage of the requests served within a certain time (ms)
50% 1145
66% 2322
75% 3880
80% 4997
90% 10773
95% 17623
98% 32100
99% 41291
100% 63622 (longest request)
参考文献
Nginx从听说到学会: https://www.jianshu.com/p/630e2e1ca57f
网友评论