PHP實(shí)現(xiàn)的服務(wù)器一致性hash分布算法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的服務(wù)器一致性hash分布算法。分享給大家供大家參考,具體如下:
<?php
/**
* 對(duì)服務(wù)器進(jìn)行一致性hash分布算法
*/
class HashRing
{
private $servers = array();
private $nodeList = array();
private $nodeHashList = array();
private $nodeTotalNum = 0;
private $virtualNodeNum = 32;
private $keyHash = '';
public function __construct($servers)
{
$this->servers = $servers;
foreach ($servers as $server) {
for ($i = 0; $i < $this->virtualNodeNum; $i++) {
$this->nodeList[sprintf("%u", crc32($server.'-'.$i))] = array($server, $i);
}
}
ksort($this->nodeList);
$this->nodeHashList = array_keys($this->nodeList);
}
private function getNodeIndex($key)
{
$this->keyHash = sprintf("%u", crc32($key));
if ($this->keyHash > end($this->nodeHashList)) {
$this->keyHash = $this->keyHash % end($this->nodeHashList);
}
if ($this->keyHash <= reset($this->nodeHashList)) {
return 0;
}
$this->nodeTotalNum = count($this->nodeHashList);
return $this->binaryChopIndex(0, $this->nodeTotalNum);
}
private function binaryChopIndex($l=0, $r=0)
{
if ($l < $r) {
$avg = intval(($l+$r) / 2);
if ($this->nodeHashList[$avg] == $this->keyHash) {
return $avg;
} elseif ($this->keyHash < $this->nodeHashList[$avg] && ($avg > 0)) {
return $this->binaryChopIndex($l, $avg-1);
} else {
return $this->binaryChopIndex($avg+1, $r);
}
} else {
return $l;
}
}
public function getServersByKey($key, $num=1)
{
$index = $this->getNodeIndex($key);
$server = $this->nodeList[$this->nodeHashList[$index]];
if ($num == 1) {
return $server[0];
}
if ($num >= count($this->servers)) {
$num = count($this->servers);
}
$result = array($server[0]);
for ($i=$index+1; true; $i++) {
if ($i >= $this->nodeTotalNum) {
$i = 0;
}
$nextServer = $this->nodeList[$this->nodeHashList[$i]];
if (!in_array($nextServer[0], $result)) {
$result[] = $nextServer[0];
}
if (count($result) == $num) {
break;
}
}
return $result;
}
}
//示例
$servers = array(
'127.0.0.1:11211',
'127.0.0.1:11212',
'127.0.0.1:11213',
'127.0.0.1:11214',
'127.0.0.1:11215'
);
$obj = new HashRing($servers);
$servers = $obj->getServersByKey('testkey', 2);
print_r($servers);
echo "\n";
運(yùn)行結(jié)果:
Array
(
[0] => 127.0.0.1:11214
[1] => 127.0.0.1:11211
)
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ì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)無限極分類的兩種方式示例【遞歸和引用方式】
這篇文章主要介紹了PHP實(shí)現(xiàn)無限極分類的兩種方式,結(jié)合實(shí)例形式分析了php基于遞歸和引用方式進(jìn)行數(shù)組遍歷的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
PHP合并數(shù)組+與array_merge的區(qū)別分析
PHP中兩個(gè)數(shù)組合并可以使用+或者array_merge,但之間還是有區(qū)別的,而且這些區(qū)別如果了解不清楚項(xiàng)目中會(huì)要命的!2010-08-08
php中XMLHttpRequest(Ajax)不能設(shè)置自定義的Referer的解決方法
php中XMLHttpRequest(Ajax)不能設(shè)置自定義的Referer的解決方法,需要的朋友可以參考下。2011-11-11
phpmyadmin 3.4 空密碼登錄的實(shí)現(xiàn)方法
很多時(shí)候我們?cè)诒緳C(jī)測(cè)試時(shí)會(huì)將root用戶密碼設(shè)置為空。2010-05-05
php使用Jpgraph繪制簡單X-Y坐標(biāo)圖的方法
這篇文章主要介紹了php使用Jpgraph繪制簡單X-Y坐標(biāo)圖的方法,實(shí)例分析了Jpgraph繪制坐標(biāo)圖及繪制曲線的相關(guān)技巧,需要的朋友可以參考下2015-06-06

