Redis Cluster


Redis Cluster



Redis-Cluster簡介


什麼是Redis-Cluster

爲什麼要搭建Redis集羣。Redis是在內存中保存數據的,而咱們的電腦通常內存都不大,這也就意味着Redis不適合存儲大數據,適合存儲大數據的是Hadoop生態系統的Hbase或者是MogoDBRedis更適合處理高併發,一臺設備的存儲能力是頗有限的,可是多臺設備協同合做,就可讓內存增大不少倍,這就須要用到集羣。node

Redis集羣搭建的方式有多種,例如使用客戶端分片TwemproxyCodis等,但從redis 3.0以後版本支持redis-cluster集羣,它是Redis官方提出的解決方案Redis-Cluster採用無中心結構,每一個節點保存數據和整個集羣狀態,每一個節點都和其餘全部節點鏈接。其redis-cluster架構圖以下:linux



客戶端與 redis 節點直連,不須要中間 proxy .客戶端不須要鏈接集羣全部節點鏈接集羣中任何一個可用節點便可c++

全部的 redis 節點彼此互聯(PING-PONG 機制),內部使用二進制協議優化傳輸速度和帶寬.redis


分佈存儲機制-

1redis-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。它的靈感與特性來自於 PerlSmalltalkEiffelAda以及 Lisp 語言。由 Ruby 語言自己還發展出了JRubyJava平臺)、IronRuby.NET平臺)等其餘平臺的 Ruby 語言替代品Ruby的做者於1993224日開始編寫Ruby,直至199512月才正式公開發布fj新聞組)。由於Perl發音與6月誕生石pearl珍珠)相同,所以Ruby7月誕生石ruby紅寶石)命名

RubyGems簡稱gems,是一個用於對 Ruby組件進行打包的 Ruby 打包系統

3redis源碼包上傳到 linux 系統  ,解壓redis源碼包

4編譯redis源碼  ,進入redis源碼文件夾

make

看到如下輸出結果,表示編譯成功

 

5)建立目錄/usr/local/redis-cluster目錄,  安裝6redis實例,分別安裝在如下目錄

/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

 

出現此提示表示成功,按此方法安裝其他5redis實例

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)測試關閉700170027003 會發生什麼。


長按圖片識別二維碼關注


 CSDN博客地址



一個喜歡分享最新開發技術的公衆號,是我我的的公衆號,強烈推薦關注。


關注後回覆1001,領取一整套JAVA相關電子書籍。


本文分享自微信公衆號 - 一點博客(xiaochen_182)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索