国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼

2024-05-04 23:46:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了round robin權(quán)重輪循算法php實(shí)現(xiàn)代碼,需要的朋友可以參考下
 

先上代碼,采用php腳本語(yǔ)言

<?php/*  * Copyright (C) FatHong *//* 數(shù)據(jù)初始化,weight: 權(quán)重 */$hosts['a'] = array('weight' => 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();/* 模擬10次 */for ($i = 0; $i < 10; $i++) {  round_robin($hosts, $result);}/* 輸出結(jié)果 */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;}

輸出結(jié)果:

Array
(
[0] => a
[1] => b
[2] => c
[3] => a
[4] => a
[5] => b
[6] => a
[7] => c
[8] => b
[9] => a
)

負(fù)載均衡的服務(wù)器中,其實(shí)現(xiàn)算法有種是round-robin權(quán)重輪循,就是后端的服務(wù)器列表中,給每個(gè)服務(wù)器標(biāo)上權(quán)重,代表它被采用的機(jī)率。

這段代碼把最簡(jiǎn)潔的流程剝離出來(lái),沒(méi)考慮后端掛起等情況,可以知道它是怎么實(shí)現(xiàn)的,僅供參考



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 甘谷县| 南昌市| 五原县| 阜宁县| 秭归县| 隆昌县| 循化| 栖霞市| 屯门区| 黑山县| 峨眉山市| 社会| 渭南市| 马龙县| 财经| 景德镇市| 六安市| 和平区| 兴化市| 米泉市| 靖安县| 科技| 辽宁省| 南宫市| 互助| 扎兰屯市| 江北区| 安图县| 大石桥市| 洪江市| 浦城县| 长泰县| 且末县| 内乡县| 古蔺县| 苗栗市| 延川县| 什邡市| 余庆县| 宜良县| 青神县|