mod_proxy 和 mod_proxy_core 支持 3 种负载均衡器,用于在多个后端之间分发负载。其中之一是哈希负载均衡,它非常适用于缓存代理(如 Squid)的负载均衡。如果您将哈希负载均衡的性能与经典的轮询负载均衡进行比较,您会发现性能有所提升,因为后端可以更好地利用其缓存。使用轮询 (RR),每个后端都必须处理完整的 URL 命名空间;而使用哈希负载均衡,每个后端只需处理一部分。这增加了缓存局部性并提升了整体性能。我已将
“wikipedia” 作为哈希负载均衡的测试平台。
$SERVER["socket"] == ":1445" {
proxy-core.balancer = "hash"
proxy-core.protocol = "http"
proxy-core.backends = ( "wikipedia.org" )
proxy-core.rewrite-response = (
"Location" => ( "^http://en.wikipedia.org/(.*)" => "http://127.0.0.1:1445/$1" ),
)
proxy-core.rewrite-request = (
"Host" => ( ".*" => "en.wikipedia.org" ),
)
}
域名
wikipedia.org 解析到多个 IP 地址
(trace) resolving wikipedia.org on port 80
(trace) adding 207.142.131.204:80 to the address-pool
(trace) adding 207.142.131.205:80 to the address-pool
(trace) adding 207.142.131.206:80 to the address-pool
(trace) adding 207.142.131.210:80 to the address-pool
(trace) adding 207.142.131.213:80 to the address-pool
(trace) adding 207.142.131.214:80 to the address-pool
(trace) adding 207.142.131.235:80 to the address-pool
(trace) adding 207.142.131.236:80 to the address-pool
(trace) adding 207.142.131.245:80 to the address-pool
(trace) adding 207.142.131.246:80 to the address-pool
(trace) adding 207.142.131.247:80 to the address-pool
(trace) adding 207.142.131.248:80 to the address-pool
(trace) adding 207.142.131.202:80 to the address-pool
(trace) adding 207.142.131.203:80 to the address-pool
当我请求
http://127.0.0.1:1445/ 时,负载均衡器会获取 URL,对其进行哈希,然后将其发送到其中一个后端。
(trace) using hash-balancing: /wiki/Main_Page -> 207.142.131.204:80
(trace) using hash-balancing: /skins-1.5/monobook/main.css -> 207.142.131.204:80
(trace) using hash-balancing: /skins-1.5/common/commonPrint.css -> 207.142.131.213:80
(trace) using hash-balancing: /skins-1.5/common/wikibits.js -> 207.142.131.245:80
(trace) using hash-balancing: /w/index.php -> 207.142.131.206:80
(trace) using hash-balancing: /w/index.php -> 207.142.131.206:80
(trace) using hash-balancing: /w/index.php -> 207.142.131.206:80
(trace) using hash-balancing: /w/index.php -> 207.142.131.206:80
(trace) using hash-balancing: /skins-1.5/monobook/headbg.jpg -> 207.142.131.202:80
(trace) using hash-balancing: /skins-1.5/monobook/bullet.gif -> 207.142.131.245:80
(trace) using hash-balancing: /skins-1.5/common/images/poweredby_mediawiki_88x31.png -> 207.142.131.236:80
(trace) using hash-balancing: /images/wikimedia-button.png -> 207.142.131.203:80
(trace) using hash-balancing: /skins-1.5/monobook/bullet.gif -> 207.142.131.245:80
(trace) using hash-balancing: /skins-1.5/monobook/user.gif -> 207.142.131.202:80
(trace) using hash-balancing: /images/wiki-en.png -> 207.142.131.204:80
您会看到相同的 URL 会连接到相同的地址。如果其中一个后端发生故障,所有原定发往该后端的请求将分散到其他后端。如果您有 10 个后端,其中 1 个发生故障,则每个后端需要处理该故障后端 1/9 的 URL。* 您有
100 个 URL,10 个后端 * 每个后端处理
(100/10) = 10 个 URL * 一个后端发生故障,其
10 个 URL 分散到其他 9 个后端 * 每个后端现在处理其
之前的 10 个 URL + 故障后端 (10/9) 个 URL 哈希负载均衡遵循来自
“http://icp.ircache.net/carp.txt” 的思想。