還請Gatewayworker 大佬去看看呢,實現(xiàn)思路也比較簡單。
Gateway 內(nèi)部維護一個數(shù)組結構,后續(xù)上線的businessworker 初始化0,后續(xù)誰的連接數(shù)小就優(yōu)先分發(fā)給誰。
分布式部署如果businessworker 性能不足的時候,需要增加幾十個worker干活,新來的客戶端連接會優(yōu)先分配給剛上線businessworker進程,直到他們與運行已久的進程連接數(shù)相同,才會逐個輪訓,最終達到連接數(shù)均勻。
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
}