Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加轮询负载均衡 #116

Merged
merged 1 commit into from
Nov 19, 2023
Merged

增加轮询负载均衡 #116

merged 1 commit into from
Nov 19, 2023

Conversation

twomiao
Copy link
Contributor

@twomiao twomiao commented Nov 18, 2023

hi,你好

GatewayWorker 负载均衡模式随机性分配,导致每个worker 服务连接负载不均匀,有的进程连接数比较少,有的连接数比较多。
所以扩展了一下轮询负载均衡,Nginx 默认也是轮询负载均衡。

测试代码:
并发连接数 5000,测试负载均衡是否均匀。

增加负载均衡模式两种:随机,轮询(已更改为默认)

// 设置轮询
Gateway::$selectLoadBalancingMode = Gateway::ROUTER_ROUND_ROBIN;

// 设置随机
Gateway::$selectLoadBalancingMode = Gateway::ROUTER_RANDOM;

测试负载均衡结果如下:

随机负载均衡:
{
"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
}

从结果来看轮询负载均衡能够好分配连接到业务逻辑服务器,不会出现某些业务服务器空闲,有些繁忙。
可自定义选择使用随机,轮询,我已经修改默认为轮询均衡分配连接给BusinessWorker服务器。

// array_search(min($roundRobinRecord), $roundRobinRecord, true);
使用最小值就是方便分布式部署新的服务器进程上线空闲,这时候连接上线初始化连接为0,
Gateway根据内部轮询表判断哪台服务器连接数少就分配给谁,这样达到连接数最终均衡的目的。

@walkor walkor merged commit 3e86fba into walkor:master Nov 19, 2023
@walkor
Copy link
Owner

walkor commented Nov 19, 2023

感谢pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants