Rumah > Artikel > pembangunan bahagian belakang > PHP取模hash和一致性hash操作Memcached分布式集群
1.开启4个Memcached服务模拟集群
/usr/local/memcached/bin/memcached -d -p 11211 -u memcached -vv >> /var/log/memcached.11211.log 2>&1 /usr/local/memcached/bin/memcached -d -p 11212 -u memcached -vv >> /var/log/memcached.11212.log 2>&1 /usr/local/memcached/bin/memcached -d -p 11213 -u memcached -vv >> /var/log/memcached.11213.log 2>&1 /usr/local/memcached/bin/memcached -d -p 11214 -u memcached -vv >> /var/log/memcached.11214.log 2>&1
2.取模hash算法
php代码
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/28 * Time: 11:38 */ $memcached = new Memcached(); //设置算法为取模hash $memcached->setOptions( array( Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_MODULA, //Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_REMOVE_FAILED_SERVERS=> true, ) ); //添加服务器 $memcached->addServer('192.168.75.132', '11211'); $memcached->addServer('192.168.75.132', '11212'); $memcached->addServer('192.168.75.132', '11213'); $memcached->addServer('192.168.75.132', '11214'); //写入12个key for ($i =1;$i <= 12;$i++){ $memcached->set('key_'.$i, 'value_'.$i); }
执行上述代码,查看log
#memcached.11211.log <28 new auto-negotiating client connection 28: Client using the ascii protocol <28 set key_2 0 0 7 >28 STORED <28 set key_3 0 0 7 >28 STORED <28 set key_4 0 0 7 >28 STORED <28 set key_10 0 0 8 >28 STORED <28 quit <28 connection closed. #memcached.11212.log <28 new auto-negotiating client connection 28: Client using the ascii protocol <28 set key_1 0 0 7 >28 STORED <28 set key_6 0 0 7 >28 STORED <28 set key_9 0 0 7 >28 STORED <28 set key_12 0 0 8 >28 STORED <28 quit <28 connection closed. #memcached.11213.log <28 new auto-negotiating client connection 28: Client using the ascii protocol <28 set key_7 0 0 7 >28 STORED <28 set key_8 0 0 7 >28 STORED <28 quit <28 connection closed. #memcached.11214.log <28 new auto-negotiating client connection 28: Client using the ascii protocol <28 set key_5 0 0 7 >28 STORED <28 set key_11 0 0 8 >28 STORED <28 quit <28 connection closed.
查看key的分布
注释掉php代码中的11214//$memcached->addServer('192.168.75.132', '11214');
再次执行php代码
查看key的分布
对比两次key的分布:
key_2和key_10命中没有变动,始终在11211中,其他10个key因为服务器的减少命中发生变化
3.一致性hash算法
php代码
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/28 * Time: 11:38 */ $memcached = new Memcached(); //设置算法为一致性hash $memcached->setOptions( array( Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_REMOVE_FAILED_SERVERS=> true, ) ); //添加服务器 $memcached->addServer('192.168.75.132', '11211'); $memcached->addServer('192.168.75.132', '11212'); $memcached->addServer('192.168.75.132', '11213'); $memcached->addServer('192.168.75.132', '11214'); //写入12个key for ($i =1;$i <= 12;$i++){ $ret = $memcached->set('key_'.$i, 'value_'.$i); }
执行上述代码,查看log
查看key的分布
注释掉php代码中的11214//$memcached->addServer('192.168.75.132', '11214');
再次执行php代码
查看key的分布
对比两次key的分布:
11211原有的key命中没有发生变化,新增了key_4
11212原有的key命中没有发生变化
11213原有的key命中没有发生变化,新增了key_12
有2个key因为服务器的减少命中发生变化
4.对比
取模hash算法减少一台服务器有10个key命中发生了变化。
一致性hash算法减少一台服务器2个key命中发生了变化。
这里只测试了12个key,模拟的数据量太小导致key分布不均匀,但服务器减少导致key命中发生变化和模拟数据量大小无关,而是和hash算法有关,这些测试体现了一致性hash算法的优势,取模hash因为服务器的减少导致大量key的取模结果发生变化,命中的服务器也发生了变化;而一致性hash算法key是固定在一个有2^32-1个节点的hash环上,服务器减少key在hash环上的位置不会发生变化,仅仅影响减少的那台服务器上key的命中,增加服务器也仅仅影响hash环上下一个位置服务器的部分key而已
Atas ialah kandungan terperinci PHP取模hash和一致性hash操作Memcached分布式集群. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!