Redis Cluster 集群搭建

什么是Redis-Cluster

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

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

 

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

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

 

分布存储机制-

 

1redis-cluster 把所有的物理节点映射到[0-16383]slot ,cluster 负责维护

 

node<->slot<->value

 

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

 

Redis可以无限扩展集群吗?当然不行,Redis最多集群16383台节点,只有这1万多个节点,够我们用吗?

 

号称全球最大的Redis集群维护商,新浪微博的工程师们曾经在多个公开场合都讲到过,微博平台当前在使用并维护着可能是世界上最大的Redis集群,其中最大的一个业务,单个业务使用了超过 10T 的内存,这里说的就是微博关系服务。

 

全球最大的Redis集群才用了10T数据,换算一下,按现在普通服务器都有50GB左右内存的机器,10T也就相当于: (10 * 1024)/50 =204.8,差不多205台机器。

 

容错机制-投票

 

1选举过程是集群中所有master参与,如果半数以上master节点与故障节点通信超过(cluster-node-timeout),认为该节点故障,自动触发故障转移操作.  故障节点对应的从节点自动升级为主节点

 

2什么时候整个集群不可用(cluster_state:fail)? 

 

如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态

 

搭建要求

 

需要 6 redis 服务器。搭建伪集群。

 

需要 6 redis 实例。

 

配置集群

 

(1)修改每个redis节点的配置文件redis.conf

 

启动后台启动

 

 

开启集群

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

 

启动每个redis实例

 

上传redis-3.0.0.gem 安装 ruby用于搭建redis集群的脚本。

 

(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

注:后面IP 端口号 写自己节点的

 

SpringDataRedis连接Redis集群

工程添加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">  
  
   <!-- 加载配置属性文件 按需加载 -->  
   <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">  
      <property name="maxRedirects" value="${redis.maxRedirects}"></property>  
      <property name="clusterNodes">  
         <set>  
            <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
               <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.RedisClusterNode">  
               <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.RedisClusterNode">  
               <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.RedisClusterNode">  
               <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.RedisClusterNode">  
               <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.RedisClusterNode">  
               <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_pro" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  >  
      <constructor-arg ref="redisClusterConfiguration" />  
      <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.128
redis.port1=7001

redis.host2=192.168.25.128
redis.port2=7002

redis.host3=192.168.25.128
redis.port3=7003

redis.host4=192.168.25.128
redis.port4=7004

redis.host5=192.168.25.128
redis.port5=7005

redis.host6=192.168.25.128
redis.port6=7006

redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600

<?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连接方式
&lt;!&ndash; redis 相关配置 &ndash;&gt;
<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="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<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.RedisClusterNode">
<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.RedisClusterNode">
<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.RedisClusterNode">
<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.RedisClusterNode">
<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.RedisClusterNode">
<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="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>


<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>

<!-- 但是RedisTemplate的key和value都将采用JDK序列化 这样就会出现采用不同template保存的数据不能用同一个template删除的问题 -->
<!--<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
&lt;!&ndash;使用字符串方式反序列化 &ndash;&gt;
<bean id="stringHashRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
<property name="hashValueSerializer" ref="stringRedisSerializer" />
</bean>-->

<!--事务RedisTemplate对象-->
<bean id="transRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
<!--开启事务-->
<property name="enableTransactionSupport" value="true" />
</bean>

</beans>

 

posted @ 2019-08-08 15:21  HankPeng  阅读(633)  评论(0编辑  收藏  举报