本文实例讲述了PHP一致性hash分布式算法封装类定义与用法。分享给大家供大家参考,具体如下:
一、无虚拟节点实现
<"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
PS:这里再为大家提供2款hash相关在线工具供大家参考使用:
在线散列/哈希算法加密工具:
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
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《PHP编码与转码操作技巧汇总》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php正则表达式用法总结》
希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com