初识Redis集群(Redis Cluster)

Redis Cluster(Redis集群)

一丶前言

​ Redis是在内存中保存数据的,而我们的电脑一般内存都不大,这也就意味着Redis不适合存储大数据,适合存储大数据的是Hadoop生态系统的Hbase或者是MogoDB。Redis更适合处理高并发,一台设备的存储能力是很有限的,但是多台设备协同合作,就可以让内存增大很多倍,这就需要用到集群。

​ Redis集群搭建的方式有多种,例如使用客户端分片、Twemproxy、Codis等,但从redis 3.0之后版本支持redis-cluster集群,它是Redis官方提出的解决方案,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。其redis-cluster架构图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cvgukhde-1583766383596)(C:\Users\86132\AppData\Roaming\Typora\typora-user-images\image-20200302194932390.png)]

图解:客户端与 redis 节点直连,不需要中间 proxy 层.客户端不需要连接集群所有节点连接集群中任何一个可用节点 即可。

​ 所有的 redis 节点彼此互联(PING-PONG 机制),内部使用二进制协议优化传输速度和带宽.

二丶分布存储机制-槽

  • redis-cluster 把所有的物理节点映射到[0-16383]slot 上,cluster 负责维护 node<->slot<->value

  • Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点。 例如三个节点:槽分布的值如下:

    SERVER1: 0-5460, SERVER2: 5461-10922, SERVER3: 10923-16383

三丶容错机制-投票

  • 选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作. 故障节点对应的从节点自动升级为主节点
  • 集群不可用前提:如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态.

四丶搭建Redis-Cluster

  • 前提:

    • 需要 6 台 redis 服务器。搭建伪集群。
    • 需要 6 个 redis 实例。
    • 需要运行在不同的端口 7001-7006
  • 准备工作:

    • 安装gcc:redis是c语言开发的,安装redis

      yum install gcc-c++
      
    • 安装ruby,我们需要使用ruby脚本实现集群搭建:

      yum install ruby
      yum install rubygems
      

      说明:Ruby,一种简单快捷的面向对象面向对象程序设计脚本语言,在20世纪90年代由日本人松本行弘([Yukihiro Matsumoto](https://baike.baidu.com/item/Yukihiro Matsumoto))开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 PerlSmalltalkEiffelAda以及 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 打包系统

    • 将redis源码包上传到linux系统,解压redis源码

      tar -zxvf ...
      
    • 进入redis源码文件夹,使用make命令编译redis源码

      make
      

      结果成功图:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6rOUoy7D-1583766383607)(C:\Users\86132\AppData\Roaming\Typora\typora-user-images\image-20200302203915402.png)]

    • 创建目录/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
      
    • 以第一个实例为例:命令如下:

      make install PREFIX=/usr/local/redis-cluster/redis-1
      

      结果成功图:(按此方法安装其余五个)[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VtxjyTq4-1583766383614)(C:\Users\86132\AppData\Roaming\Typora\typora-user-images\image-20200302204329176.png)]

    • 复制配置文件/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
      
  • 配置集群

    • 修改每个redis接点的配置文件夹redis.conf,修改运行端口为:7001…

    • 将cluster-enabled yes 前的注释去掉(632行)

    • 启动每个redis实例,以第一个为例:

      cd /usr/local/redis-cluster/redis-1/bin/
      ./redis-server redis.conf
      

      结果成功图:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZdkndH0G-1583766383618)(C:\Users\86132\AppData\Roaming\Typora\typora-user-images\image-20200302205127219.png)]

    • 其余五个同方法启动,并查看是否都完成启动:

      [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
      
    • 上传redis-3.0.0.gem

    • 安装 ruby用于搭建redis集群的脚本。

    [root@localhost ~]# gem install redis-3.0.0.gem

    
    * 使用ruby脚本搭建集群:进入redis源码目录中的src目录,执行以下命令:
    
    ~~~xml
    ./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
    

    说明:1为有一个从节点

    成功提示:

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

    说明:结果内部显示master,显示从节点等信息

五丶连接redis-cluster

  • 客户端工具连接

    redis-cli -p 主机ip -p 端口(集群中任意端口) -c
    

    说明:-c表示连接的是redis集群,不带参数-c表示连接的是单机

    测试值的存取:
    (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集群

    • redis属性配置文件:

      #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集群配置文件:

      <?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本地属性配置文件:

Redis settings

# server IP 

redis.host=127.0.0.1
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间(毫秒),则直接抛出JedisConnectionException;
redis.maxWait=3000
# 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true
~~~

* redis配置文件:

  ~~~ 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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:cache="http://www.springframework.org/schema/cache"
    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   
              http://www.springframework.org/schema/mvc   
              http://www.springframework.org/schema/mvc/spring-mvc.xsd 
              http://www.springframework.org/schema/cache  
              http://www.springframework.org/schema/cache/spring-cache.xsd">  
    
     <context:property-placeholder location="classpath*:properties/*.properties" />   
    
     <!-- redis 相关配置 --> 
     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
       <property name="maxIdle" value="${redis.maxIdle}" />   
       <property name="maxWaitMillis" value="${redis.maxWait}" />  
       <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
     </bean>  
    
     <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
         p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
     
     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
      	<property name="connectionFactory" ref="JedisConnectionFactory" />  
     </bean>  
        
  </beans>  
  ~~~

  提示:redisTemplate配置时引入JedisConnectionFactory,我们可以用maven profile对该id进行动态赋值

### 六丶模拟集群异常测试

客户端关闭接点的命令:

~~~ xml
./redis-cli -p 端口 shutdown 
~~~

.data.redis.core.RedisTemplate">

  </beans>  
  ~~~

  提示:redisTemplate配置时引入JedisConnectionFactory,我们可以用maven profile对该id进行动态赋值

### 六丶模拟集群异常测试

客户端关闭接点的命令:

~~~ xml
./redis-cli -p 端口 shutdown 
~~~

测试结论:当主节点挂掉,从节点会顶上,两个都挂掉,会瘫痪.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值