隨着企業數據量的增多,Redis不論做爲數據存儲或是緩存,它的數據量也會逐漸增多,雖然Redis的速度很是可觀,但隨着其中的數據量的龐大,而且僅僅在一個設備或是一個Redis實例中,其存取速度也會大打折扣,因此咱們須要在不一樣的設備或服務器上,搭建多個Redis實例倉庫,將原來的Redis的全部的keys分發到各個服務器的Redis上,這就是如今所謂的Redis集羣(Redis Cluster)。node
· 原理redis
· 實施緩存
· 注意ruby
· 問題服務器
1、原理app
一、數據共享異步
Redis提供多個節點實例間的數據共享,也就是Redis A,B,C,D彼此之間的數據是同步的,一樣彼此之間也能夠通訊,而對於客戶端操做的keys是由Redis系統自行分配到各個節點中。tcp
二、主從複製工具
Redis的多個實例間通訊時,一旦其中的一個節點故障,那麼Redis集羣就不能繼續正常工做,因此須要一種複製機制(Master-Slave)機制,作到一旦節點A故障了,那麼其從節點A1和A2就能夠接管並繼續提供與A一樣的工做服務,固然若是節點A,A1,A2節點都出現問題,那麼一樣這個集羣不會繼續保持工做,可是這種狀況比較罕見,即便出現了,也會及時發現並修復使用。性能
建議:部署主從複製機制(Master-Slave)。
三、哈希槽值
Redis集羣中使用哈希槽來存儲客戶端的keys,而在Redis中,目前存在16384個哈希槽,它們被所有分配給全部的節點,正如上圖所示,全部的哈希槽值被節點A,B,C分配完成了。
2、實施
一、建立集羣
A、輔助工具安裝
在搭建Redis Cluster以前,請先確保系統已經安裝了zlib和ruby(含rubygems)軟件依賴包,請自行安裝。
B、安裝redis-cluster
在Redis的源碼路徑下,位於src目錄中的redis-trib.rb文件是用來搭建和維護集羣的工具,咱們須要將其放入與redis-server和redis-cli一樣的指令環境下,以下:
sudo cp /redis/redis-3.0.7/src/redis-trib.rb /redis/bin
sudo cp /redis/redis-3.0.7/src/redis-server /redis/bin
sudo cp /redis/redis-3.0.7/src/redis-cli /redis/bin
C、配置redis-cluster
文件結構:
通用配置:
#generate configs
daemonize no
tcp-backlog 511
timeout 2000
tcp-keepalive 0
loglevel notice
databases 16
slave-serve-stale-data yes
slave-read-only yes
repl-disable-tcp-nodelay yes
slave-priority 100
#open the aof persistence
appendonly yes
#config aof mode for everysec
appendfsync everysec
#while rewrite op then close the aof write mode
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 64mb
aof-rewrite-incremental-fsync yes
#limit the time of lua scripts executes
lua-time-limit 5000
#open redis cluster switch
cluster-enabled yes
#timeout of nodes connections
cluster-node-timeout 15000
cluster-migration-barrier 1
slowlog-log-slower-than 10000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
#open the online rehash
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
特殊配置:
這裏以6379端口爲例說明,其它的特殊配置只需修改對應的端口便可:
#common configs
include /redis/etc/redis-common.conf
#listen tcp port
port 6379
#memory cahce max size
maxmemory 100m
maxmemory-policy allkeys-lru
#aof filename
appendfilename "appendonly-6379.aof"
#rdb file,only use in the slave handle
dbfilename dump-6379.rdb
dir /redis/db-6379
#cluster config that auto create
cluster-config-file nodes-6379.conf
#log path
logfile /redis/logs/6379/log-6379.log
#in the same computer redis,then give the limit
#fork all redis processes done rewrite,using big memory
auto-aof-rewrite-percentage 80-100
D、搭建redis-cluster
在這裏,咱們使用Redis自帶的ruby工具redis-trib.rb來建立和管理集羣。本人共建立了6個redis節點,分別啓動以後,使用./redis-trib.rb create –replicas配置和生成3個主節點和對應每一個節點的3個從節點。
首先,啓動6個節點實例:
$redis-server /redis/etc/redis-6379.conf
$redis-server /redis/etc/redis-6380.conf
$redis-server /redis/etc/redis-6381.conf
$redis-server /redis/etc/redis-7379.conf
$redis-server /redis/etc/redis-7380.conf
$redis-server /redis/etc/redis-7381.conf
其次,建立分配主從節點:
$redis-trib.rb create --replicas 1
127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380127.0.0.1:7381
執行的結果:
NOTE:
--replicas 1表明建立1個從節點,建立的從節點的順序是按照6379-7379,6380-7380及6381-7381的順序建立配置的。
從上圖知道,已經建立和配置了3個Master和對應的3個Sub子節點,同時會提示您是否贊成這個配置生成,當你回覆yes以後,顯示以下:
NOTE:
從上圖知道,Redis集羣已經建立和配置成功了,而且redis的16384個哈希槽已經所有分配完成。
二、集羣驗證
驗證可使用redis-rb-cluster或是redis-cli來驗證,這裏以最簡單的方式redis-cli來驗證使用,意在說明集羣的使用和校驗。
A、哈希槽分配
$redis-cli -c -p 6379
127.0.0.1:6379> set mykey "hello"
-> Redirected to slot [14687] located at 127.0.0.1:6381
$redis-cli -c -p 6380
127.0.0.1:6380> set mykey2 "hello world"
-> Redirected to slot [14119] located at 127.0.0.1:6381
OK
$redis-cli -c -p 6381
127.0.0.1:6381> set mykey3 "hello"
-> Redirected to slot [9990] located at 127.0.0.1:6380
OK
B、集羣驗證
$redis-trib.rb 127.0.0.1:7379
結果:
右上圖,能夠知道集羣配置沒問題,繼續往下驗證下數據的異步同步。
C、集羣數據共享
$redis-cli -c -p 6379
127.0.0.1:6379> set mykey "hello"
-> Redirected to slot [14687] located at 127.0.0.1:6381
OK
127.0.0.1:6381> get mykey2
"hello world"
127.0.0.1:6381> get mykey3
-> Redirected to slot [9990] located at 127.0.0.1:6380
"hello"
NOTE:
從上面能夠看出,我實例子6379中能夠訪問同一個集羣內的節點數據,訪問的機制是根據set時分配的哈希槽,例如:在6379中,使用get mykey3,那麼自動定位到6380。
3、注意
一、主從複製
Redis集羣支持主從複製功能,也就是主節點對應的從節點,可是不須要在從節點中加入slaveof <server>:<port>,不然會報錯哦。
二、主從配置
通常狀況下,從節點的配置和對應的主節點的配置相似,可是通常從節點的大小要小於主節點的配置大小,這主要考慮內存和性能均衡方面,請在實際使用時留意下。
三、實例通訊
Redis集羣中的節點實例間的數據共享機制是經過定位哈希槽(set時的鍵值分配的哈希),不會區分主從節點或是普通節點的通訊。
4、問題
遇到問題:
custom_require.rb:36:in `require': cannot load such file -- redis(LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ./redis-trib.rb:25:in `<main>'
解決辦法:
sudo gem install redis來安裝ruby和redis的接口包便可