亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

php操作redis cluster集群成功的實例講解

來源:不言 發(fā)布時間:2019-01-10 10:56:19 閱讀量:971

本篇文章給大家?guī)淼膬?nèi)容是關(guān)于php操作redis cluster集群成功的實例講解,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

java操作redis cluster集群可使用jredis

php要操作redis cluster集群有兩種方式:

1、使用phpredis擴展,這是個c擴展,性能更高,但是phpredis2.x擴展不行,需升級phpredis到3.0,但這個方案參考資料很少

2、使用predis,純php開發(fā),使用了命名空間,需要php5.3+,靈活性高

我用的是predis,下載地址https://github.com/nrk/predis...

下載好后重命名為predis,

server1:192.168.1.198
server2:192.168.1.199

predis.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<?php

require 'predis/autoload.php';//引入predis相關(guān)包

//redis實例

$servers = array(

    'tcp://192.168.1.198:7000',

    'tcp://192.168.1.198:7001',

    'tcp://192.168.1.198:7002',

    'tcp://192.168.1.199:7003',

    'tcp://192.168.1.199:7004',

    'tcp://192.168.1.199:7005',

);

  

$client = new Predis\Client($servers, array('cluster' => 'redis'));

  

$client->set("name1", "11");

$client->set("name2", "22");

$client->set("name3", "33");

  

$name1 = $client->get('name1');

$name2 = $client->get('name2');

$name3 = $client->get('name3');

var_dump($name1, $name2, $name3);die;

name1,name2,name3是3個key,按照算法分配到3個slot上,有可能分到3臺服務(wù)器上
首先運行predis.php查看結(jié)果:

2378044598-5c34862341fcd_articlex.png

然后登錄到redis客戶端進行集群驗證:

server1

1

2

3

4

5

6

7

8

9

10

[root@localhost src]# redis-cli -c -p 7000

127.0.0.1:7000> get name1

-> Redirected to slot [12933] located at 192.168.1.199:7004

"11"

192.168.1.199:7004> get name2

-> Redirected to slot [742] located at 192.168.1.199:7003

"22"

192.168.1.199:7003> get name3

"33"

192.168.1.199:7003>

server2

1

2

3

4

5

6

7

8

9

10

[root@localhost src]# redis-cli -c -p 7003

127.0.0.1:7003> get name1

-> Redirected to slot [12933] located at 192.168.1.199:7004

"11"

192.168.1.199:7004> get name2

-> Redirected to slot [742] located at 192.168.1.199:7003

"22"

192.168.1.199:7003> get name3

"33"

192.168.1.199:7003>

可以看到數(shù)據(jù)分布在各個服務(wù)器上,可以根據(jù)ps -ef | grep redis,殺掉其中幾個redis實例,再看效果


標(biāo)簽: PHP
分享:
評論:
你還沒有登錄,請先