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_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); }
再次執行php程式碼
查看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中文網其他相關文章!