负载均衡(Balancer)
负载均衡用于在多个出站之间选择一个最佳目标,配合路由规则的 destination 使用。
配置位置
balancers 属于 routing 配置:
{
"routing": {
"rules": [
{
"inboundTag": ["in"],
"destination": "round"
}
],
"balancers": [
{
"tag": "round",
"selector": ["out"],
"strategy": { "type": "roundRobin" }
}
]
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TIP
outbound 与 balancer 共享 destination 命名空间,tag 不能重复,否则启动失败。
WARNING
balancer 允许在 selector / fallback 中引用其他 balancer;若形成环路,启动会失败。
BalancerObject
{
"tag": "balancer",
"selector": [],
"fallbackTag": "outbound",
"strategy": {}
}2
3
4
5
6
tag: string
负载均衡器标识,用于在路由规则中作为 destination。
selector: [string]
字符串数组,按前缀匹配出站 tag,例如 ["a"] 可匹配 a / ab。
也可以直接写入 balancer tag,会被解析为实际 outbound(支持嵌套)。
当策略为 selector / fallback 时,selector 的顺序会被保留,用于“按顺序挑选可用节点”。
fallbackTag: string
当无可用节点时的兜底目标(可填写 outbound 或 balancer)。
strategy: StrategyObject
StrategyObject
{
"type": "roundRobin",
"settings": {}
}2
3
4
type: "random" | "roundRobin" | "leastPing" | "leastLoad" | "selector" | "fallback"
random默认值。随机选择匹配到的出站。roundRobin轮询选择。leastPing选择延迟最小的出站(需 observatory)。leastLoad选择最稳定的出站(需 observatory)。selector按顺序选择首个可用节点(可与 observatory 结合过滤不健康节点)。fallback类似selector,并自动在selector末尾追加fallbackTag。
TIP
无论哪一种模式,一旦其所有的 selector 对应节点配置了 observatory,则可以过滤出健康节点。若没有任何健康节点可用,会尝试 fallbackTag。
settings: StrategySettingsObject
StrategySettingsObject(leastLoad)
目前只有 leastLoad 支持该配置项。
{
"expected": 2,
"maxRTT": "1s",
"tolerance": 0.01,
"baselines": ["1s"],
"costs": [
{
"regexp": false,
"match": "tag",
"value": 0.5
}
]
}2
3
4
5
6
7
8
9
10
11
12
13
expected: number
负载均衡器选出最优节点的个数,流量将在这几个节点中随机分配。
maxRTT: string
最高可接受的测速 RTT 时长。
tolerance: float number
最多可接受的测速失败比例。
baselines: [string]
最高可接受的测速 RTT 标准差时长。
costs: [CostObject]
可选配置项,一个数组,可以给所有出站指定权重。
regexp: true | false
是否用正则表达式选择出站 tag。
match: string
匹配出站 tag。
value: float number
权重值,值越大,对应节点越不易被选中。