This article mainly introduces the php implementation code of the roundrobin weight round robin algorithm. if you need the php script language, refer to the following code first.
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 <10; $ I ++) {round_robin ($ hosts, $ result);}/* output result */print_r ($ result ); /* round robin */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] =>
[1] => B
[2] => c
[3] =>
[4] =>
[5] => B
[6] =>
[7] => c
[8] => B
[9] =>
)
In the server load balancer server, the implementation algorithm is round-robin Weighted round robin, that is, in the back-end server list, each server is assigned a weight, representing its probability of being used.
This code separates the most concise process and does not consider backend suspension. you can see how it is implemented for your reference only.
For more articles about the php implementation code of the round robin weighted round robin algorithm, visit PHP!