解决 Gatewayworker 负载均衡不均PR,

meows

问题描述

还请Gatewayworker 大佬去看看呢,实现思路也比较简单。
Gateway 内部维护一个数组结构,后续上线的businessworker 初始化0,后续谁的连接数小就优先分发给谁。
分布式部署如果businessworker 性能不足的时候,需要增加几十个worker干活,新来的客户端连接会优先分配给刚上线businessworker进程,直到他们与运行已久的进程连接数相同,才会逐个轮训,最终达到连接数均匀。

1个Gateway, 32个Worker,1个注册中心,最终测试结果:
随机负载均衡:
{
"127.0.0.1:ChatBusinessWorker:28": 180,
"127.0.0.1:ChatBusinessWorker:22": 160,
"127.0.0.1:ChatBusinessWorker:12": 144,
"127.0.0.1:ChatBusinessWorker:16": 151,
"127.0.0.1:ChatBusinessWorker:31": 150,
"127.0.0.1:ChatBusinessWorker:13": 146,
"127.0.0.1:ChatBusinessWorker:15": 164,
"127.0.0.1:ChatBusinessWorker:24": 163,
"127.0.0.1:ChatBusinessWorker:25": 169,
"127.0.0.1:ChatBusinessWorker:18": 157,
"127.0.0.1:ChatBusinessWorker:29": 142,
"127.0.0.1:ChatBusinessWorker:6": 168,
"127.0.0.1:ChatBusinessWorker:3": 153,
"127.0.0.1:ChatBusinessWorker:26": 148,
"127.0.0.1:ChatBusinessWorker:21": 134,
"127.0.0.1:ChatBusinessWorker:9": 131,
"127.0.0.1:ChatBusinessWorker:27": 155,
"127.0.0.1:ChatBusinessWorker:30": 173,
"127.0.0.1:ChatBusinessWorker:23": 169,
"127.0.0.1:ChatBusinessWorker:1": 156,
"127.0.0.1:ChatBusinessWorker:4": 154,
"127.0.0.1:ChatBusinessWorker:20": 156,
"127.0.0.1:ChatBusinessWorker:7": 146,
"127.0.0.1:ChatBusinessWorker:17": 149,
"127.0.0.1:ChatBusinessWorker:10": 162,
"127.0.0.1:ChatBusinessWorker:19": 163,
"127.0.0.1:ChatBusinessWorker:5": 147,
"127.0.0.1:ChatBusinessWorker:2": 163,
"127.0.0.1:ChatBusinessWorker:8": 162,
"127.0.0.1:ChatBusinessWorker:11": 164,
"127.0.0.1:ChatBusinessWorker:14": 166,
"127.0.0.1:ChatBusinessWorker:0": 155
}

// 可见随机的劣势,最小值131,最大值180,分配不均衡导致性能问题。

轮询负载均衡:
{
"127.0.0.1:ChatBusinessWorker:24": 157,
"127.0.0.1:ChatBusinessWorker:13": 157,
"127.0.0.1:ChatBusinessWorker:29": 157,
"127.0.0.1:ChatBusinessWorker:11": 157,
"127.0.0.1:ChatBusinessWorker:30": 157,
"127.0.0.1:ChatBusinessWorker:20": 157,
"127.0.0.1:ChatBusinessWorker:4": 157,
"127.0.0.1:ChatBusinessWorker:18": 157,
"127.0.0.1:ChatBusinessWorker:10": 156,
"127.0.0.1:ChatBusinessWorker:0": 156,
"127.0.0.1:ChatBusinessWorker:1": 156,
"127.0.0.1:ChatBusinessWorker:28": 156,
"127.0.0.1:ChatBusinessWorker:21": 156,
"127.0.0.1:ChatBusinessWorker:2": 156,
"127.0.0.1:ChatBusinessWorker:7": 156,
"127.0.0.1:ChatBusinessWorker:3": 156,
"127.0.0.1:ChatBusinessWorker:14": 156,
"127.0.0.1:ChatBusinessWorker:19": 156,
"127.0.0.1:ChatBusinessWorker:17": 156,
"127.0.0.1:ChatBusinessWorker:26": 156,
"127.0.0.1:ChatBusinessWorker:6": 156,
"127.0.0.1:ChatBusinessWorker:22": 156,
"127.0.0.1:ChatBusinessWorker:9": 156,
"127.0.0.1:ChatBusinessWorker:15": 156,
"127.0.0.1:ChatBusinessWorker:27": 156,
"127.0.0.1:ChatBusinessWorker:16": 156,
"127.0.0.1:ChatBusinessWorker:25": 156,
"127.0.0.1:ChatBusinessWorker:5": 156,
"127.0.0.1:ChatBusinessWorker:8": 156,
"127.0.0.1:ChatBusinessWorker:12": 156,
"127.0.0.1:ChatBusinessWorker:23": 156,
"127.0.0.1:ChatBusinessWorker:31": 156
}
718 1 0
1个回答

walkor 打赏

很好的想法,已经合并

×
🔝