All Projects → Tinywan → Load Balancing

Tinywan / Load Balancing

Implementing several load balancing scheduling algorithms with PHP - 用 PHP 实现几种负载均衡调度算法

Introduction(介绍)

用 PHP 实现几种负载均衡调度算法,详细见 负载均衡算法 系列。fork

Scheduling Algorithm (调度算法)

Install

composer require tinywan/load-balancing

Basic Usage

// 服务器数
$services = [
    '192.168.10.1' => 5,
    '192.168.10.2' => 1,
    '192.168.10.3' => 1,
];

// 使用平滑加权算法 (Smooth Weighted Round Robin)
$robin = new \Robin\SmoothWeightedRobin();
$robin->init($services);

$nodes = [];
$sumWeight = $robin->getSumWeight();
for ($i = 1; $i <= $sumWeight; $i++) {
    $node = $robin->next();
    $nodes[$i] = $node;
}
var_export($nodes);

// 会生成如下均匀序列
array (
  1 => '192.168.10.1',
  2 => '192.168.10.1',
  3 => '192.168.10.2',
  4 => '192.168.10.1',
  5 => '192.168.10.3',
  6 => '192.168.10.1',
  7 => '192.168.10.1',
)

Help

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].