一.redisCluster 简介
1.什么是redis-cluster?
为何要搭建 Redis 集群。Redis 是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着 Redis 不适合存储大数据,适合存储大数据的是 Hadoop 生态系统的 Hbase 或者是 MogoDB。Redis 更适合处理高并发,一台设备的存储能力是很有限的,但是多台设备协同合作,就可以让内存增大很多倍,这就需要用到集群。
Redis 集群搭建的方式有多种,例如使用客户端分片、Twemproxy、Codis 等,但从redis 3.0 之后版本支持 redis-cluster 集群,它是 Redis 官方提出的解决方案,Redis-Cluster 采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。其 redis-cluster 架构图如下:
客户端与 redis 节点直连,不需要中间 proxy 层.客户端不需要连接集群所有节点连接集群中任何一个可用节点即可。所有的 redis 节点彼此互联(PING-PONG 机制),内部使用二进制协议优化传输速度和带宽.
2.分布式机制---------槽
(1)redis-cluster 把所有的物理节点映射到[0-16383]slot 上,cluster 负责维护
node<->slot<->value
(2)Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。
例如三个节点:槽分布的值如下:
SERVER1: 0-5460
SERVER2: 5461-10922
SERVER3: 10923-16383
3.容错机制---------投票
这个选举和zookeeper集群搭建过程中的投票原理不一样,主要是redis中投票决定那个节点死去了.
(1)选举过程是集群中所有 master 参与,如果半数以上 master 节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作. 故障节点对应的从节点自动升级为主节点
(2)什么时候整个集群不可用(cluster_state:fail)?
如果集群任意 master 挂掉,且当前 master 没有 slave.集群进入 fail 状态,也可以理解成集群的 slot 映射[0-16383]不完成时进入 fail 状态.
二.搭建redis-cluster
1.搭建要求:
我们在这必须使用3.0以上的版本,因为只要3.0以上的才支持redis-cluster集群搭建
需要 6 台 redis 服务器。搭建伪集群。
需要 6 个 redis 实例。
需要运行在不同的端口 7001-7006
2.准备工作
(1)安装gcc
Redis 是 c 语言开发的。安装 redis 需要 c 语言的编译环境。如果没有 gcc 需要在线安装。
(2)使用 yum 命令安装 ruby (我们需要使用 ruby 脚本来实现集群搭建)
(3)将 redis 源码包上传到 linux 系统 ,解压 redis 源码包
(4)编译 redis 源码 ,进入 redis 源码文件夹
执行make
看到以下输出结果,表示编译成功
(5)创建目录/usr/local/redis-cluster 目录, 安装 6 个 redis 实例,分别安装在以下目录
/usr/local/redis-cluster/redis-1
/usr/local/redis-cluster/redis-2
/usr/local/redis-cluster/redis-3
/usr/local/redis-cluster/redis-4
/usr/local/redis-cluster/redis-5
/usr/local/redis-cluster/redis-6
以第一个 redis 实例为例,命令如下
make install PREFIX=/usr/local/redis-cluster/redis-1
一共安装六个
(6)复制配置文件 将 /redis-3.0.0/redis.conf 复制到 redis 下的 bin 目录下
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-1/bin
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-2/bin
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-3/bin
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-4/bin
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-5/bin
[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-6/bin
三.配置集群
(1)修改配置文件
(2)启动每个 redis 实例
cd /usr/local/redis-cluster/redis-1/bin/
./redis-server redis.conf
一次启动所有的服务
查看,是否所有的都启动了
(3)上传 redis-3.0.0.gem ,安装 ruby 用于搭建 redis 集群的脚本
gem install redis-3.0.0.gem
(4)使用 ruby 脚本搭建集群。
进入 redis 源码目录中的 src 目录 执行下面的命令
./redis-trib.rb create --replicas 1 192.168.72.133:7001 192.168.72.133:7002 192.168.72.133:7003 192.168.72.133:7004 192.168.72.133:7005 192.168.72.133:7006
安装完成
四.客户端连接redis-cluster集群
Redis-cli 连接集群:
redis-cli -p 主机 ip -p 端口(集群中任意端口) -c
-c:代表连接的是 redis 集群
测试值的存取:
(1)从本地连接到集群 redis 使用 7001 端口 加 -c 参数
(2)存入 name 值为 abc ,系统提示此值被存入到了 7002 端口所在的 redis (槽是 5798)
(3)提取 name 的值,可以提取。
(4)退出(quit)
(5)再次以 7001 端口进入 ,不带-c
(6)查询 name 值,无法获取,因为值在 7002 端口的 redis 上
(7)我们以 7002 端口进入,获取 name 值发现是可以获取的,而以其它端口进入均不能获
取
五 SpringDataRedis 连接 Redis 集群
applicationContext-redis-cluster.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true"
location="classpath:properties/redis-cluster-config.properties" />
<bean id="redis-clusterConfiguration"
class="org.springframework.data.redis.connection.redis-clusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<constructor-arg ref="redis-clusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory" />
</bean>
</beans>
添加属性文件 redis-cluster-config.properties
#cluster configuration
redis.host1=192.168.25.135
redis.port1=7001
redis.host2=192.168.25.135
redis.port2=7002
redis.host3=192.168.25.135
redis.port3=7003
redis.host4=192.168.25.135
redis.port4=7004
redis.host5=192.168.25.135
redis.port5=7005
redis.host5=192.168.25.135
redis.port5=7006
注意:端口号,要改为自己机器实际的端口号
六.模拟集群异常测试
关闭节点命令
./redis-cli -p 端口 shutdown
(1)测试关闭 7001 和 7004, 看看会发生什么。
(2)测试关闭 7001、7002、7003 会发生什么。