PHP code example of tourze / load-balancer

1. Go to this page and download the library: Download tourze/load-balancer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / load-balancer example snippets


use Tourze\LoadBalance\LoadBalancerFactory;
use Tourze\LoadBalance\Node;

// 1. 定义你的节点
$nodes = [
    new Node(key: 'server1', value: '192.168.1.100', weight: 10),
    new Node(key: 'server2', value: '192.168.1.101', weight: 20),
];

// 2. 使用工厂创建一个负载均衡器 (例如,平滑加权轮询)
$balancer = LoadBalancerFactory::createSmoothWeightedRoundRobin();

// 3. 选择一个节点
$selectedServer = $balancer->select($nodes);

echo $selectedServer;

new Node(
    string $key,   // 节点的唯一标识符
    mixed $value,  // 节点的实际值 (IP, 主机名, 对象等)
    int $weight = 1 // 节点的权重 (必须为非负整数)
);

$balancer = LoadBalancerFactory::createRandom();
$server = $balancer->select($nodes);

$balancer = LoadBalancerFactory::createRoundRobin();

$server1 = $balancer->select($nodes); // server1
$server2 = $balancer->select($nodes); // server2
$server3 = $balancer->select($nodes); // server1 (循环)

$nodes = [
    new Node('A', 'A', 90), // 90% 概率
    new Node('B', 'B', 10), // 10% 概率
];
$balancer = LoadBalancerFactory::createWeighted();
$server = $balancer->select($nodes); // 'A' 或 'B'

$clientIp = '8.8.8.8';
$balancer = LoadBalancerFactory::createIpHash($clientIp);

// 对于同一个 IP,选择结果总是相同的
$server = $balancer->select($nodes);

$balancer = LoadBalancerFactory::createLeastConnections();
$nodes = [
    $node1 = new Node('A', 'A'),
    $node2 = new Node('B', 'B'),
];

// 模拟连接建立
$balancer->incrementConnection($node1);
$balancer->incrementConnection($node1); // A 有 2 个连接
$balancer->incrementConnection($node2); // B 有 1 个连接

// 选择时会返回连接数最少的节点 B
$server = $balancer->select($nodes); // 返回 'B'

// 模拟连接断开
$balancer->decrementConnection($node1);

$balancer = LoadBalancerFactory::createSmoothWeightedRoundRobin();
$nodes = [
    new Node('A', 'A', 5),
    new Node('B', 'B', 1),
    new Node('C', 'C', 1),
];

// 多次调用 select() 将得到一个平滑的、可预测的序列

// 工厂方法默认使用 InMemoryStateProvider
$balancer = LoadBalancerFactory::createRoundRobin();

use Tourze\LoadBalance\State\ApcuStateProvider;

// 检查环境是否支持
if (function_exists('apcu_fetch')) {
    $stateProvider = new ApcuStateProvider('my_app_prefix_');
    $balancer = LoadBalancerFactory::createRoundRobin($stateProvider);
}

use Symfony\Component\Cache\Adapter\RedisAdapter;
use Tourze\LoadBalance\State\CacheStateProvider;

// 1. 创建一个 PSR-6 缓存池实例 (这里以 Redis 为例)
$redisClient = RedisAdapter::createConnection('redis://localhost');
$cachePool = new RedisAdapter($redisClient);

// 2. 创建 CacheStateProvider
$stateProvider = new CacheStateProvider($cachePool, 'my_app_prefix_');

// 3. 将它注入到负载均衡器中
$balancer = LoadBalancerFactory::createLeastConnections($stateProvider);

// 现在,所有状态都将通过 Redis 进行存储和同步