round robin权重轮循算法php实现代码


Posted in PHP onMay 28, 2016

先上代码,采用php脚本语言

<?php

/* 
 * Copyright (C) FatHong
 */

/* 数据初始化,weight: 权重 */
$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);
}

/* 输出结果 */
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;
}

输出结果:

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

负载均衡的服务器中,其实现算法有种是round-robin权重轮循,就是后端的服务器列表中,给每个服务器标上权重,代表它被采用的机率。

这段代码把最简洁的流程剥离出来,没考虑后端挂起等情况,可以知道它是怎么实现的,仅供参考.

PHP 相关文章推荐
用Php实现链结人气统计
Oct 09 PHP
AJAX for PHP简单表数据查询实例
Jan 02 PHP
php 清除网页病毒的方法
Dec 05 PHP
三种php连接access数据库方法
Nov 11 PHP
带密匙的php加密解密示例分享
Jan 29 PHP
php+js iframe实现上传头像界面无跳转
Apr 29 PHP
destoon后台网站设置变成空白的解决方法
Jun 21 PHP
php通过strpos查找字符串出现位置的方法
Mar 17 PHP
php中JSON的使用方法
Apr 30 PHP
yii2带搜索功能的下拉框实例详解
May 12 PHP
php短信接口代码
May 13 PHP
PHP7 windows支持
Mar 09 PHP
php自定义中文字符串截取函数substr_for_gb2312及substr_for_utf8示例
May 28 #PHP
php获取一定范围内取N个不重复的随机数
May 28 #PHP
smarty的section嵌套循环用法示例
May 28 #PHP
PHP引用返回用法示例
May 28 #PHP
php时间函数用法分析
May 28 #PHP
zend framework重定向方法小结
May 28 #PHP
php通过文件头判断格式的方法
May 28 #PHP
You might like
如何用php获取文件名后缀
2013/06/09 PHP
PHP session_start()问题解疑(详细介绍)
2013/07/05 PHP
PHP获取时间排除周六、周日的两个方法
2014/06/30 PHP
浅谈javascript 面向对象编程
2009/10/28 Javascript
jquery动画3.创建一个带遮罩效果的图片走廊
2012/08/24 Javascript
javascript parseInt() 函数的进制转换注意细节
2013/01/08 Javascript
jquery使用Cookie和JSON记录用户最近浏览历史
2016/04/19 Javascript
基于Bootstrap的Metronic框架实现页面链接收藏夹功能
2016/08/29 Javascript
基于vue.js实现图片轮播效果
2016/12/01 Javascript
JS限制输入框输入的实现代码
2018/07/02 Javascript
[04:03]DOTA2肉山黑名单梦之声 风暴之灵中文配音鉴赏
2013/07/03 DOTA
[47:55]Ti4第二日主赛事败者组 NaVi vs EG 1
2014/07/20 DOTA
Python中查看文件名和文件路径
2017/03/31 Python
机器学习经典算法-logistic回归代码详解
2017/12/22 Python
Python编写通讯录通过数据库存储实现模糊查询功能
2019/07/18 Python
windows 10 设定计划任务自动执行 python 脚本的方法
2019/09/11 Python
Python爬虫实现“盗取”微信好友信息的方法分析
2019/09/16 Python
Python object类中的特殊方法代码讲解
2020/03/06 Python
python中 _、__、__xx__()区别及使用场景
2020/06/30 Python
Python下划线5种含义代码实例解析
2020/07/10 Python
使用BeautifulSoup4解析XML的方法小结
2020/12/07 Python
匡威德国官网:Converse德国
2019/01/26 全球购物
美国糖果店:Sugarfina
2019/02/21 全球购物
安德玛加拿大官网:Under Armour加拿大
2019/10/02 全球购物
信息管理专业学生自荐信格式
2013/09/22 职场文书
怎样填写就业意向
2014/04/02 职场文书
专科应届毕业生求职信
2014/06/04 职场文书
培训科主任岗位职责
2014/08/08 职场文书
专升本学生毕业自我鉴定
2014/10/04 职场文书
新郎接新娘保证书
2015/05/08 职场文书
2015年前台文员工作总结
2015/05/18 职场文书
铁人纪念馆观后感
2015/06/16 职场文书
nginx location优先级的深入讲解
2021/03/31 Servers
解决ObjectMapper.convertValue() 遇到的一些问题
2021/06/30 Java/Android
深入理解go缓存库freecache的使用
2022/02/15 Golang
Spring Boot项目传参校验的最佳实践指南
2022/04/05 Java/Android