集群
地址:https://coding.imooc.com/lesson/162.html#mid=9011
作用
(一)、提高服务的性能,并发能力以及高可用性
高可用性:nginx下挂多台tomcat,当tomcat1挂掉的时候,就可以把这个节点从tomcat配置集群中摘掉,把nginx达到可用的tomcat机器上,这样就不影响提供的服务,tomcat就能带来一定的高可用性
(二)、提供项目架构的横向扩展能力
实现原理
通过nginx负载均衡进行请求转发
负载均衡的策略和特点
轮询(默认)
优点:实现简单
缺点:不考虑每台服务器处理能力
upstream www.happymmall.com{
server www.happymmall.com:8080;
server www.happymmall.com:9080;
}
权重
优点:考虑了每台服务器处理能力的不同
upstream www.happymmall.com{
server www.happymmall.com:8080 weight=15;
server www.happymmall.com:9080 weight=10;
}
10和15不是具体的值,只是权重,表示8080服务器是9080的1.5倍
ip hash
优点:能实现同一个用户访问同一个服务器
缺点:根据ip hash不一定平均
upstream www.happymmall.com{
ip_hash;
server www.happymmall.com:8080 weight=15;
server www.happymmall.com:9080 weight=10;
}
url hash(第三方)
优点:能实现同一个服务访问同一个服务器
缺点:根据url hash分配请求会不平均,请求频繁的url会请求到同一个服务器
upstream www.happymmall.com{
server www.happymmall.com:8080 weight=15;
server www.happymmall.com:9080 weight=10;
hash $request_url;
}
比如登录和获取商品列表,这是俩个url,可以通过url hash,将url绑定到同一个服务器,之后再请求这个url,还是之前的服务器
使用url hash需要装插件
fair(第三方)
特点:按后端服务器的响应时间来分配请求,响应时间短的优先分配
upstream www.happymmall.com{
server www.happymmall.com:8080 weight=15;
server www.happymmall.com:9080 weight=10;
fair;
}