Code first, with PHP scripting language
<?php/* Copyright (C) Fathong */* Data initialization, Weight: Weight */$hosts [' a '] = array (' We Ight ' + 5, ' current_weight ' + 0, ' count ' + 0); $hosts [' b '] = array (' Weight ' + 3, ' current_weight ' = + 0, ' Count ' = 0); $hosts [' c '] = Array (' weight ' = + 2, ' current_weight ' = 0, ' count ' = 0); $result = Array (); /* Simulate 10 times */for ($i = 0; $i <, $i + +) {Round_robin ($hosts, $result),}/* Output */print_r ($result); /* Round robin round */function round_robin (& $hosts, & $result) {$total = 0; $best = null; foreach ($hosts as $key = + $item) {$current = & $hosts [$key]; $weight = $current [' Weight ']; $current [' current_weight '] + = $weight; $total + = $weight; if ($best = = null) | | ($hosts [$best] [' Current_weight '] < $current [' Current_weight ']) {$best = $key; }} $hosts [$best] [' current_weight ']-= $total; $hosts [$best] [' count ']++; $result [] = $best;}
Output Result:
Array
(
[0] = a
[1] = b
[2] = C
[3] = a
[4] = a
[5] = b
[6] = a
[7] = C
[8] = b
[9] = a
)
Load Balancing server, its implementation algorithm is a round-robin weight round, is the back end of the server list, to each server weighted, representing the probability of its adoption.
This code to the most concise process stripping out, regardless of the back end hangs and so on, you can know how it is implemented, for reference only.