跳到主要内容

负载均衡

当代理配置了多个目标服务(targets)时,系统支持通过不同的负载均衡策略将请求分发到各个目标服务。

支持的策略

策略值说明
roundrobin轮询,按顺序轮流分配请求(默认)
weight加权轮询,按权重比例分配请求
random随机选择目标服务
leastconn分配给当前连接数最少的目标

配置示例

轮询

[[proxies]]
name = "web"
protocol = "http"
custom_domains = ["a.domain.com"]
targets = [
{ host = "127.0.0.1", port = 8001, name = "web-1" },
{ host = "127.0.0.1", port = 8002, name = "web-2" }
]

[proxies.loadbalance]
strategy = "roundrobin"

加权轮询

[[proxies]]
name = "web"
protocol = "http"
custom_domains = ["a.domain.com"]
targets = [
{ host = "127.0.0.1", port = 8001, weight = 70, name = "web-large" },
{ host = "127.0.0.1", port = 8002, weight = 30, name = "web-small" }
]

[proxies.loadbalance]
strategy = "weight"

TCP 长连接

[[proxies]]
name = "database"
protocol = "tcp"
remote_port = 3306
targets = [
{ host = "192.168.1.10", port = 3306, name = "db-1" },
{ host = "192.168.1.11", port = 3306, name = "db-2" }
]

[proxies.loadbalance]
strategy = "leastconn"

参数说明

loadbalance 配置

参数名类型默认值描述必填
strategyStringroundrobin负载均衡策略

targets 子参数

参数名类型默认值描述必填
hostString127.0.0.1目标服务主机地址
portInteger-目标服务端口
weightInteger1权重值,用于加权轮询策略
nameString-目标服务名称