diff --git a/notes/pics/lb-dns.png b/notes/pics/lb-dns.png new file mode 100644 index 00000000..19345683 Binary files /dev/null and b/notes/pics/lb-dns.png differ diff --git a/notes/pics/lb-http-redirect.png b/notes/pics/lb-http-redirect.png new file mode 100644 index 00000000..da0c3fa9 Binary files /dev/null and b/notes/pics/lb-http-redirect.png differ diff --git a/notes/pics/lb-ip-hash.png b/notes/pics/lb-ip-hash.png new file mode 100644 index 00000000..c9cbe07d Binary files /dev/null and b/notes/pics/lb-ip-hash.png differ diff --git a/notes/pics/lb-least-conn-problem.png b/notes/pics/lb-least-conn-problem.png new file mode 100644 index 00000000..989050fa Binary files /dev/null and b/notes/pics/lb-least-conn-problem.png differ diff --git a/notes/pics/lb-least-conn.png b/notes/pics/lb-least-conn.png new file mode 100644 index 00000000..a18dbccd Binary files /dev/null and b/notes/pics/lb-least-conn.png differ diff --git a/notes/pics/lb-random.png b/notes/pics/lb-random.png new file mode 100644 index 00000000..78ec1e84 Binary files /dev/null and b/notes/pics/lb-random.png differ diff --git a/notes/pics/lb-round-robin-weak.png b/notes/pics/lb-round-robin-weak.png new file mode 100644 index 00000000..7a3250a4 Binary files /dev/null and b/notes/pics/lb-round-robin-weak.png differ diff --git a/notes/pics/lb-round-robin.png b/notes/pics/lb-round-robin.png new file mode 100644 index 00000000..b4d2f008 Binary files /dev/null and b/notes/pics/lb-round-robin.png differ diff --git a/notes/pics/lb-weighted-round-robin.png b/notes/pics/lb-weighted-round-robin.png new file mode 100644 index 00000000..cbbbd79d Binary files /dev/null and b/notes/pics/lb-weighted-round-robin.png differ diff --git a/notes/集群.md b/notes/集群.md index df93c17c..0a0125ce 100644 --- a/notes/集群.md +++ b/notes/集群.md @@ -45,14 +45,14 @@ 轮询算法把每个请求轮流发送到每个服务器上。 -下图中,一共有 6 个客户端产生了 6 个请求,这 6 个请求按 (1, 2, 3, 4, 5, 6) 的顺序发送。(1, 3, 5) 的请求会被发送到服务器 1,(2, 4, 6) 的请求会被发送到服务器 2。 +下图中,6 个请求按 (1, 2, 3, 4, 5, 6) 的顺序到达。(1, 3, 5) 的请求会被发送到服务器 1,(2, 4, 6) 的请求会被发送到服务器 2。 -

+

该算法比较适合每个服务器的性能差不多的场景,如果有性能存在差异的情况下,那么性能较差的服务器可能无法承担过大的负载(下图的 Server 2)。 -

+

#### 2. 加权轮询(Weighted Round Robin) @@ -60,9 +60,9 @@ 例如下图中,服务器 1 被赋予的权值为 5,服务器 2 被赋予的权值为 1,那么 (1, 2, 3, 4, 5) 请求会被发送到服务器 1,(6) 请求会被发送到服务器 2。 -

+

-实际实现(例如 Nginx 的平滑加权轮询)会把请求交错发送,避免连续的请求集中到同一台服务器上。 +实际实现(例如 Nginx 的平滑加权轮询)会把请求交错发送,避免连续的请求集中到同一台服务器上,上图最后一行是按 Nginx 源码中的算法计算出的顺序。 #### 3. 最少连接(Least Connections) @@ -70,13 +70,13 @@ 例如下图中,(1, 3, 5) 请求会被发送到服务器 1,但是 (1, 3) 很快就断开连接,此时只有 (5) 请求连接服务器 1;(2, 4, 6) 请求被发送到服务器 2,只有 (2) 的连接断开,此时 (6, 4) 请求连接服务器 2。该系统继续运行时,服务器 2 会承担过大的负载。 -

+

最少连接算法就是将请求发送给当前最少连接数的服务器上。 例如下图中,服务器 1 当前连接数最小,那么新到来的请求 6 就会被发送到服务器 1 上。 -

+

#### 4. 加权最少连接(Weighted Least Connection) @@ -88,7 +88,7 @@ 和轮询算法类似,该算法比较适合服务器性能差不多的场景。 -

+

#### 6. 源地址哈希法 (IP Hash) @@ -96,9 +96,9 @@ 可以保证同一 IP 的客户端的请求会转发到同一台服务器上,用来实现会话粘滞(Sticky Session)。 -

+

-缺点是同一出口 IP 后面的大量用户会落到同一台服务器上,造成负载不均;服务器数量变化时取模结果会全部改变,可以改用一致性哈希。 +缺点是同一出口 IP 后面的大量用户会落到同一台服务器上,造成负载不均;服务器数量变化时大部分客户端的取模结果都会改变,可以改用一致性哈希。需要注意的是,Nginx 的 ip_hash 只用 IPv4 地址的前 3 个字节计算哈希,同一个 /24 网段的客户端都会落到同一台服务器上。 ### 转发实现 @@ -113,7 +113,7 @@ HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服 该负载均衡转发的缺点比较明显,实际场景中很少使用它。 -

+

#### 2. DNS 域名解析 @@ -129,7 +129,7 @@ HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服 大型网站基本使用了 DNS 作为第一级负载均衡手段,然后在内部使用其它方式做第二级负载均衡。也就是说,域名解析的结果为内部的负载均衡服务器 IP 地址。 -

+

#### 3. 反向代理服务器 @@ -174,7 +174,9 @@ HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服 参考: - [Comparing Load Balancing Algorithms](http://www.jscape.com/blog/load-balancing-algorithms) -- [Redirection and Load Balancing](http://slideplayer.com/slide/6599069/#) +- [Nginx: ngx_http_upstream_module](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) +- [Nginx 源码:ngx_http_upstream_round_robin.c](https://github.com/nginx/nginx/blob/master/src/http/ngx_http_upstream_round_robin.c) +- [Redirection and Load Balancing](https://web.archive.org/web/20250420002753/http://slideplayer.com/slide/6599069/) ## 二、集群下的 Session 管理 @@ -229,5 +231,5 @@ HTTP 重定向负载均衡服务器使用某种负载均衡算法计算得到服 参考: -- [Session Management using Spring Session with JDBC DataStore](https://sivalabs.in/2018/02/session-management-using-spring-session-jdbc-datastore/) +- [Session Management using Spring Session with JDBC DataStore](https://web.archive.org/web/20230602191402/https://www.sivalabs.in/2018/02/session-management-using-spring-session-jdbc-datastore/)