/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罢了
以上就是PHP取模hash以及分歧性hash操作Memcached散布式集群的具体内容,更多请存眷资源魔其它相干文章!
标签: php hash php开发教程 php开发资料 php开发自学 Memcached
抱歉,评论功能暂时关闭!