2019年12月

1. 简单实现

一般能想到的实现应该是记录一个 current index, 再取mod, 存在的问题是请求不够平滑。
例如, 要对3台机器进行 加权轮询负载均衡, 权重分别是 1,2,3

机器权重
0011
0022
0033

那么结果是

次数indexmod结果命中机器
100001
211002
322002
433003
544003
655003
750001

...

存在的问题,请求会不平滑, 同一台机器的请求会比较集中,比如上面的结果,后面3次集中在 003 的机器。

2. 更好的算法

每个后端peer都有三个权重变量,先解释下它们的含义。

(1) weight

配置文件中指定的该后端的权重,这个值是固定不变的。

(2) current_weight

后端目前的权重,一开始为0,之后会动态调整。那么是怎么个动态调整呢?

每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight,

同时累加所有后端的weight,保存为total。

如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total。

如果该后端没有被选定,那么current_weight不用减小。

 

弄清了三个weight字段的含义后,加权轮询算法可描述为:

1 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行:

  peer->current_weight += peer->effecitve_weight。
  同时累加所有peer的effective_weight,保存为total。

2 从集群中选出current_weight最大的peer,作为本次选定的后端。

3 对于本次选定的后端,执行:peer->current_weight -= total。

还是上面 3台机器 , 权重分别是 1,2,3

机器权重
0011
0022
0033

那么结果是
WXWorkCapture_15764984078276.png

请求就稍微平滑些了

参考:
Dubbo加权轮询负载均衡的源码和Bug,了解一下?
Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 上篇

example:

curl -o /dev/null -s -v --resolve www.baidu.com:443:103.235.46.39 https://www.baidu.com

输出

$ curl -o /dev/null -s -v --resolve www.baidu.com:443:103.235.46.39 https://www.baidu.com
* Added www.baidu.com:443:103.235.46.39 to DNS cache
* Rebuilt URL to: https://www.baidu.com/
* Hostname www.baidu.com was found in DNS cache
*   Trying 103.235.46.39...
* TCP_NODELAY set
* Connected to www.baidu.com (103.235.46.39) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [96 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [3629 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; ST=beijing; L=beijing; OU=service operation department; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
*  start date: May  9 01:22:02 2019 GMT
*  expire date: Jun 25 05:31:02 2020 GMT
*  subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
*  issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign Organization Validation CA - SHA256 - G2
*  SSL certificate verify ok.
} [5 bytes data]
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.58.0
> Accept: */*
> 
{ [5 bytes data]
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Tue, 03 Dec 2019 02:02:06 GMT
< Etag: "58860411-98b"
< Last-Modified: Mon, 23 Jan 2017 13:24:33 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
< 
{ [1448 bytes data]
* Connection #0 to host www.baidu.com left intact