(1).安装redis.编译安装redis5.0
(2).创建6个Redis配置文件
6个配置文件不能在同一个目录,此处我们定义如下:
代码语言:javascript复制/www/server/redis/redis-cluster-conf/7001/redis.conf
/www/server/redis/redis-cluster-conf/7002/redis.conf
/www/server/redis/redis-cluster-conf/7003/redis.conf
/www/server/redis/redis-cluster-conf/7004/redis.conf
/www/server/redis/redis-cluster-conf/7005/redis.conf
/www/server/redis/redis-cluster-conf/7006/redis.conf
配置文件的内容:
代码语言:javascript复制bind 0.0.0.0
port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes.conf
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /var/run/redis_7001.pid
cluster-config-file nodes_7001.conf #
其中port、pidfile、cluster-config-file每个配置文件要递增数字
(4).启动全部节点,建议保存为sh文件执行
代码语言:javascript复制/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7001/redis.conf
/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7002/redis.conf
/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7003/redis.conf
/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7004/redis.conf
/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7005/redis.conf
/www/server/redis/src/redis-server /www/server/redis/redis-cluster-conf/7006/redis.conf
(5).启动集群
代码语言:javascript复制redis-cli --cluster create 192.168.13.125:7001 192.168.13.125:7002 192.168.13.125:7003 192.168.13.125:7004 192.168.13.125:7005 192.168.13.125:7006 --cluster-replicas 1
输出的log:
代码语言:javascript复制>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.13.125:7004 to 192.168.13.125:7001
Adding replica 192.168.13.125:7005 to 192.168.13.125:7002
Adding replica 192.168.13.125:7006 to 192.168.13.125:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 934700bc6ef9b59354952864b203fb242330815d 192.168.13.125:7001
slots:[0-5460] (5461 slots) master
M: 714005417369f83fbfb654a237f7aa665e88d016 192.168.13.125:7002
slots:[5461-10922] (5462 slots) master
M: 5bd69e526f3547858cb09781d1c948385d5f0927 192.168.13.125:7003
slots:[10923-16383] (5461 slots) master
S: 25125090e96a7b464f747b749e1ef3241eecaaa1 192.168.13.125:7004
replicates 714005417369f83fbfb654a237f7aa665e88d016
S: 0cf1689aea364023c78b6582faa8a16d29d037f3 192.168.13.125:7005
replicates 5bd69e526f3547858cb09781d1c948385d5f0927
S: 60026698155e403bb201e7212ea0b287342a44b2 192.168.13.125:7006
replicates 934700bc6ef9b59354952864b203fb242330815d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.13.125:7001)
M: 934700bc6ef9b59354952864b203fb242330815d 192.168.13.125:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 714005417369f83fbfb654a237f7aa665e88d016 192.168.13.125:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 0cf1689aea364023c78b6582faa8a16d29d037f3 192.168.13.125:7005
slots: (0 slots) slave
replicates 5bd69e526f3547858cb09781d1c948385d5f0927
M: 5bd69e526f3547858cb09781d1c948385d5f0927 192.168.13.125:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 60026698155e403bb201e7212ea0b287342a44b2 192.168.13.125:7006
slots: (0 slots) slave
replicates 934700bc6ef9b59354952864b203fb242330815d
S: 25125090e96a7b464f747b749e1ef3241eecaaa1 192.168.13.125:7004
slots: (0 slots) slave
replicates 714005417369f83fbfb654a237f7aa665e88d016
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
(6).php连接测试
代码语言:javascript复制<?php
/**
* Redis集群
* RedisCluster类介绍.网上资料不全.自己翻译水平有限.有错误请提出
* 参数1:用于通过名称加载集群配置,但是需要我们在redis.ini中提前配置好对应的名称和数据,例如redis.ini的配置如下:
* // redis.clusters.seeds = "mycluster[]=localhost:7000&test[]=localhost:7001"
* // redis.clusters.timeout = "mycluster=5"
* // redis.clusters.read_timeout = "mycluster=10"
* 那么直接new RedisCluster('mycluster')即可实例化,但是通常我们的配置是在php文件中,所以百度上面的文档第一个参数都是NULL
* 参数2:用于通过PHP数组来加载集群Host
* 参数3:连接多久算超时,单位是秒
* 参数4:读取多久算超时,单位是秒
* 参数5:是否开启持久连接
* 参数6:连接密码(文档有写,但是实际没有这个参数)
* 其他demo参考:https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#readme
*/
$config = [
'192.168.13.125:7001',
'192.168.13.125:7002',
'192.168.13.125:7003',
'192.168.13.125:7004',
'192.168.13.125:7005',
'192.168.13.125:7006',
];
$redisCluster = new RedisCluster(null, $config, 1.5, 1.5, true);
$redisCluster->set('username', 'chenhuohuo');
$value = $redisCluster->get('username');
var_dump($value);
解析:redis集群分为主从复制、哨兵模式、Cluster模式.通常使用cluster模式,本文也是使用的cluster模式。cluster模式不会同步数据,我们获取一个key是通过底层的算法自动去找对应节点来拿数据,所以不要用节点1设置key,节点2去获取key,是无法获取到的。