2017-5-5/PHP实现负载均衡的加权轮询
<?php
// php实现负载均衡的加权轮询(WRR)
class WRR {
// 每次取100人
const num = 100;
// 上次取值时间,秒级时间戳
public $last_time;
// 权重 machine=>weight
public $machines = array(
'a' => 3, // 0.6
'b' => 1, // 0.2
'c' => 1 // 0.2
);
// 占比
public $proportion = array();
// 用户队列
public static $user_ids = array(); public function __construct() {
// 各机器的占比
$total = 0;
foreach ($this->machines as $machine => $weight) {
$total += $weight;
}
$this->proportion['a'] = $this->machines['a'] / $total;
$this->proportion['b'] = $this->machines['b'] / $total;
$this->proportion['c'] = $this->machines['c'] / $total;
} public function getUsers() {
// 用户人数
$cnt = count(self::$user_ids);
$a_num = 0;
$b_num = 0;
$c_num = 0;
if ($cnt >= self::num) { // 队列超过100人
$a_num = round(self::num * $this->proportion['a']);
$b_num = round(self::num * $this->proportion['b']);
$c_num = $cnt - $a_num - $b_num;
} else { // 队列不足100人
$last_time = $this->last_time; // 上次访问时间
while (true) {
$current_time = $this->getMillisecond();
if (($current_time - $last_time) >= 10) { // 当前时间和上一次取值时间超过10ms
$a_num = round($cnt * $this->proportion['a']);
$b_num = round($cnt * $this->proportion['b']);
$c_num = $cnt - $a_num - $b_num;
$this->last_time = self::getMillisecond(); // 更新访问时间
break;
}
}
}
$a = array_splice(self::$user_ids, 0, $a_num);
$b = array_splice(self::$user_ids, 0, $b_num);
$c = array_splice(self::$user_ids, 0, $c_num);
return array(
'a' => $a,
'b' => $b,
'c' => $c
);
} // 获取毫秒级时间戳
public function getMillisecond() {
list($t1, $t2) = explode(" ", microtime());
return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
} // 测试
$wrr = new WRR();
for ($i = 0; $i < 3; $i++) {// 模拟持续不断的用户请求
$random = rand(10, 120);
$user_ids = range(1, $random);
WRR::$user_ids = $user_ids;
$users = $wrr->getUsers();
print_r($users);
}
真实的算法比这个复杂多了,它需要考虑一点,就是来过的用户要保持原来分配的机器,除非原来的机器挂了。这样做的原因是缓存。很多基于内存的缓存,都是基于用户级别的,所以相同的用户保持同一台机器,有助于提升性能。
2017-5-5/PHP实现负载均衡的加权轮询的更多相关文章
- 负载均衡算法,轮询方式 大话设计模式之工厂模式 C#
负载均衡算法,轮询方式 2018-04-13 17:37 by 天才卧龙, 13 阅读, 0 评论, 收藏, 编辑 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现 ...
- 负载均衡算法: 简单轮询算法, 平滑加权轮询, 一致性hash算法, 随机轮询, 加权随机轮询, 最小活跃数算法(基于dubbo) java代码实现
直接上干活 /** * @version 1.0.0 * @@menu <p> * @date 2020/11/17 16:28 */ public class LoadBlance { ...
- 负载均衡之DNS轮询
大多数域名注册商都支持对统一主机添加多条A记录,这就是DNS轮询,DNS服务器将解析请求按照A记录的顺序,随机分配到不同的IP上,这样就完成了简单的负载均衡.下图的例子是:有3台联通服务器.3台电信服 ...
- 如何配置nginx负载均衡配置(轮询,权重,ip绑定)
集群是为了解决单节点无法服务高并发的情况,在集群中nginx是如何分配将来自客户端的请求 转发给服务器的 负载均衡可以提高网站的吞吐量(接受和响应),减轻单台服务器的压力 负载均衡提供了三种策略:轮询 ...
- [原]F5负载均衡示例:轮寻
/** * lihaibo 欢迎转载,请保留原地址 */ 规划: F5 1600 BIG-IP 内网 192.168.100.0 255.255.255.0 外网 10.50.20.0 255.255 ...
- nginx负载均衡 加权轮询和ip_hash
下面给大家总结了几种真正的nginx负载均衡的功能了,在此我们加了一个权重判断法就是根据nginx负载的状态实现分配访问用户到权重值少的机器了,具体配置如下. nginx为后端web服务器(apach ...
- Nginx 负载均衡-加权轮询策略剖析
本文介绍的是客户端请求在多个后端服务器之间的均衡,注意与客户端请求在多个nginx进程之间的均衡相区别(Nginx根据每个工作进程的当前压力调整它们获取监听套接口的几率,那些当前比较空闲的工作进程有更 ...
- Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 下篇
Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 上篇blog讲述了加权轮询算法的原理.以及负载均衡模块中使用的数据结构,接着我们来看看加权轮询算法的具 ...
- Nginx的负载均衡 - 加权轮询 (Weighted Round Robin) 上篇
Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 来看一个简单的Nginx负载均衡配置. http { upstream cluster { ...
随机推荐
- Hadoop之mapreduce
doc Hadoop初探之Stream Hadoop Stream 用python + hadoop streaming 编写分布式程序(一) -- 原理介绍,样例程序与本地调试 用python + ...
- 在 2016 年学 JavaScript 是一种什么样的体验?(React从入门到放弃)
jquery 年代 vs 前端模块化 http://blog.csdn.net/offbye/article/details/52793921 ++ 嘿,我最近接到一个 Web 项目,不过老实说,我这 ...
- HTML+CSS+JS 传智 详细笔记
HTML(1)- -毕向东老师对Html的简介 CSS- -毕老师对CSS的简介 Javascript- -毕老师对JS的简介 html&css等等练习表(W3Cscholl) js练习表回顾 ...
- python接口测试模版
"""Test case implementation""" import sys import functools import diff ...
- Java 数据库篇
一.简易封装JDBC工具类: package com.jackie.MyBatis.main; import java.sql.Connection; import java.sql.DriverMa ...
- 理解 Redis(1) - Redis 简介
Redis 的含义 全称: REmote DIctionary Server 远程词典服务器 由于支持 string, list, set, ordered set, hash 等多重数据结构, 因此 ...
- 改变input中的placeholder样式
1.input[placeholder]{ color:#d5d5d5; } 2.input::-moz-placeholder { color: #d5d5d5; } input:-ms-input ...
- 桂电第一次程序设计 C STL 排序
Problem 1197 # 共享厕所 ediszhao添加于2017-12-22 15:00:56 时间限制 : 1000MS 内存限制 : 65536KB ×提示 : 你已解决此题目. 问题描述 ...
- 算法笔记--单调队列优化dp
单调队列:队列中元素单调递增或递减,可以用双端队列实现(deque),队列的前面和后面都可以入队出队. 单调队列优化dp: 问题引入: dp[i] = min( a[j] ) ,i-m < j ...
- Visual Studio 2015+InstallShield 2015
下载Installshield http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visua ...