hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - PHP教程 - 正文 请牢记本站网址www.sosophp.cn

PHP取模hash和一致性hash操纵Memcached分布式集群【php教程】

2019-12-01PHP教程搜搜PHP网62°c
A+ A-
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罢了

以上就是PHP取模hash和一致性hash操纵Memcached散布式集群的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
PHP取模hash和一致性hash操纵Memcached分布式集群【php教程】

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签: