本文實(shí)例講述了PHP一致性hash分布式算法封裝類定義與用法。分享給大家供大家參考,具體如下:
一、無虛擬節(jié)點(diǎn)實(shí)現(xiàn)
?php
/**
* 一致性hash分布式算法
* @param $key
* @return int
* 實(shí)現(xiàn)步驟
* 1.先將0~ 是32位最大帶符號(hào)整數(shù)(0x7FFFFFFF) 想象成一個(gè)閉環(huán)
* 2.將服務(wù)器列表通過hash算法分布在 圓環(huán)之中
* 3.將key值也分布在圓環(huán)之中
* 4.通過_isSorted判斷服務(wù)器是否需要進(jìn)行倒序排序 排序后遍歷服務(wù)器 找到最近的服務(wù)器 返回
* hash算法是不保證平衡的 為了盡量保證平衡性 我們應(yīng)該加入虛擬節(jié)點(diǎn)數(shù) 將一個(gè)服務(wù)器節(jié)點(diǎn)虛擬化成為多個(gè) 較大程度上保證了平衡性
*/
class FlexiHash{
private $_serverList = array();
private $_isSorted = false;
private $_virtual_node_num = 20;//虛擬節(jié)點(diǎn)數(shù) 服務(wù)器越少 增加的虛擬節(jié)點(diǎn)數(shù)應(yīng)該越多
//通過hash算法返回一個(gè)整數(shù)值
protected function myHash($key){
$md5 = substr(md5($key),0,8);
$seed = 31; //種子值
$hash=0;
for($i=0;$i8;$i++){
$hash = $hash*$seed+ord($md5{$i}); //ord 返回ascii值
$i++;
}
return $hash0x7FFFFFFF; //0x7FFFFFFF表示最大值
}
//添加服務(wù)器
function addServer($server){
$hash =$this->myHash($server. '#1');
if(!isset($this->_serverList[$hash])) {
for ($i = 1; $i = $this->_virtual_node_num; $i++) {
$hash = $this->myHash($server . '#' . $i);
$this->_serverList[$hash] = $server;
}
}
$this->_isSorted = false;
return true;
}
//刪除服務(wù)器
function removeServer($server){
for ($i = 1; $i = $this->_virtual_node_num; $i++) {
$hash = $this->myHash($server . '#' . $i);
unset($this->_serverList[$hash]);
}
$this->_isSorted = false;
return true;
}
//獲取服務(wù)器
function lookup($key){
$hash =$this->myHash($key);
if(!$this->_isSorted){
krsort($this->_serverList,SORT_NUMERIC);
$this->_isSorted = true;
}
foreach($this->_serverList as $pos=>$server){
if($hash >= $pos) return $server;
}
return end($this->_serverList);
}
public function getServerList(){
krsort($this->_serverList,SORT_NUMERIC);
return $this->_serverList;
}
}
//demo test
$hserver = new FlexiHash();
//添加服務(wù)器
$hserver->addServer('192.168.1.1');
$hserver->addServer('192.168.1.2');
$hserver->addServer('192.168.1.3');
$hserver->addServer('192.168.1.4');
$hserver->addServer('192.168.1.5');
$key1='Key1111';
$key2='Key2222';
$key2='Key3333';
$key2='Key4444';
$key2='Key5555';
$key2='Key6666';
echo "save key1 in server: ".$hserver->lookup($key1).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key2).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key3).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key4).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key5).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key6).PHP_EOL;
echo "================================================".PHP_EOL;
//移除服務(wù)器 key值將自動(dòng)轉(zhuǎn)義到下一臺(tái)服務(wù)器
$hserver->removeServer('192.168.1.4');
echo "save key1 in server: ".$hserver->lookup($key1).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key2).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key3).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key4).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key5).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key6).PHP_EOL;
echo "================================================".PHP_EOL;
//恢復(fù)故障服務(wù)器 key值將恢復(fù)原來服務(wù)器
$hserver->addServer('192.168.1.4');
echo "save key1 in server: ".$hserver->lookup($key1).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key2).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key3).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key4).PHP_EOL;
echo "save key1 in server: ".$hserver->lookup($key5).PHP_EOL;
echo "save key2 in server: ".$hserver->lookup($key6).PHP_EOL;
二、運(yùn)行結(jié)果:
save key1 in server: 192.168.1.4
save key2 in server: 192.168.1.4
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
================================================
save key1 in server: 192.168.1.2
save key2 in server: 192.168.1.5
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
================================================
save key1 in server: 192.168.1.4
save key2 in server: 192.168.1.4
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
save key1 in server: 192.168.1.3
save key2 in server: 192.168.1.3
PS:這里再為大家提供2款hash相關(guān)在線工具供大家參考使用:
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php加密方法總結(jié)》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《php正則表達(dá)式用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- 標(biāo)準(zhǔn)PHP的AES加密算法類
- php 地區(qū)分類排序算法
- PHP對(duì)稱加密算法(DES/AES)類的實(shí)現(xiàn)代碼
- PHP實(shí)現(xiàn)的各類hash算法長(zhǎng)度及性能測(cè)試實(shí)例
- PHP排序算法類實(shí)例
- PHP經(jīng)典算法集錦【經(jīng)典收藏】
- php加密算法之實(shí)現(xiàn)可逆加密算法和解密分享
- PHP面試常用算法(推薦)
- PHP mcrypt可逆加密算法分析
- PHP實(shí)現(xiàn)的ID混淆算法類與用法示例