An example of successful operation of redis cluster in PHP

不言
Release: 2023-04-04 17:16:02
forward
2796 people have browsed it

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 &#39;predis/autoload.php&#39;;//引入predis相关包
//redis实例
$servers = array(
    &#39;tcp://192.168.1.198:7000&#39;,
    &#39;tcp://192.168.1.198:7001&#39;,
    &#39;tcp://192.168.1.198:7002&#39;,
    &#39;tcp://192.168.1.199:7003&#39;,
    &#39;tcp://192.168.1.199:7004&#39;,
    &#39;tcp://192.168.1.199:7005&#39;,
);
 
$client = new Predis\Client($servers, array(&#39;cluster&#39; => '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;
Copy after login

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:


An example of successful operation of redis cluster in PHPThen 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>
Copy after login

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>
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template