我將在同一臺(tái)Redis節(jié)點(diǎn)上創(chuàng)建三主三從Redis集群
創(chuàng)建并啟動(dòng)redis節(jié)點(diǎn)
- 在redis文件夾下創(chuàng)建8000 ~8005總共6個(gè)文件夾,作為存放redis配置文件的目錄
cd redis-5.0.4
mkdir 8000 8001 8002 8003 8004 8005
- 將redis目錄下的redis.conf復(fù)制到各個(gè)節(jié)點(diǎn)目錄下
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8000/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8001/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8002/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8003/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8004/
[root@bigdata24 redis-5.0.4]$ cp redis.conf /redis/8005/
- 修改配置文件,以8000端口為例
protected-mode no
port 8000
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 5000
daemonize yes
pidfile /var/run/redis_8000.pid
logfile "8000.log"
dir /redis/data
bind 127.0.0.1
- 啟動(dòng)各個(gè)redis節(jié)點(diǎn)
[root@bigdata24 redis-5.0.4]$ src/redis-server 8000/redis.conf
[root@bigdata24 redis-5.0.4]$ src/redis-server 8001/redis.conf
[root@bigdata24 redis-5.0.4]$ src/redis-server 8002/redis.conf
[root@bigdata24 redis-5.0.4]$ src/redis-server 8003/redis.conf
[root@bigdata24 redis-5.0.4]$ src/redis-server 8004/redis.conf
[root@bigdata24 redis-5.0.4]$ src/redis-server 8005/redis.conf
- 查看是否正常啟動(dòng)
[root@bigdata24 redis-5.0.4]$ ps -ef|grep redis
root 3778 1 0 14:20 ? 00:00:00 src/redis-server 127.0.0.1:8000 [cluster]
root 3819 1 0 14:21 ? 00:00:00 src/redis-server 127.0.0.1:8001 [cluster]
root 3827 1 0 14:21 ? 00:00:00 src/redis-server 127.0.0.1:8002 [cluster]
root 3834 1 0 14:21 ? 00:00:00 src/redis-server 127.0.0.1:8003 [cluster]
root 3842 1 0 14:21 ? 00:00:00 src/redis-server 127.0.0.1:8004 [cluster]
root 3849 1 0 14:21 ? 00:00:00 src/redis-server 127.0.0.1:8005 [cluster]
root 3858 1598 0 14:21 pts/1 00:00:00 grep --color=auto redis
啟動(dòng)集群
Redis5.0集群管理工具redis-trib.rb已經(jīng)被棄用,所以不用安裝ruby等組件,上面安裝ruby的方法針對(duì)于redis5.0以下使用。redis-trib.rb的功能,現(xiàn)在已經(jīng)集成到了redis-cli中
- 通過(guò)以下語(yǔ)句創(chuàng)建集群
redis-cli --cluster create 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 --cluster-replicas 1
[root@bigdata24 src]$ redis-cli --cluster create 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:8004 to 127.0.0.1:8000
Adding replica 127.0.0.1:8005 to 127.0.0.1:8001
Adding replica 127.0.0.1:8003 to 127.0.0.1:8002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 127.0.0.1:8000
slots:[0-5460] (5461 slots) master
M: 860d39006e21ba74052627e899f307d0cfbec0b5 127.0.0.1:8001
slots:[5461-10922] (5462 slots) master
M: d5882b089f975713c94dcbc1537c11428d5a9e1a 127.0.0.1:8002
slots:[10923-16383] (5461 slots) master
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 127.0.0.1:8003
replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
S: fd6db44a16e12d6dc00a70d02472d07a69917acd 127.0.0.1:8004
replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
S: 93de823d19aa2879c283e47e02a7b0a33ff0e0a3 127.0.0.1:8005
replicates 860d39006e21ba74052627e899f307d0cfbec0b5
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 127.0.0.1:8000)
M: 17e2fc02eb1e9961fa36af210397ed8dc1dc360d 127.0.0.1:8000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3606713658cc3cdf7e1c6456d94143b91c7da928 127.0.0.1:8003
slots: (0 slots) slave
replicates d5882b089f975713c94dcbc1537c11428d5a9e1a
M: d5882b089f975713c94dcbc1537c11428d5a9e1a 127.0.0.1:8002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 860d39006e21ba74052627e899f307d0cfbec0b5 127.0.0.1:8001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 93de823d19aa2879c283e47e02a7b0a33ff0e0a3 127.0.0.1:8005
slots: (0 slots) slave
replicates 860d39006e21ba74052627e899f307d0cfbec0b5
S: fd6db44a16e12d6dc00a70d02472d07a69917acd 127.0.0.1:8004
slots: (0 slots) slave
replicates 17e2fc02eb1e9961fa36af210397ed8dc1dc360d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
- 通過(guò)以下語(yǔ)句連接到某個(gè)節(jié)點(diǎn)
redis-cli -h bigdata24 -p 8000
使用java連接Redis集群
public class JedisClusterUtil {
private static volatile JedisCluster cluster;
private JedisClusterUtil() {}
public static JedisCluster getInstance() {
if (cluster == null) {
synchronized (JedisClusterUtil.class) {
if (cluster == null) {
Set<HostAndPort> nodes = new LinkedHashSet<>();
nodes.add(new HostAndPort("bigdata24", 8000));
nodes.add(new HostAndPort("bigdata24", 8001));
nodes.add(new HostAndPort("bigdata24", 8002));
nodes.add(new HostAndPort("bigdata24", 8003));
nodes.add(new HostAndPort("bigdata24", 8004));
nodes.add(new HostAndPort("bigdata24", 8005));
cluster = new JedisCluster(nodes);
return cluster;
}
}
}
return cluster;
}
}
遇到的問(wèn)題
當(dāng)我修改redis配置的時(shí)候,kill掉了所有的redis節(jié)點(diǎn),cluster宕掉了,這個(gè)時(shí)候重新創(chuàng)建集群失敗
[ERR] Node bigdata24:8000 is not empty.
Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
需要?jiǎng)h除掉redis根目錄下data以及src目錄下的dump.rdb以及nodes-8003.conf
確認(rèn)刪除干凈后一定要重啟各個(gè)redis節(jié)點(diǎn)
之后還遇到一個(gè)問(wèn)題
Node bigdata24:8001 replied with error:
ERR Invalid node address specified: bigdata24:8000
這是因?yàn)槲以谂渲胷edis.conf的時(shí)候使用了如下語(yǔ)句
bind bigdata24
Redis并不能識(shí)別到到IP映射 所以報(bào)錯(cuò) 將其改為IP之后重啟節(jié)點(diǎn)再創(chuàng)建集群就成功了