本文参考 嗨客网 Redis面试题
Redis cluster 是 Redis 官方提供的分布式解决方案,在 3.0 版本后推出的,有效地解决了 Redis 分布式的需求,当一个 Redis 节点挂了可以快速的切换到另一个节点。当遇到单机内存、并发等瓶颈时,可以采用分布式方案要解决问题。
Redis 最开始使用 主从模式 做集群,若 master 宕机需要手动配置 slave 转为 master;后来为了高可用提出来哨兵模式,该模式下有一个哨兵监视 master 和 slave,若 master 宕机可自动将 slave 转为 master,但它也有一个问题,就是不能动态扩充;所以在 3.x 提出 cluster 集群模式。
尽管属于无中心化架构一类的分布式系统,但不同产品的细节实现和代码质量还是有不少差异的,就比如 Redis Cluster 有些地方的设计看起来就有一些 “奇葩” 和简陋:
Redis-cluster 集群的架构图如下图所示:
我们可以看到:
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。
Redis-cluster 投票架构图如下图所示:
也就是说:
yum -y install ruby
yum -y install rubygems
现在已经准备好了,6 份干净的 redis,如下所示:
[root@localhost redis-cluster]# pwd
/usr/local/redis/redis-cluster
[root@localhost redis-cluster]# ll
total 72
drwxr-xr-x 2 root root 4096 Nov 2 00:17 redis1
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis2
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis3
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis4
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis5
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis6
-rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
[root@localhost redis-cluster]#
将 redis 源文件 src 目录下的 redis-trib.rb 文件拷贝过来了。 redis-trib.rb 这个文件是 redis 集群的管理文件,ruby 脚本。我们将要设置的节点的 redis.conf 配置文件按照如下进行修改:
################################ GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port *
################################ REDIS CLUSTER ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes
端口号如果是同一台主机的话,必须不同。不同主机可以相同。我这里是使用一台主机,所以我将六个节点的端口号修改为 7001-7006。
cd redis1
./redis-server redis.conf
cd ..
cd redis2
./redis-server redis.conf
cd ..
cd redis3
./redis-server redis.conf
cd ..
cd redis4
./redis-server redis.conf
cd ..
cd redis5
./redis-server redis.conf
cd ..
cd redis6
./redis-server redis.conf
cd ..
./redis1/redis-cli -p 7001 shutdown
./redis1/redis-cli -p 7002 shutdown
./redis1/redis-cli -p 7003 shutdown
./redis1/redis-cli -p 7004 shutdown
./redis1/redis-cli -p 7005 shutdown
./redis1/redis-cli -p 7006 shutdown
两个脚本都放在如下所属目录:
[root@localhost redis-cluster]# pwd
/usr/local/redis/redis-cluster
[root@localhost redis-cluster]# ll
total 80
drwxr-xr-x 2 root root 4096 Nov 2 00:52 redis1
drwxr-xr-x 2 root root 4096 Nov 2 00:51 redis2
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis3
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis4
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis5
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis6
-rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
-rw-r--r-- 1 root root 252 Nov 2 00:55 start-all.sh
-rw-r--r-- 1 root root 216 Nov 2 00:57 stop-all.sh
[root@localhost redis-cluster]#
[root@localhost redis-cluster]# chmod -u+x start-all.sh stop-all.sh
[root@localhost redis-cluster]# ./start-all.sh
[root@localhost redis-cluster]# ps aux | grep redis
root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:53 ./redis-server *:6379 [cluster]
root 11924 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7001 [cluster]
root 11928 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7002 [cluster]
root 11932 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7003 [cluster]
root 11936 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7004 [cluster]
root 11940 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7005 [cluster]
root 11944 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7006 [cluster]
root 11948 0.0 0.0 4360 748 pts/2 S+ 01:01 0:00 grep redis
[root@localhost redis-cluster]#
[root@localhost redis-cluster]# pwd
/usr/local/redis/redis-cluster
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 192.168.37.131:7004 192.168.37.131:7005 192.168.37.131:7006
原文链接:链接
其他:目录
更多文章,可以关注下方公众号: