您好,登錄后才能下訂單哦!
使用PHP怎么封裝一個一致性hash分布式算法?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一、無虛擬節點實現
<?php /** * 一致性hash分布式算法 * @param $key * @return int * 實現步驟 * 1.先將0~ 是32位最大帶符號整數(0x7FFFFFFF) 想象成一個閉環 * 2.將服務器列表通過hash算法分布在 圓環之中 * 3.將key值也分布在圓環之中 * 4.通過_isSorted判斷服務器是否需要進行倒序排序 排序后遍歷服務器 找到最近的服務器 返回 * hash算法是不保證平衡的 為了盡量保證平衡性 我們應該加入虛擬節點數 將一個服務器節點虛擬化成為多個 較大程度上保證了平衡性 */ class FlexiHash{ private $_serverList = array(); private $_isSorted = false; private $_virtual_node_num = 20;//虛擬節點數 服務器越少 增加的虛擬節點數應該越多 //通過hash算法返回一個整數值 protected function myHash($key){ $md5 = substr(md5($key),0,8); $seed = 31; //種子值 $hash=0; for($i=0;$i<8;$i++){ $hash = $hash*$seed+ord($md5{$i}); //ord 返回ascii值 $i++; } return $hash&0x7FFFFFFF; //0x7FFFFFFF表示最大值 } //添加服務器 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; } //刪除服務器 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; } //獲取服務器 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(); //添加服務器 $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; //移除服務器 key值將自動轉義到下一臺服務器 $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; //恢復故障服務器 key值將恢復原來服務器 $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;
二、運行結果:
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
看完上述內容,你們掌握使用PHP怎么封裝一個一致性hash分布式算法的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。