Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
hi,你好
测试代码:
并发连接数 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根据内部轮询表判断哪台服务器连接数少就分配给谁,这样达到连接数最终均衡的目的。