Redis Cluster

1 Redis-Cluster簡介

1.1 什麼是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.2分佈存儲機制-槽

    (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.3容錯機制-投票

  (1)選舉過程是集羣中全部master參與,若是半數以上master節點與故障節點通訊超過(cluster-node-timeout),認爲該節點故障,自動觸發故障轉移操做.  故障節點對應的從節點自動升級爲主節點

  (2)何時整個集羣不可用(cluster_state:fail)? 

      若是集羣任意master掛掉,且當前master沒有slave.集羣進入fail狀態,也能夠理解成集羣的slot映射[0-16383]不完成時進入fail狀態. 

                  

 2搭建Redis-Cluster

2.1搭建要求

    須要 6 臺 redis 服務器。搭建僞集羣。

    須要 6 個 redis 實例。  

    須要運行在不一樣的端口 7001-7006

2.2準備工做

    (1)安裝gcc  

        Redis 是 c 語言開發的。安裝 redis 須要 c 語言的編譯環境。若是沒有 gcc 須要在線安裝。

yum install gcc-c++

    (2)使用yum命令安裝 ruby  (咱們須要使用ruby腳原本實現集羣搭建)【此步省略】

yum install ruby
yum install rubygems

    (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

2.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個也啓動起來,而後查看一下是否是都啓動起來了

      

 

 

  (3)上傳redis-3.0.0.gem ,安裝 ruby用於搭建redis集羣的腳本。(alt+p) 

 put h:/linux_upload/redis-3.0.0.gem

  執行下面命令

gem install redis-3.0.0.gem

  (4)使用 ruby 腳本搭建集羣。

    進入redis源碼目錄中的src目錄  執行下面的命令

./redis-trib.rb create --replicas 1 192.168.40.128:7001 192.168.40.128:7002 192.168.40.128:7003 192.168.40.128:7004 192.168.40.128:7005 192.168.40.128:7006

  結果以下:

  

>>> Creating cluster
Connecting to node 192.168.40.128:7001: OK           //檢測鏈接
Connecting to node 192.168.40.128:7002: OK
Connecting to node 192.168.40.128:7003: OK
Connecting to node 192.168.40.128:7004: OK
Connecting to node 192.168.40.128:7005: OK
Connecting to node 192.168.40.128:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.40.128:7001
192.168.40.128:7002
192.168.40.128:7003
Adding replica 192.168.40.128:7004 to 192.168.40.128:7001    //將4做爲1的從節點
Adding replica 192.168.40.128:7005 to 192.168.40.128:7002
Adding replica 192.168.40.128:7006 to 192.168.40.128:7003
M: 35bc27cc56f03bc5a780eaaf6d8dbb2ef99baa50 192.168.40.128:7001
   slots:0-5460 (5461 slots) master                //槽
M: ef7dcb063e3c941b666a78c8cd44721d526a5916 192.168.40.128:7002
   slots:5461-10922 (5462 slots) master
M: ea883ab716e0b194ca6a2dbf0d64137f3af0cb18 192.168.40.128:7003
   slots:10923-16383 (5461 slots) master
S: bf166b59899f586207a056d5c7e06131a34957b4 192.168.40.128:7004
   replicates 35bc27cc56f03bc5a780eaaf6d8dbb2ef99baa50
S: b301e5cf266d03d4582a316a5572eb49e5af6f93 192.168.40.128:7005
   replicates ef7dcb063e3c941b666a78c8cd44721d526a5916
S: a5243ab9438029660e0d9890d501457310681395 192.168.40.128:7006
   replicates ea883ab716e0b194ca6a2dbf0d64137f3af0cb18
Can I set the above configuration? (type 'yes' to accept): yes     //注意選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.40.128:7001)
M: 35bc27cc56f03bc5a780eaaf6d8dbb2ef99baa50 192.168.40.128:7001
   slots:0-5460 (5461 slots) master
M: ef7dcb063e3c941b666a78c8cd44721d526a5916 192.168.40.128:7002
   slots:5461-10922 (5462 slots) master
M: ea883ab716e0b194ca6a2dbf0d64137f3af0cb18 192.168.40.128:7003
   slots:10923-16383 (5461 slots) master
M: bf166b59899f586207a056d5c7e06131a34957b4 192.168.40.128:7004
   slots: (0 slots) master
   replicates 35bc27cc56f03bc5a780eaaf6d8dbb2ef99baa50
M: b301e5cf266d03d4582a316a5572eb49e5af6f93 192.168.40.128:7005
   slots: (0 slots) master
   replicates ef7dcb063e3c941b666a78c8cd44721d526a5916
M: a5243ab9438029660e0d9890d501457310681395 192.168.40.128:7006
   slots: (0 slots) master
   replicates ea883ab716e0b194ca6a2dbf0d64137f3af0cb18
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

    搭建成功....

3鏈接Redis-Cluster

3.1客戶端工具鏈接

    Redis-cli 鏈接集羣:

redis-cli -p 主機ip -p 端口(集羣中任意端口) -c
redis-cli -p 192.168.40.128 -p 7001 -c

4SpringDataRedis鏈接Redis集羣

  1建立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="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" 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>

  2建立redis-cluster-config.properties

#cluster configuration
redis.host1=192.168.40.128
redis.port1=7001

redis.host2=192.168.40.128
redis.port2=7002

redis.host3=192.168.40.128
redis.port3=7003

redis.host4=192.168.40.128
redis.port4=7004

redis.host5=192.168.40.128
redis.port5=7005

redis.host6=192.168.40.128
redis.port6=7006

redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600
相關文章
相關標籤/搜索