Redis5.0.4集群模式搭建

我将在同一台Redis节点上创建三主三从Redis集群

创建并启动redis节点

  1. 在redis文件夹下创建8000 ~8005总共6个文件夹,作为存放redis配置文件的目录
cd redis-5.0.4
mkdir 8000 8001 8002 8003 8004 8005
  1. 将redis目录下的redis.conf复制到各个节点目录下
[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/
  1. 修改配置文件,以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
  1. 启动各个redis节点
[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 
  1. 查看是否正常启动
[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

启动集群

Redis5.0集群管理工具redis-trib.rb已经被弃用,所以不用安装ruby等组件,上面安装ruby的方法针对于redis5.0以下使用。redis-trib.rb的功能,现在已经集成到了redis-cli中

  1. 通过以下语句创建集群

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.
  1. 通过以下语句连接到某个节点
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 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;
        }
}
遇到的问题

当我修改redis配置的时候,kill掉了所有的redis节点,cluster宕掉了,这个时候重新创建集群失败

[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.

需要删除掉redis根目录下data以及src目录下的dump.rdb以及nodes-8003.conf
确认删除干净后一定要重启各个redis节点

之后还遇到一个问题

Node bigdata24:8001 replied with error:
ERR Invalid node address specified: bigdata24:8000

这是因为我在配置redis.conf的时候使用了如下语句

bind bigdata24

Redis并不能识别到到IP映射 所以报错 将其改为IP之后重启节点再创建集群就成功了

你可能感兴趣的:(Redis5.0.4集群模式搭建)