Redis Cluster
1.1 什麼是Redis-Cluster
為何要搭建Redis叢集。Redis是在記憶體中儲存資料的,而我們的電腦一般記憶體都不大,這也就意味着Redis不适合存儲大資料,适合存儲大資料的是Hadoop生态系統的Hbase或者是MogoDB。Redis更适合處理高并發,一台裝置的存儲能力是很有限的,但是多台裝置協同合作,就可以讓記憶體增大很多倍,這就需要用到叢集。
Redis叢集搭建的方式有多種,例如使用用戶端分片、Twemproxy、Codis等,但從redis 3.0之後版本支援redis-cluster叢集,它是Redis官方提出的解決方案,Redis-Cluster采用無中心結構,每個節點儲存資料和整個叢集狀态,每個節點都和其他所有節點連接配接。其redis-cluster架構圖如下:
用戶端與 redis 節點直連,不需要中間 proxy 層.用戶端不需要連接配接叢集所有節點連接配接叢集中任何一個可用節點即可。
所有的 redis 節點彼此互聯(PING-PONG 機制),内部使用二進制協定優化傳輸速度和帶寬.
1.2分布存儲機制-槽
(1)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
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準備工作
- 安裝gcc 【此步省略】
Redis 是 c 語言開發的。安裝 redis 需要 c 語言的編譯環境。如果沒有 gcc 需要線上安裝。
yum install gcc-c++ |
- 使用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目錄下
[[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-1/bin [[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-2/bin [[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-3/bin [[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-4/bin [[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-5/bin [[email protected] redis-3.0.0]# cp redis.conf /usr/local/redis-cluster/redis-6/bin |
2.3配置叢集
- 修改每個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個也啟動起來,然後檢視一下是不是都啟動起來了
[[email protected] ~]# 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叢集的腳本。
[[email protected] ~]# 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... |
- 使用 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. |
3連接配接Redis-Cluster
3.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值發現是可以擷取的,而以其它端口進入均不能擷取
3.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 |
3.4模拟叢集異常測試
關閉節點指令
./redis-cli -p 端口 shutdown |
- 測試關閉7001 和7004, 看看會發生什麼。
- 測試關閉7001、7002、7003 會發生什麼。