redis cluster 集群
府谷網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),府谷網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為府谷1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的府谷做網(wǎng)站的公司定做!
Redis-Cluster 在設(shè)計的時候,就考慮到了去中心化,去中間件,集群中的每個節(jié)點都是平等的關(guān)系,都是對等的,每個節(jié)點都保存各自的數(shù)據(jù)和整個集群的狀態(tài)。每個節(jié)點都和其他所有節(jié)點連接,而且這些連接保持活躍,這樣就保證了我們只需要連接集群中的任意一個節(jié)點,就可以獲取到其他節(jié)點的數(shù)據(jù)。
Redis 集群沒有并使用傳統(tǒng)的一致性哈希來分配數(shù)據(jù),而是采用另外一種叫做哈希槽 (hash slot)的方式來分配的。redis cluster 默認分配了 16384 個slot,當我們set一個key 時,會用CRC16算法來取模得到所屬的slot,然后將這個key 分到哈希槽區(qū)間的節(jié)點上,具體算法就是:CRC16(key) % 16384。所以我們在測試的時候看到set 和 get 的時候,直接跳轉(zhuǎn)到了默認第一個配置啟動的節(jié)點。
Redis 集群會把數(shù)據(jù)存在一個 master 節(jié)點,然后在這個 master 和其對應(yīng)的salve 之間進行數(shù)據(jù)同步。當讀取數(shù)據(jù)時,也根據(jù)一致性哈希算法到對應(yīng)的 master 節(jié)點獲取數(shù)據(jù)。只有當一個master 掛掉之后,才會啟動一個對應(yīng)的 salve 節(jié)點,充當 master 。
需要注意的是:Redis集群至少3+3,是還延續(xù)著Master-Slave的設(shè)計理念;
一、安裝依賴
yum -y install gcc gcc-c++ tcl ruby ruby-devel rubygems rpm-build
安裝完Redis再執(zhí)行:gem install redis
二、redis配置實例
conf目錄:redis_cluster
data目錄: data
logs目錄:logs
端口實例;6001 6001 6002 6003 6004 6005 6006
# cat redis.conf
bind 10.10.101.100
protected-mode yes
port 6379
# 端口配置6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
# redis后臺運行
cluster-enabled yes
# 開啟集群
cluster-config-file nodes_6379.conf
集群配置 首次啟動生成
cluster-node-timeout 5000
# 請求超時
appendonly yes
# aof 日志開啟
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /opt/redis/logs/6379/redis.log
# 日志目錄
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /opt/redis/data/6379
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendfilename "appendonly.6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
三、啟動
src/redis-server redis_cluster/6001/redis.conf
src/redis-server redis_cluster/6002/redis.conf
src/redis-server redis_cluster/6003/redis.conf
src/redis-server redis_cluster/6004/redis.conf
src/redis-server redis_cluster/6005/redis.conf
src/redis-server redis_cluster/6006/redis.conf
四、集群操作
通過使用 Redis 集群命令行工具 redis-trib , 編寫節(jié)點配置文件的工作可以非常容易地完成: redis-trib 位于 Redis 源碼的 src 文件夾中, 它是一個 Ruby 程序, 這個程序通過向?qū)嵗l(fā)送特殊命令來完成創(chuàng)建新集群, 檢查集群, 或者對集群進行重新分片(reshared)等工作。
1、創(chuàng)建一個集群
redis-trib.rb create --replicas 1 10.10.101.100:6001 10.10.101.100:6002 10.10.101.100:6003 10.10.101.100:6004 10.10.101.100:6005 10.10.101.100:6006
// --replicas 1 表示 為集群中的每個主節(jié)點創(chuàng)建一個從節(jié)點。

2、集群重新分片
./redis-trib.rb reshard 10.10.101.100:6003
// 需要指定移動哈希槽數(shù) ( 1 - 16383 ) 和目標節(jié)點 ID
# src/redis-cli -h 10.10.101.100 -p 6003 cluster nodes | grep myself
root@redis: /opt/redis# redis-cli -h 10.10.101.100 -p 6003 cluster nodes | grep myself
739f446f69739eb7f95331eec7c74a4e57d2eeac 10.10.101.100:6003 myself,master - 0 0 3 connected 10923-16383

3、添加一個新節(jié)點
添加新的節(jié)點的基本過程就是添加一個空的節(jié)點然后移動一些數(shù)據(jù)給它,有兩種情況,添加一個主節(jié)點和添加一個從節(jié)點(添加從節(jié)點時需要將這個新的節(jié)點設(shè)置為集群中某個節(jié)點的復(fù)制).
兩種情況第一步都是要添加一個空的節(jié)點.
A、添加一個節(jié)點到現(xiàn)有的集群中去.
./redis-trib.rb add-node 10.10.101.100:6379 10.10.101.100:6003
// 使用addnode命令來添加節(jié)點,第一個參數(shù)是新節(jié)點的地址,第二個參數(shù)是任意一個已經(jīng)存在的節(jié)點的IP和端口.
root@redis: /opt/redis# src/redis-cli -c -h 10.10.101.100 -p 6379
10.10.101.100:6379> cluster nodes
0d9cb75cf92ce44dc3a4ad48bd95b48fc0d7a5ca 10.10.101.100:6379 myself,master - 0 0 0 connected
40d6721c15f32d732da89871b941788c3c57dcd3 10.10.101.100:6001 master - 0 1494413014845 1 connected 0-5460
041cb360dfcd8c1e8c77d8e7db03183da553953b 10.10.101.100:6004 slave 40d6721c15f32d732da89871b941788c3c57dcd3 0 1494413014344 1 connected
c9d5f58cd5e74f2c5ebbf6213e4314f4e92fb389 10.10.101.100:6006 slave 739f446f69739eb7f95331eec7c74a4e57d2eeac 0 1494413014644 3 connected
5a606eef48bc1fd8be9ad4d7a1d2a9420d8b2552 10.10.101.100:6002 master - 0 1494413015845 2 connected 5461-10922
f539cc2330a19f31a8dd55360e184cda2dc81151 10.10.101.100:6005 slave 5a606eef48bc1fd8be9ad4d7a1d2a9420d8b2552 0 1494413015345 2 connected
739f446f69739eb7f95331eec7c74a4e57d2eeac 10.10.101.100:6003 master - 0 1494413016345 3 connected 10923-16383
10.10.101.100:6379>
// 新節(jié)點沒有包含任何數(shù)據(jù), 因為它沒有包含任何哈希槽.


盡管新節(jié)點沒有包含任何哈希槽, 但它仍然是一個主節(jié)點, 所以在集群需要將某個從節(jié)點升級為新的主節(jié)點時, 這個新節(jié)點不會被選中。只要將集群中的某些哈希桶移動到新節(jié)點里面, 新節(jié)點就會成為真正的主節(jié)點了。
4、添加一個從節(jié)點
有兩種方法添加從節(jié)點,可以像添加主節(jié)點一樣使用redis-trib 命令,也可以像下面的例子一樣使用 –slave選項:
src/redis-trib.rb add-node --slave 10.10.101.100:6379 10.10.101.100:6001
// 此處并沒有指定添加的這個從節(jié)點的主節(jié)點,這種情況下系統(tǒng)會在其他的復(fù)制集中的主節(jié)點中隨機選取一個作為這個從節(jié)點的主節(jié)點。
a、指定主節(jié)點添加:
src/redis-trib.rb add-node --slave --master-id 739f446f69739eb7f95331eec7c74a4e57d2eeac 10.10.101.100:6379 10.10.101.100:6001
b、給主節(jié)點 10.10.101.100:6003添加一個從節(jié)點,該節(jié)點哈希槽的范圍10923-16383
節(jié)點 ID 739f446f69739eb7f95331eec7c74a4e57d2eeac,我們需要鏈接新的節(jié)點(已經(jīng)是空的主節(jié)點)并執(zhí)行命令:
redis 10.10.101.100:6379> cluster replicate 739f446f69739eb7f95331eec7c74a4e57d2eeac
c、驗證下集群節(jié)點
$ redis-cli -p 6379 cluster nodes | grep slave | grep739f446f69739eb7f95331eec7c74a4e57d2eeac
f093c80dde814da99c5cf72a7dd01590792b783b 10.10.101.100:6379 slave 739f446f69739eb7f95331eec7c74a4e57d2eeac 0 1385543617702 3 connected
041cb360dfcd8c1e8c77d8e7db03183da553953b 10.10.101.100:6004 slave 40d6721c15f32d732da89871b941788c3c57dcd3 0 1494413014344 1 connected
// 節(jié)點 739f446f69739eb7f95331eec7c74a4e57d2eeac 有兩個從節(jié)點, 6004 (已經(jīng)存在的) 和 6379 (新添加的).
5、移除一個節(jié)點
只要使用 del-node 命令即可:
src/redis-trib del-node 10.10.101.100:6001 `<node-id>`
// 第一個參數(shù)是任意一個節(jié)點的地址,第二個節(jié)點是你想要移除的節(jié)點地址。
使用同樣的方法移除主節(jié)點,不過在移除主節(jié)點前,需要確保這個主節(jié)點是空的. 如果不是空的,需要將這個節(jié)點的數(shù)據(jù)重新分片到其他主節(jié)點上.
替代移除主節(jié)點的方法是手動執(zhí)行故障恢復(fù),被移除的主節(jié)點會作為一個從節(jié)點存在,不過這種情況下不會減少集群節(jié)點的數(shù)量,也需要重新分片數(shù)據(jù).
6、從節(jié)點的遷移
在Redis集群中會存在改變一個從節(jié)點的主節(jié)點的情況,需要執(zhí)行如下命令 :
CLUSTER REPLICATE <master-node-id>
網(wǎng)頁題目:Redis_cluster集群
標題來源:http://www.chinadenli.net/article20/pgcjco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站營銷、網(wǎng)站內(nèi)鏈、軟件開發(fā)、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)