深入浅出负载均衡

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一、负载均衡简介","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1. 大型网站面临的挑战","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大型网站都要面对庞大的用户量,高并发,海量数据等挑战。为了提升系统整体的性能,可以采用垂直扩展和水平扩展两种方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"垂直扩展","attrs":{}},{"type":"text","text":":在网站发展早期,可以从单机的角度通过增加硬件处理能力,比如 CPU 处理能力,内存容量,磁盘等方面,实现服务器处理能力的提升。但是,单机是有性能瓶颈的,一旦触及瓶颈,再想提升,付出的成本和代价会极高。这显然不能满足大型分布式系统(网站)所有应对的大流量,高并发,海量数据等挑战。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"水平扩展","attrs":{}},{"type":"text","text":":通过集群来分担大型网站的流量。集群中的应用服务器(节点)通常被设计成无状态,用户可以请求任何一个节点,这些节点共同分担访问压力。水平扩展有两个要点:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"应用集群:将同一应用部署到多台机器上,组成处理集群,接收负载均衡设备分发的请求,进行处理,并返回相应数据。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡:将用户访问请求,通过某种算法,分发到集群中的节点。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2. 什么是负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡(Load Balance,简称 LB)是高并发、高可用系统必不可少的关键组件,目标是 尽力将网络流量平均分发到多个服务器上,以提高系统整体的响应速度和可用性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡的主要作用如下:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"高并发:","attrs":{}},{"type":"text","text":"负载均衡通过算法调整负载,尽力均匀的分配应用集群中各节点的工作量,以此提高应用集群的并发处理能力(吞吐量)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"伸缩性:","attrs":{}},{"type":"text","text":"添加或减少服务器数量,然后由负载均衡进行分发控制。这使得应用集群具备伸缩性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"高可用:","attrs":{}},{"type":"text","text":"负载均衡器可以监控候选服务器,当服务器不可用时,自动跳过,将请求分发给可用的服务器。这使得应用集群具备高可用的特性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"安全防护:","attrs":{}},{"type":"text","text":"有些负载均衡软件或硬件提供了安全性功能,如:黑白名单处理、防火墙,防 DDos 攻击等。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二、负载均衡的分类","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"支持负载均衡的技术很多,我们可以通过不同维度去进行分类。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.1 载体维度分类","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从支持负载均衡的载体来看,可以将负载均衡分为两类:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"硬件负载均衡、软件负载均衡","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.1.1硬件负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"硬件负载均衡,一般是在定制处理器上运行的独立负载均衡服务器,价格昂贵,土豪专属。硬件负载均衡的主流产品有:F5 和 A10。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"硬件负载均衡的 优点:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"功能强大:支持全局负载均衡并提供较全面的、复杂的负载均衡算法。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"性能强悍:硬件负载均衡由于是在专用处理器上运行,因此吞吐量大,可支持单机百万以上的并发。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安全性高:往往具备防火墙,防 DDos 攻击等安全功能。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"硬件负载均衡的 缺点:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"成本昂贵:购买和维护硬件负载均衡的成本都很高。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"扩展性差:当访问量突增时,超过限度不能动态扩容。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.1.2 软件负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡,应用最广泛,无论大公司还是小公司都会使用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡从软件层面实现负载均衡,一般可以在任何标准物理设备上运行。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡的 主流产品 有:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Nginx、HAProxy、LVS","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"LVS 可以作为四层负载均衡器。其负载均衡的性能要优于 Nginx。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HAProxy 可以作为 HTTP 和 TCP 负载均衡器。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Nginx、HAProxy 可以作为四层或七层负载均衡器。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡的 优点:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"扩展性好:适应动态变化,可以通过添加软件负载均衡实例,动态扩展到超出初始容量的能力。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"成本低廉:软件负载均衡可以在任何标准物理设备上运行,降低了购买和运维的成本。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡的 缺点:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"性能略差:相比于硬件负载均衡,软件负载均衡的性能要略低一些。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2 网络通信分类","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"软件负载均衡从通信层面来看,又可以分为四层和七层负载均衡。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1) ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"七层负载均衡","attrs":{}},{"type":"text","text":":就是可以根据访问用户的 HTTP 请求头、URL 信息将请求转发到特定的主机。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 重定向","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 重定向","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2) ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"四层负载均衡","attrs":{}},{"type":"text","text":":基于 IP 地址和端口进行请求的转发。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"修改 IP 地址","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"修改 MAC 地址","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2.1 DNS 负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 负载均衡一般用于互联网公司,复杂的业务系统不适合使用。大型网站一般使用 DNS 负载均衡作为 第一级负载均衡手段,然后在内部使用其它方式做第二级负载均衡。DNS 负载均衡属于七层负载均衡。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 即 域名解析服务,是 OSI 第七层网络协议。DNS 被设计为一个树形结构的分布式应用,自上而下依次为:根域名服务器,一级域名服务器,二级域名服务器,... ,本地域名服务器。显然,如果所有数据都存储在根域名服务器,那么 DNS 查询的负载和开销会非常庞大。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,DNS 查询相对于 DNS 层级结构,是一个逆向的递归流程,DNS 客户端依次请求本地 DNS 服务器,上一级 DNS 服务器,上上一级 DNS 服务器,... ,根 DNS 服务器(又叫权威 DNS 服务器),一旦命中,立即返回。为了减少查询次数,每一级 DNS 服务器都会设置 DNS 查询缓存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 负载均衡的工作原理就是:基于 DNS 查询缓存,按照负载情况返回不同服务器的 IP 地址。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/43/436f20a02681c457d94088c5b52d8c70.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 重定向的 优点:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用简单:负载均衡工作,交给 DNS 服务器处理,省掉了负载均衡服务器维护的麻烦","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"提高性能:可以支持基于地址的域名解析,解析成距离用户最近的服务器地址(类似 CDN 的原理),可以加快访问速度,改善性能;","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS 重定向的 缺点:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可用性差:DNS 解析是多级解析,新增/修改 DNS 后,解析时间较长;解析过程中,用户访问网站将失败;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"扩展性低:DNS 负载均衡的控制权在域名商那里,无法对其做更多的改善和扩展;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"维护性差:也不能反映服务器的当前运行状态;支持的算法少;不能区分服务器的差异(不能根据系统与服务的状态来判断负载)。","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2.2 HTTP 负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 负载均衡是基于 HTTP 重定向实现的。HTTP 负载均衡属于七层负载均衡。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 重定向原理是:根据用户的 HTTP 请求计算出一个真实的服务器地址,将该服务器地址写入 HTTP 重定向响应中,返回给浏览器,由浏览器重新进行访问。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4f/4f2d1678bd2b93ee4d41964348ac5c45.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 重定向的优点:方案简单。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 重定向的 缺点:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"性能较差:每次访问需要两次请求服务器,增加了访问的延迟。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"降低搜索排名:使用重定向后,搜索引擎会视为 SEO 作弊。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果负载均衡器宕机,就无法访问该站点。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于其缺点比较明显,所以这种负载均衡策略实际应用较少。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2.3 反向代理负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理(Reverse Proxy)方式是指以 代理服务器 来接受网络请求,然后 将请求转发给内网中的服务器,并将从内网中的服务器上得到的结果返回给网络请求的客户端。反向代理负载均衡属于七层负载均衡。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理服务的主流产品:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Nginx、Apache","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正向代理与反向代理有什么区别?","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"正向代理:发生在 客户端,是由用户主动发起的。翻墙软件就是典型的正向代理,客户端通过主动访问代理服务器,让代理服务器获得需要的外网数据,然后转发回客户端。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理:发生在 服务端,用户不知道代理的存在。","attrs":{}}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1a/1a604ba54f4121f7ed625e5d124cfe35.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理是如何实现负载均衡的呢?以 Nginx 为例,如下所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/50/50aae959161035ed1ed6404a6bf573da.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,在代理服务器上设定好负载均衡规则。然后,当收到客户端请求,反向代理服务器拦截指定的域名或 IP 请求,根据负载均衡算法,将请求分发到候选服务器上。其次,如果某台候选服务器宕机,反向代理服务器会有容错处理,比如分发请求失败 3 次以上,将请求分发到其他候选服务器上。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理的 优点:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1) 多种负载均衡算法:支持多种负载均衡算法,以应对不同的场景需求。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2) 可以监控服务器:基于 HTTP 协议,可以监控转发服务器的状态,如:系统负载、响应时间、是否可用、连接数、流量等,从而根据这些数据调整负载均衡的策略。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理的 缺点:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1) 额外的转发开销:反向代理的转发操作本身是有性能开销的,可能会包括创建连接,等待连接响应,分析响应结果等操作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2) 增加系统复杂度:反向代理常用于做分布式应用的水平扩展,但反向代理服务存在以下问题,为了解决以下问题会给系统整体增加额外的复杂度和运维成本:","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"反向代理服务如果自身宕机,就无法访问站点,所以需要有 高可用 方案,常见的方案有:主备模式(一主一备)、双主模式(互为主备)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"},"content":[{"type":"text","text":"反向代理服务自身也存在性能瓶颈,随着需要转发的请求量不断攀升,需要有 可扩展 方案。","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2.4 IP负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IP 负载均衡是在网络层通过修改请求目的地址进行负载均衡。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e6b307e7492c6b7ee3149fbccb8d8c1e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上图所示,IP 均衡处理流程大致为:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客户端请求 192.168.137.10,由负载均衡服务器接收到报文。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡服务器根据算法选出一个服务节点 192.168.0.1,然后将报文请求地址改为该节点的 IP。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"真实服务节点收到请求报文,处理后,返回响应数据到负载均衡服务器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡服务器将响应数据的源地址改负载均衡服务器地址,返回给客户端。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IP 负载均衡在内核进程完成数据分发,较反向代理负载均衡有更好的从处理性能。但是,由于所有请求响应都要经过负载均衡服务器,集群的吞吐量受制于负载均衡服务器的带宽。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"2.2.5 数据链路层负载均衡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"数据链路层负载均衡是指在通信协议的数据链路层修改 mac 地址进行负载均衡。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/28/28e74446bf201c13805d2687e92de38c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Linux 平台上最好的链路层负载均衡开源产品是 LVS (Linux Virtual Server)。LVS 是基于 Linux 内核中 netfilter 框架实现的负载均衡系统。netfilter 是内核态的 Linux 防火墙机制,可以在数据包流经过程中,根据规则设置若干个关卡(hook 函数)来执行相关的操作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"LVS 的工作流程大致如下:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当用户访问 www.sina.com.cn 时,用户数据通过层层网络,最后通过交换机进入 LVS 服务器网卡,并进入内核网络层。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"进入 PREROUTING 后经过路由查找,确定访问的目的 VIP 是本机 IP 地址,所以数据包进入到 INPUT 链上;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IPVS 是工作在 INPUT 链上,会根据访问的 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"vip+port","attrs":{}},{"type":"text","text":" 判断请求是否 IPVS 服务,如果是则调用注册的 IPVS HOOK 函数,进行 IPVS 相关主流程,强行修改数据包的相关数据,并将数据包发往 POSTROUTING 链上;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"POSTROUTING 上收到数据包后,根据目标 IP 地址(后端服务器),通过路由选路,将数据包最终发往后端的服务器上。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"开源 LVS 版本有 3 种工作模式,每种模式工作原理截然不同,说各种模式都有自己的优缺点,分别适合不同的应用场景,不过最终本质的功能都是能实现均衡的流量调度和良好的扩展性。主要包括三种模式:DR 模式、NAT 模式、Tunnel 模式。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、负载均衡算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡器的实现可以分为两个部分:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据负载均衡算法在候选服务器列表选出一个服务器;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":"br"}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"将请求数据发送到该服务器上。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡算法是负载均衡服务核心中的核心。负载均衡产品多种多样,但是各种负载均衡算法原理是共性的。负载均衡算法有很多种,分别适用于不同的应用场景,本文仅介绍最为常见的负载均衡算法的特性及原理:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"轮询、随机、最小活跃数、源地址哈希、一致性哈希","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注:","attrs":{}},{"type":"text","text":"负载均衡算法的实现,推荐阅读 ","attrs":{}},{"type":"link","attrs":{"href":"https://dubbo.apache.org/zh/docs/v2.7/dev/source/loadbalance/","title":null,"type":null},"content":[{"type":"text","text":"Dubbo 官方负载均衡算法说明","attrs":{}}]},{"type":"text","text":" ,源码讲解非常详细,非常值得借鉴。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1 随机","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.1.1 随机算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"随机(Random)","attrs":{}},{"type":"text","text":" 算法将请求随机分发到候选服务器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"随机算法 适合服务器硬件相同的场景。学习过概率论的都知道,调用量较小的时候,可能负载并不均匀,调用量越大,负载越均衡。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/db/db8a87707531558572c5dbf982774882.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】随机算法实现示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡接口","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface LoadBalance {\n\n N select(List nodes, String ip);\n\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"负载均衡抽象类","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class BaseLoadBalance implements LoadBalance {\n\n @Override\n public N select(List nodes, String ip) {\n if (CollectionUtil.isEmpty(nodes)) {\n return null;\n }\n\n // 如果 nodes 列表中仅有一个 node,直接返回即可,无需进行负载均衡\n if (nodes.size() == 1) {\n return nodes.get(0);\n }\n\n return doSelect(nodes, ip);\n }\n\n protected abstract N doSelect(List nodes, String ip);\n\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服务器节点类","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Node implements Comparable {\n\n protected String url;\n\n protected Integer weight;\n\n protected Integer active;\n\n // ...\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"随机算法实现","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class RandomLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n private final Random random = new Random();\n\n @Override\n protected N doSelect(List nodes, String ip) {\n // 在列表中随机选取一个节点\n int index = random.nextInt(nodes.size());\n return nodes.get(index);\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.1.2 加权随机算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"加权随机(Weighted Random","attrs":{}},{"type":"text","text":") 算法在随机算法的基础上,按照概率调整权重,进行负载分配。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】加权随机算法实现示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class WeightRandomLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n private final Random random = ThreadLocalRandom.current();\n\n @Override\n protected N doSelect(List nodes, String ip) {\n\n int length = nodes.size();\n AtomicInteger totalWeight = new AtomicInteger(0);\n for (N node : nodes) {\n Integer weight = node.getWeight();\n totalWeight.getAndAdd(weight);\n }\n\n if (totalWeight.get() > 0) {\n int offset = random.nextInt(totalWeight.get());\n for (N node : nodes) {\n // 让随机值 offset 减去权重值\n offset -= node.getWeight();\n if (offset < 0) {\n // 返回相应的 Node\n return node;\n }\n }\n }\n\n // 直接随机返回一个\n return nodes.get(random.nextInt(length));\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2 轮询","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.2.1 轮询算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"轮询(Round Robin)","attrs":{}},{"type":"text","text":"算法的策略是:将请求依次分发到候选服务器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如下图所示,负载均衡器收到来自客户端的 6 个请求,(1, 3, 5) 的请求会被发送到服务器 1,(2, 4, 6) 的请求会被发送到服务器 2。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c19e290329384726b3f3379f6eec250d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"该算法适合场景:各服务器处理能力相近,且每个事务工作量差异不大。如果存在较大差异,那么处理较慢的服务器就可能会积压请求,最终无法承担过大的负载。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/33/3332a6f9a33847df3e7bf08cc55ccbb2.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】轮询算法示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"轮询负载均衡算法实现","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class RoundRobinLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n private final AtomicInteger position = new AtomicInteger(0);\n\n @Override\n protected N doSelect(List nodes, String ip) {\n int length = nodes.size();\n // 如果位置值已经等于节点数,重置为 0\n position.compareAndSet(length, 0);\n N node = nodes.get(position.get());\n position.getAndIncrement();\n return node;\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"3.2.2 加权轮询算法","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"加权轮询(Weighted Round Robbin)","attrs":{}},{"type":"text","text":"算法在轮询算法的基础上,增加了权重属性来调节转发服务器的请求数目。性能高、处理速度快的节点应该设置更高的权重,使得分发时优先将请求分发到权重较高的节点上。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如下图所示,服务器 A 设置权重为 5,服务器 B 设置权重为 1,负载均衡器收到来自客户端的 6 个请求,那么 (1, 2, 3, 4, 5) 请求会被发送到服务器 A,(6) 请求会被发送到服务器 B。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/07/07072e320d6d5d2ca38b2942b71e465a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】加权轮询算法实现示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下实现基于 Dubbo 加权轮询算法做了一些简化。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class WeightRoundRobinLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n /**\n * 60秒\n */\n private static final int RECYCLE_PERIOD = 60000;\n\n /**\n * Node hashcode 到 WeightedRoundRobin 的映射关系\n */\n private ConcurrentMap weightMap = new ConcurrentHashMap<>();\n\n /**\n * 原子更新锁\n */\n private AtomicBoolean updateLock = new AtomicBoolean();\n\n @Override\n protected N doSelect(List nodes, String ip) {\n\n int totalWeight = 0;\n long maxCurrent = Long.MIN_VALUE;\n\n // 获取当前时间\n long now = System.currentTimeMillis();\n N selectedNode = null;\n WeightedRoundRobin selectedWRR = null;\n\n // 下面这个循环主要做了这样几件事情:\n // 1. 遍历 Node 列表,检测当前 Node 是否有相应的 WeightedRoundRobin,没有则创建\n // 2. 检测 Node 权重是否发生了变化,若变化了,则更新 WeightedRoundRobin 的 weight 字段\n // 3. 让 current 字段加上自身权重,等价于 current += weight\n // 4. 设置 lastUpdate 字段,即 lastUpdate = now\n // 5. 寻找具有最大 current 的 Node,以及 Node 对应的 WeightedRoundRobin,\n // 暂存起来,留作后用\n // 6. 计算权重总和\n for (N node : nodes) {\n int hashCode = node.hashCode();\n WeightedRoundRobin weightedRoundRobin = weightMap.get(hashCode);\n int weight = node.getWeight();\n if (weight < 0) {\n weight = 0;\n }\n\n // 检测当前 Node 是否有对应的 WeightedRoundRobin,没有则创建\n if (weightedRoundRobin == null) {\n weightedRoundRobin = new WeightedRoundRobin();\n // 设置 Node 权重\n weightedRoundRobin.setWeight(weight);\n // 存储 url 唯一标识 identifyString 到 weightedRoundRobin 的映射关系\n weightMap.putIfAbsent(hashCode, weightedRoundRobin);\n weightedRoundRobin = weightMap.get(hashCode);\n }\n // Node 权重不等于 WeightedRoundRobin 中保存的权重,说明权重变化了,此时进行更新\n if (weight != weightedRoundRobin.getWeight()) {\n weightedRoundRobin.setWeight(weight);\n }\n\n // 让 current 加上自身权重,等价于 current += weight\n long current = weightedRoundRobin.increaseCurrent();\n // 设置 lastUpdate,表示近期更新过\n weightedRoundRobin.setLastUpdate(now);\n // 找出最大的 current\n if (current > maxCurrent) {\n maxCurrent = current;\n // 将具有最大 current 权重的 Node 赋值给 selectedNode\n selectedNode = node;\n // 将 Node 对应的 weightedRoundRobin 赋值给 selectedWRR,留作后用\n selectedWRR = weightedRoundRobin;\n }\n\n // 计算权重总和\n totalWeight += weight;\n }\n\n // 对 weightMap 进行检查,过滤掉长时间未被更新的节点。\n // 该节点可能挂了,nodes 中不包含该节点,所以该节点的 lastUpdate 长时间无法被更新。\n // 若未更新时长超过阈值后,就会被移除掉,默认阈值为60秒。\n if (!updateLock.get() && nodes.size() != weightMap.size()) {\n if (updateLock.compareAndSet(false, true)) {\n try {\n // 遍历修改,即移除过期记录\n weightMap.entrySet().removeIf(item -> now - item.getValue().getLastUpdate() > RECYCLE_PERIOD);\n } finally {\n updateLock.set(false);\n }\n }\n }\n\n if (selectedNode != null) {\n // 让 current 减去权重总和,等价于 current -= totalWeight\n selectedWRR.decreaseCurrent(totalWeight);\n // 返回具有最大 current 的 Node\n return selectedNode;\n }\n\n // should not happen here\n return nodes.get(0);\n }\n\n protected static class WeightedRoundRobin {\n\n // 服务提供者权重\n private int weight;\n // 当前权重\n private AtomicLong current = new AtomicLong(0);\n // 最后一次更新时间\n private long lastUpdate;\n\n public long increaseCurrent() {\n // current = current + weight;\n return current.addAndGet(weight);\n }\n\n public long decreaseCurrent(int total) {\n // current = current - total;\n return current.addAndGet(-1 * total);\n }\n\n public int getWeight() {\n return weight;\n }\n\n public void setWeight(int weight) {\n this.weight = weight;\n // 初始情况下,current = 0\n current.set(0);\n }\n\n public AtomicLong getCurrent() {\n return current;\n }\n\n public void setCurrent(AtomicLong current) {\n this.current = current;\n }\n\n public long getLastUpdate() {\n return lastUpdate;\n }\n\n public void setLastUpdate(long lastUpdate) {\n this.lastUpdate = lastUpdate;\n }\n\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3 最小活跃数","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"最小活跃数(Least Active)","attrs":{}},{"type":"text","text":"算法 将请求分发到连接数/请求数最少的候选服务器(目前处理请求最少的服务器)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"特点:根据候选服务器当前的请求连接数,动态分配。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"场景:适用于对系统负载较为敏感或请求连接时长相差较大的场景。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于每个请求的连接时长不一样,如果采用简单的轮循或随机算法,都可能出现某些服务器当前连接数过大,而另一些服务器的连接过小的情况,这就造成了负载并非真正均衡。虽然,轮询或算法都可以通过加权重属性的方式进行负载调整,但加权方式难以应对动态变化。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如下图中,(1, 3, 5) 请求会被发送到服务器 1,但是 (1, 3) 很快就断开连接,此时只有 (5) 请求连接服务器 1;(2, 4, 6) 请求被发送到服务器 2,只有 (2) 的连接断开。该系统继续运行时,服务器 2 会承担过大的负载。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/42/421e8bff0463932aba2b1ed14ee51087.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最小活跃数算法会记录当前时刻,每个候选节点正在处理的连接数,然后选择连接数最小的节点。该策略能够动态、实时地反应服务器的当前状况,较为合理地将负责分配均匀,适用于对当前系统负载较为敏感的场景。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如下图中,服务器 1 当前连接数最小,那么新到来的请求 6 就会被发送到服务器 1 上。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e0/e0303e8eaad543889d0ebd86a1821a6d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"加权最小活跃数(Weighted Least Connection)","attrs":{}},{"type":"text","text":"在最小活跃数的基础上,根据服务器的性能为每台服务器分配权重,再根据权重计算出每台服务器能处理的连接数。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最小活跃数算法实现要点:活跃调用数越小,表明该服务节点处理能力越高,单位时间内可处理更多的请求,应优先将请求分发给该服务。在具体实现中,每个服务节点对应一个活跃数 active。初始情况下,所有服务提供者活跃数均为 0。每收到一个请求,活跃数加 1,完成请求后则将活跃数减 1。在服务运行一段时间后,性能好的服务提供者处理请求的速度更快,因此活跃数下降的也越快,此时这样的服务提供者能够优先获取到新的服务请求、这就是最小活跃数负载均衡算法的基本思想。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】最小活跃数算法实现","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下实现基于 Dubbo 最小活跃数负载均衡算法做了些许改动。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class LeastActiveLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n private final Random random = new Random();\n\n @Override\n protected N doSelect(List nodes, String ip) {\n int length = nodes.size();\n // 最小的活跃数\n int leastActive = -1;\n // 具有相同“最小活跃数”的服务者提供者(以下用 Node 代称)数量\n int leastCount = 0;\n // leastIndexs 用于记录具有相同“最小活跃数”的 Node 在 nodes 列表中的下标信息\n int[] leastIndexs = new int[length];\n int totalWeight = 0;\n // 第一个最小活跃数的 Node 权重值,用于与其他具有相同最小活跃数的 Node 的权重进行对比,\n // 以检测是否“所有具有相同最小活跃数的 Node 的权重”均相等\n int firstWeight = 0;\n boolean sameWeight = true;\n\n // 遍历 nodes 列表\n for (int i = 0; i < length; i++) {\n N node = nodes.get(i);\n // 发现更小的活跃数,重新开始\n if (leastActive == -1 || node.getActive() < leastActive) {\n // 使用当前活跃数更新最小活跃数 leastActive\n leastActive = node.getActive();\n // 更新 leastCount 为 1\n leastCount = 1;\n // 记录当前下标值到 leastIndexs 中\n leastIndexs[0] = i;\n totalWeight = node.getWeight();\n firstWeight = node.getWeight();\n sameWeight = true;\n\n // 当前 Node 的活跃数 node.getActive() 与最小活跃数 leastActive 相同\n } else if (node.getActive() == leastActive) {\n // 在 leastIndexs 中记录下当前 Node 在 nodes 集合中的下标\n leastIndexs[leastCount++] = i;\n // 累加权重\n totalWeight += node.getWeight();\n // 检测当前 Node 的权重与 firstWeight 是否相等,\n // 不相等则将 sameWeight 置为 false\n if (sameWeight && i > 0\n && node.getWeight() != firstWeight) {\n sameWeight = false;\n }\n }\n }\n\n // 当只有一个 Node 具有最小活跃数,此时直接返回该 Node 即可\n if (leastCount == 1) {\n return nodes.get(leastIndexs[0]);\n }\n\n // 有多个 Node 具有相同的最小活跃数,但它们之间的权重不同\n if (!sameWeight && totalWeight > 0) {\n // 随机生成一个 [0, totalWeight) 之间的数字\n int offsetWeight = random.nextInt(totalWeight);\n // 循环让随机数减去具有最小活跃数的 Node 的权重值,\n // 当 offset 小于等于0时,返回相应的 Node\n for (int i = 0; i < leastCount; i++) {\n int leastIndex = leastIndexs[i];\n // 获取权重值,并让随机数减去权重值\n offsetWeight -= nodes.get(leastIndex).getWeight();\n if (offsetWeight <= 0) {\n return nodes.get(leastIndex);\n }\n }\n }\n // 如果权重相同或权重为0时,随机返回一个 Node\n return nodes.get(leastIndexs[random.nextInt(leastCount)]);\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.4 源地址哈希","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源地址哈希(IP Hash)","attrs":{}},{"type":"text","text":"算法 根据请求源 IP,通过哈希计算得到一个数值,用该数值在候选服务器列表的进行取模运算,得到的结果便是选中的服务器。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以保证同一 IP 的客户端的请求会转发到同一台服务器上,用来实现会话粘滞(Sticky Session)。","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"特点:保证特定用户总是请求到相同的服务器,若服务器宕机,会话会丢失。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】源地址哈希算法实现示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class IpHashLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n @Override\n protected N doSelect(List nodes, String ip) {\n if (StrUtil.isBlank(ip)) {\n ip = \"127.0.0.1\";\n }\n\n int length = nodes.size();\n int index = hash(ip) % length;\n return nodes.get(index);\n }\n\n public int hash(String text) {\n return HashUtil.fnvHash(text);\n }\n\n}","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.5 一致性哈希","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一致性哈希(Consistent Hash)算法的目标是:相同的请求尽可能落到同一个服务器上。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一致性哈希 可以很好的解决 稳定性问题,可以将所有的 存储节点 排列在 首尾相接 的 Hash 环上,每个 key 在计算 Hash 后会 顺时针 找到 临接 的 存储节点 存放。而当有节点 加入 或 退出 时,仅影响该节点在 Hash环上顺时针相邻的后续节点。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/03/0395dd03dd099cb3e06e5a644c6a9b7c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1)相同的请求是指:一般在使用一致性哈希时,需要指定一个 key 用于 hash 计算,可能是:","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用户 ID","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"请求方 IP","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"请求服务名称,参数列表构成的串","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2)尽可能是指:服务器可能发生上下线,少数服务器的变化不应该影响大多数的请求。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当某台候选服务器宕机时,原本发往该服务器的请求,会基于虚拟节点,平摊到其它候选服务器,不会引起剧烈变动。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"优点:","attrs":{}},{"type":"text","text":"加入 和 删除 节点只影响 哈希环 中 顺时针方向 的 相邻的节点,对其他节点无影响。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"缺点:","attrs":{}},{"type":"text","text":"加减节点 会造成 哈希环 中部分数据 无法命中。当使用 少量节点 时,节点变化 将大范围影响 哈希","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"环 中 数据映射,不适合 少量数据节点 的分布式方案。普通 的 一致性哈希分区 在增减节点时需要 增加一倍 或 减去一半 节点才能保证 数据 和 负载的均衡。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意:","attrs":{}},{"type":"text","text":"因为 一致性哈希分区 的这些缺点,一些分布式系统采用 虚拟槽 对 一致性哈希 进行改进,比如 Dynamo 系统。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【示例】一致性哈希算法示例","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class ConsistentHashLoadBalance extends BaseLoadBalance implements LoadBalance {\n\n private final ConcurrentMap> selectors = new ConcurrentHashMap<>();\n\n @SuppressWarnings(\"unchecked\")\n @Override\n protected N doSelect(List nodes, String ip) {\n // 分片数,这里设为节点数的 4 倍\n Integer replicaNum = nodes.size() * 4;\n // 获取 nodes 原始的 hashcode\n int identityHashCode = System.identityHashCode(nodes);\n\n // 如果 nodes 是一个新的 List 对象,意味着节点数量发生了变化\n // 此时 selector.identityHashCode != identityHashCode 条件成立\n ConsistentHashSelector selector = (ConsistentHashSelector) selectors.get(ip);\n if (selector == null || selector.identityHashCode != identityHashCode) {\n // 创建新的 ConsistentHashSelector\n selectors.put(ip, new ConsistentHashSelector<>(nodes, identityHashCode, replicaNum));\n selector = (ConsistentHashSelector) selectors.get(ip);\n }\n // 调用 ConsistentHashSelector 的 select 方法选择 Node\n return selector.select(ip);\n }\n\n /**\n * 一致性哈希选择器\n */\n private static final class ConsistentHashSelector {\n\n /**\n * 存储虚拟节点\n */\n private final TreeMap virtualNodes;\n\n private final int identityHashCode;\n\n /**\n * 构造器\n *\n * @param nodes 节点列表\n * @param identityHashCode hashcode\n * @param replicaNum 分片数\n */\n ConsistentHashSelector(List nodes, int identityHashCode, Integer replicaNum) {\n this.virtualNodes = new TreeMap<>();\n this.identityHashCode = identityHashCode;\n // 获取虚拟节点数,默认为 100\n if (replicaNum == null) {\n replicaNum = 100;\n }\n for (N node : nodes) {\n for (int i = 0; i < replicaNum / 4; i++) {\n // 对 url 进行 md5 运算,得到一个长度为16的字节数组\n byte[] digest = md5(node.getUrl());\n // 对 digest 部分字节进行 4 次 hash 运算,得到四个不同的 long 型正整数\n for (int j = 0; j < 4; j++) {\n // h = 0 时,取 digest 中下标为 0 ~ 3 的4个字节进行位运算\n // h = 1 时,取 digest 中下标为 4 ~ 7 的4个字节进行位运算\n // h = 2, h = 3 时过程同上\n long m = hash(digest, j);\n // 将 hash 到 node 的映射关系存储到 virtualNodes 中,\n // virtualNodes 需要提供高效的查询操作,因此选用 TreeMap 作为存储结构\n virtualNodes.put(m, node);\n }\n }\n }\n }\n\n public N select(String key) {\n // 对参数 key 进行 md5 运算\n byte[] digest = md5(key);\n // 取 digest 数组的前四个字节进行 hash 运算,再将 hash 值传给 selectForKey 方法,\n // 寻找合适的 Node\n return selectForKey(hash(digest, 0));\n }\n\n private N selectForKey(long hash) {\n // 查找第一个大于或等于当前 hash 的节点\n Map.Entry entry = virtualNodes.ceilingEntry(hash);\n // 如果 hash 大于 Node 在哈希环上最大的位置,此时 entry = null,\n // 需要将 TreeMap 的头节点赋值给 entry\n if (entry == null) {\n entry = virtualNodes.firstEntry();\n }\n // 返回 Node\n return entry.getValue();\n }\n\n }\n\n /**\n * 计算 hash 值\n */\n public static long hash(byte[] digest, int number) {\n return (((long) (digest[3 + number * 4] & 0xFF) << 24)\n | ((long) (digest[2 + number * 4] & 0xFF) << 16)\n | ((long) (digest[1 + number * 4] & 0xFF) << 8)\n | (digest[number * 4] & 0xFF))\n & 0xFFFFFFFFL;\n }\n\n /**\n * 计算 MD5 值\n */\n public static byte[] md5(String value) {\n MessageDigest md5;\n try {\n md5 = MessageDigest.getInstance(\"MD5\");\n } catch (NoSuchAlgorithmException e) {\n throw new IllegalStateException(e.getMessage(), e);\n }\n md5.reset();\n byte[] bytes = value.getBytes(StandardCharsets.UTF_8);\n md5.update(bytes);\n return md5.digest();\n }\n\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上示例基于 Dubbo 的一致性哈希负载均衡算法做了一些简化。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、参考资料","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1. ","attrs":{}},{"type":"link","attrs":{"href":"https://www.youtube.com/watch?reload=9&app=desktop&v=iqOTT7_7qXY","title":null,"type":null},"content":[{"type":"text","text":"Comparing Load Balancing Algorithms","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2. 《大型网站技术架构:核心原理与案例分析》","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3. ","attrs":{}},{"type":"link","attrs":{"href":"https://www.cnblogs.com/itfly8/p/5043435.html","title":null,"type":null},"content":[{"type":"text","text":"大型网站架构系列:负载均衡详解(1)","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"4. ","attrs":{}},{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/32841479","title":null,"type":null},"content":[{"type":"text","text":"什么是负载均衡","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"5. ","attrs":{}},{"type":"link","attrs":{"href":"https://avinetworks.com/what-is-load-balancing/","title":null,"type":null},"content":[{"type":"text","text":"What Is Load Balancing","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"6. ","attrs":{}},{"type":"link","attrs":{"href":"https://dubbo.apache.org/zh/docs/v2.7/dev/source/loadbalance/","title":null,"type":null},"content":[{"type":"text","text":"Dubbo 官方负载均衡算法说明","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"7. ","attrs":{}},{"type":"link","attrs":{"href":"https://segmentfault.com/a/1190000004492447","title":null,"type":null},"content":[{"type":"text","text":"负载均衡算法及手段","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"8. ","attrs":{}},{"type":"link","attrs":{"href":"https://segmentfault.com/a/1190000002578457","title":null,"type":null},"content":[{"type":"text","text":"利用 dns 解析来实现网站的负载均衡","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:vivo互联网团队-Zhang Peng","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章