Successful instance of php operation redis cluster cluster

Keywords: PHP Redis Java github

java operation redis cluster cluster can use jredis

php can operate redis cluster cluster in two ways:

1. Use the phpredis extension, which is a c extension with higher performance. However, the phpredis2.x extension does not work. You need to upgrade phpredis to 3.0, but there are few references for this scheme

2. Use predis, pure PHP development, use namespace, need php5.3 +, 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';//Introducing predis related packages
//redis example
$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 and name3 are three key s, which are allocated to three slot s according to the algorithm, and may be allocated to three servers
First run predis.php to see 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 each server. You can kill several redis instances according to ps-ef | grep redis, and then see the effect

Posted by pacmon on Tue, 03 Dec 2019 00:57:52 -0800