Redis Cluster
Redis-Cluster简介
什么是Redis-Cluster
为什么要搭建Redis集群。Redis是在内存中保存数据的,而咱们的电脑通常内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB。Redis更适合处理高并发,一台设备的存储能力是颇有限的,可是多台设备协同合做,就可让内存增大不少倍,这就须要用到集群。node
Redis集群搭建的方式有多种,例如使用客户端分片、Twemproxy、Codis等,但从redis 3.0以后版本支持redis-cluster集群,它是Redis官方提出的解决方案,Redis-Cluster采用无中心结构,每一个节点保存数据和整个集群状态,每一个节点都和其余全部节点链接。其redis-cluster架构图以下:linux
客户端与 redis 节点直连,不须要中间 proxy 层.客户端不须要链接集群全部节点链接集群中任何一个可用节点便可。c++
全部的 redis 节点彼此互联(PING-PONG 机制),内部使用二进制协议优化传输速度和带宽.redis
分布存储机制-槽
(1)redis-cluster 把全部的物理节点映射到[0-16383]slot 上,cluster 负责维护算法
node<->slot<->valuespring
(2)Redis 集群中内置了 16384 个哈希槽,当须要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,而后把结果对 16384 求余数,这样每一个key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大体均等的将哈希槽映射到不一样的节点。ruby
例如三个节点:槽分布的值以下:服务器
SERVER1: 0-5460微信
SERVER2: 5461-10922架构
SERVER3: 10923-16383
容错机制-投票
(1)选举过程是集群中全部master参与,若是半数以上master节点与故障节点通讯超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操做. 故障节点对应的从节点自动升级为主节点
(2)何时整个集群不可用(cluster_state:fail)?
若是集群任意master挂掉,且当前master没有slave.集群进入fail状态,也能够理解成集群的slot映射[0-16383]不完成时进入fail状态.
2搭建Redis-Cluster
1搭建要求
须要 6 台 redis 服务器。搭建伪集群。
须要 6 个 redis 实例。
须要运行在不一样的端口 7001-7006
2准备工做
(1)安装gcc 【此步省略】
Redis 是 c 语言开发的。安装 redis 须要 c 语言的编译环境。若是没有 gcc 须要在线安装。
yum install gcc-c++ |
(2)使用yum命令安装 ruby (咱们须要使用ruby脚原本实现集群搭建)【此步省略】
yum install ruby yum install rubygems |
----- 知识点小贴士 ----- Ruby,一种简单快捷的面向对象(面向对象程序设计)脚本语言,在20世纪90年代由日本人松本行弘(Yukihiro Matsumoto)开发,遵照GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。由 Ruby 语言自己还发展出了JRuby(Java平台)、IronRuby(.NET平台)等其余平台的 Ruby 语言替代品。Ruby的做者于1993年2月24日开始编写Ruby,直至1995年12月才正式公开发布于fj(新闻组)。由于Perl发音与6月诞生石pearl(珍珠)相同,所以Ruby以7月诞生石ruby(红宝石)命名 RubyGems简称gems,是一个用于对 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 |
出现此提示表示成功,按此方法安装其他5个redis实例
(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 |
3配置集群
(1)修改每一个redis节点的配置文件redis.conf
修改运行端口为7001 (7002 7003 .....)
将cluster-enabled yes 前的注释去掉(632行)
(2)启动每一个redis实例
以第一个实例为例,命令以下
cd /usr/local/redis-cluster/redis-1/bin/ ./redis-server redis.conf |
把其他的5个也启动起来,而后查看一下是否是都启动起来了
[root@localhost ~]# ps -ef | grep redis root 15776 15775 0 08:19 pts/1 00:00:00 ./redis-server *:7001 [cluster] root 15810 15784 0 08:22 pts/2 00:00:00 ./redis-server *:7002 [cluster] root 15831 15813 0 08:23 pts/3 00:00:00 ./redis-server *:7003 [cluster] root 15852 15834 0 08:23 pts/4 00:00:00 ./redis-server *:7004 [cluster] root 15872 15856 0 08:24 pts/5 00:00:00 ./redis-server *:7005 [cluster] root 15891 15875 0 08:24 pts/6 00:00:00 ./redis-server *:7006 [cluster] root 15926 15895 0 08:24 pts/7 00:00:00 grep redis |
(3)上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本。
[root@localhost ~]# gem install redis-3.0.0.gem Successfully installed redis-3.0.0 1 gem installed Installing ri documentation for redis-3.0.0... Installing RDoc documentation for redis-3.0.0... |
(4)使用 ruby 脚本搭建集群。
进入redis源码目录中的src目录 执行下面的命令
./redis-trib.rb create --replicas 1 192.168.25.140:7001 192.168.25.140:7002 192.168.25.140:7003 192.168.25.140:7004 192.168.25.140:7005 192.168.25.140:7006 |
出现下列提示信息
>>> Creating cluster Connecting to node 192.168.25.140:7001: OK Connecting to node 192.168.25.140:7002: OK Connecting to node 192.168.25.140:7003: OK Connecting to node 192.168.25.140:7004: OK Connecting to node 192.168.25.140:7005: OK Connecting to node 192.168.25.140:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.140:7001 192.168.25.140:7002 192.168.25.140:7003 Adding replica 192.168.25.140:7004 to 192.168.25.140:7001 Adding replica 192.168.25.140:7005 to 192.168.25.140:7002 Adding replica 192.168.25.140:7006 to 192.168.25.140:7003 M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001 slots:0-5460 (5461 slots) master M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002 slots:5461-10922 (5462 slots) master M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003 slots:10923-16383 (5461 slots) master S: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004 replicates 1800237a743c2aa918ade045a28128448c6ce689 S: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005 replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d S: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006 replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 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.25.140:7001) M: 1800237a743c2aa918ade045a28128448c6ce689 192.168.25.140:7001 slots:0-5460 (5461 slots) master M: 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d 192.168.25.140:7002 slots:5461-10922 (5462 slots) master M: 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 192.168.25.140:7003 slots:10923-16383 (5461 slots) master M: c2a39a94b5f41532cd83bf6643e98fc277c2f441 192.168.25.140:7004 slots: (0 slots) master replicates 1800237a743c2aa918ade045a28128448c6ce689 M: b0e38d80273515c84b1a01820d8ecee04547d776 192.168.25.140:7005 slots: (0 slots) master replicates 7cb3f7d5c60bfbd3ab28800f8fd3bf6de005bf0d M: 03bf6bd7e3e6eece5a02043224497c2c8e185132 192.168.25.140:7006 slots: (0 slots) master replicates 436e88ec323a2f8bb08bf09f7df07cc7909fcf81 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. |
链接Redis-Cluster
1客户端工具链接
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值发现是能够获取的,而以其它端口进入均不能获取
2 SpringDataRedis链接Redis集群
修改品优购工程 在pinyougou-common工程添加spring 配置文件
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.140 redis.port1=7001
redis.host2=192.168.25.140 redis.port2=7002
redis.host3=192.168.25.140 redis.port3=7003
redis.host4=192.168.25.140 redis.port4=7004
redis.host5=192.168.25.140 redis.port5=7005
redis.host6=192.168.25.140 redis.port6=7006
redis.maxRedirects=3 redis.maxIdle=100 redis.maxTotal=600 |
模拟集群异常测试
关闭节点命令
./redis-cli -p 端口 shutdown |
(1)测试关闭7001 和7004, 看看会发生什么。
(2)测试关闭7001、7002、7003 会发生什么。
长按图片识别二维码关注
CSDN博客地址
一个喜欢分享最新开发技术的公众号,是我我的的公众号,强烈推荐关注。
关注后回复1001,领取一整套JAVA相关电子书籍。
本文分享自微信公众号 - 一点博客(xiaochen_182)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。