Home >Backend Development >PHP Tutorial >An example of successful operation of redis cluster in PHP
The content of this article is about the successful example of PHP operating redis cluster. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
java can use jredis to operate redis cluster cluster
php There are two ways to operate redis cluster cluster:
1. Use phpredis extension, which is a c extension , higher performance, but phpredis2. High flexibility
I use predis, download address https://github.com/nrk/predis...
After downloading, rename it to predis,
server1:192.168.1.198
server2:192.168.1.199
predis.php
<?php require 'predis/autoload.php';//引入predis相关包 //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 are 3 keys, which are allocated to 3 slots according to the algorithm. There are May be divided into 3 servers
First run predis.php to view the results:Then log in to the redis client for cluster verification:
server1
[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
[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>
You can see that the data is distributed on various servers. You can kill several of the redis instances according to ps -ef | grep redis, and then see Effect
The above is the detailed content of An example of successful operation of redis cluster in PHP. For more information, please follow other related articles on the PHP Chinese website!