nacosvel/load-balancer

Implementing Several Load Balancing Scheduling Algorithms with PHP

dev-main / 1.x-dev 2024-09-22 12:40 UTC

This package is auto-updated.

Last update: 2024-11-22 13:15:20 UTC


README

GitHub Tag Total Downloads Packagist Version Packagist PHP Version Support Packagist License

安装

推荐使用 PHP 包管理工具 Composer 安装:

composer require nacosvel/load-balancer

文档

Create Server Instance

<?php
$instance1 = new ServerInstance('http://nacos:nacos@192.168.3.1:8848/nacos');
$instance2 = new ServerInstance('http://nacos:nacos@192.168.3.2:8848/nacos', 1);
$instance3 = new ServerInstance('http://nacos:nacos@192.168.3.3:8848/nacos', 1, false);

// $instance->setScheme('https');
// $instance->setUser('user');
// $instance->setPass('pass');
// $instance->setHost('127.0.0.1');
// $instance->setPort(8848);
// $instance->setPath('/nacos');
// var_dump($instance->getScheme());
// var_dump($instance->getUser());
// var_dump($instance->getPass());
// var_dump($instance->getHost());
// var_dump($instance->getPort());
// var_dump($instance->getPath());
// $instance->setAlive(false);
// $instance->setWeight(5);

var_dump([
    $instance1->getURI(),
    $instance2->getURI(),
    $instance3->getURI(),
]);
// array(3) {
//     [0]=>
//   string(29) "http://192.168.3.1:8848/nacos"
//     [1]=>
//   string(29) "http://192.168.3.2:8848/nacos"
//     [2]=>
//   string(29) "http://192.168.3.3:8848/nacos"
// }

$instanceWithAuth = [
    $instance1->getURI(true),
    $instance2->getURI(true),
    $instance3->getURI(true),
];
var_dump($instanceWithAuth);
// array(3) {
//     [0]=>
//   string(41) "http://nacos:nacos@192.168.3.1:8848/nacos"
//     [1]=>
//   string(41) "http://nacos:nacos@192.168.3.2:8848/nacos"
//     [2]=>
//   string(41) "http://nacos:nacos@192.168.3.3:8848/nacos"
// }

Create ServerIterator

<?php
// Create ServerIterator with Array
$instances               = new ServerIterator($instanceWithAuth);
// Create ServerIterator with Iterator
$instances               = new ServerIterator([$instance1, $instance2, $instance3]);
// Create ServerIterator with Weight
$serverAddressWithWeight = [
    'http://nacos:nacos@192.168.3.1:8848/nacos' => 1,
    'http://nacos:nacos@192.168.3.2:8848/nacos' => 5,
    'http://nacos:nacos@192.168.3.3:8848/nacos' => 10,
];
$instances               = new ServerIterator($serverAddressWithWeight);
// Create ServerIterator with String
$serverAddress           = 'http://nacos:nacos@192.168.3.1:8848/nacos,http://nacos:nacos@192.168.3.2:8848/nacos,http://nacos:nacos@192.168.3.3:8848/nacos';
$instances               = new ServerIterator($serverAddress);

var_dump($instances->getReachableServers());
var_dump($instances->getAllServers());

while ($instances->valid()) {
    var_dump($instances->current()->getURI());
    $instances->next();
}
// string(29) "http://192.168.3.1:8848/nacos"
// string(29) "http://192.168.3.2:8848/nacos"
// string(29) "http://192.168.3.3:8848/nacos"

Create LoadBalancer

<?php
$rule = new RandomRule();
$rule = new RoundRule();
// $rule = new WeightedResponseTimeRule();
$rule = new ZoneLimitationRule();
$rule->setZoneAvoidance($_SERVER['REMOTE_ADDR'] ?? '127.0.0.1');
$rule = new BestAvailableRule();

// with default rule
$loadBalancer = new LoadBalancer($instances);
var_dump($loadBalancer->getReachableServers());
var_dump($loadBalancer->getAllServers());

// with client config rule
$loadBalancer = new LoadBalancer();
$loadBalancer->setServerAddresses($instances);
$loadBalancer->setRule($rule);

var_dump($loadBalancer->chooseServer()->getURI());
string(29) "http://192.168.3.3:8848/nacos"

License

Nacosvel LoadBalancer is made available under the MIT License (MIT). Please see License File for more information.