準(zhǔn)備工作
?需要4個部件:Redis、Ruby語言運(yùn)行環(huán)境、Redis的Ruby驅(qū)動redis-xxxx.gem、創(chuàng)建Redis集群的工具redis-trib.rb。使用redis-trib.rb工具來創(chuàng)建Redis集群,由于該文件是用ruby語言寫的,所以需要安裝Ruby開發(fā)環(huán)境,以及驅(qū)動redis-xxxx.gem。
?
1)下載Redis安裝文件:https://github.com/MSOpenTech/redis/releases/,Redis提供msi和zip格式的下載文件,這里下載zip格式Redis-x64-3.2.100版本。
2)下載Ruby安裝文件:http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe
3)下載Ruby環(huán)境下Redis的驅(qū)動:https://rubygems.org/gems/redis/versions/3.2.2,考慮到兼容性,這里下載的是3.2.2版本
注意:下載在頁面右下角相關(guān)連接一項(xiàng)中
4)下載Redis官方提供的創(chuàng)建Redis集群的ruby腳本文件redis-trib.rb,路徑如下:https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb
安裝Redis
將下載到的Redis-x64-3.2.100.zip解壓即可,為了方便使用,建議放在盤符根目錄下,如:D:Redis-ClusterRedis-x64-3.2.100。?
安裝Redis,并運(yùn)行3個實(shí)例(Redis集群需要至少3個以上節(jié)點(diǎn),低于3個無法創(chuàng)建);
通過配置文件來啟動6個不同的Redis實(shí)例,由于Redis默認(rèn)端口為6379,所以這里使用了6380、6381、6382、6383、6384、6385來運(yùn)行6個Redis實(shí)例。
注意:
1)為了避免不必要的錯誤,配置文件盡量保存為utf8格式,并且不要包含注釋;
2)配置文件中以下兩種保存日志的方式(保存在文件中、保存到System Log中)請根據(jù)需求選擇其中一種即可:
loglevel notice #日志的記錄級別,notice是適合生產(chǎn)環(huán)境的 logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6380_log.txt" #指定log的保持路徑,默認(rèn)是創(chuàng)建在Redis安裝目錄下,如果有子目錄需要手動創(chuàng)建,如此處的Logs目錄 syslog-enabled yes #是否使用系統(tǒng)日志 syslog-ident redis6380 #在系統(tǒng)日志的標(biāo)識名
這里使用了保存在文件中的方式,所以先在Redis目錄D:Redis-ClusterRedis-x64-3.2.100下新建Logs文件夾。
在Redis安裝根目錄下,創(chuàng)建編碼格式為utf-8的配置文件:redis.6380.conf、redis.6381.conf、redis.6382.conf、redis.6383.conf、redis.6384.conf、redis.6385.conf。
redis.6380.conf、
port 6380 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6380_log.txt" appendonly yes appendfilename "appendonly.6380.aof" cluster-enabled yes cluster-config-file nodes.6380.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
redis.6381.conf、
port 6381 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6381_log.txt" appendonly yes appendfilename "appendonly.6381.aof" cluster-enabled yes cluster-config-file nodes.6381.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
redis.6382.conf、
port 6382 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6382_log.txt" appendonly yes appendfilename "appendonly.6382.aof" cluster-enabled yes cluster-config-file nodes.6382.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
redis.6383.conf、
port 6383 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6383_log.txt" appendonly yes appendfilename "appendonly.6383.aof" cluster-enabled yes cluster-config-file nodes.6383.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
redis.6384.conf、
port 6384 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6384_log.txt" appendonly yes appendfilename "appendonly.6384.aof" cluster-enabled yes cluster-config-file nodes.6384.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
redis.6385.conf
port 6385 loglevel notice logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6385_log.txt" appendonly yes appendfilename "appendonly.6385.aof" cluster-enabled yes cluster-config-file nodes.6385.conf cluster-node-timeout 15000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes
配置解釋如下:
1 port 6380 #端口號 2 loglevel notice #日志的記錄級別,notice是適合生產(chǎn)環(huán)境的 3 logfile "Logs/redis6380_log.txt" #指定log的保持路徑,默認(rèn)是創(chuàng)建在Redis安裝目錄下,如果有子目錄需要手動創(chuàng)建,如此處的Logs目錄 4 syslog-enabled yes #是否使用系統(tǒng)日志 5 syslog-ident redis6380 #在系統(tǒng)日志的標(biāo)識名 6 appendonly yes #數(shù)據(jù)的保存為aof格式 7 appendfilename "appendonly.6380.aof" #數(shù)據(jù)保存文件 8 cluster-enabled yes #是否開啟集群 9 cluster-config-file nodes.6380.conf 10 cluster-node-timeout 15000 11 cluster-slave-validity-factor 10 12 cluster-migration-barrier 1 13 cluster-require-full-coverage yes
將上述配置文件保存到Redis目錄下,并使用這些配置文件安裝6個redis服務(wù),命令如下:
D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6380.conf --service-name redis6380 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6381.conf --service-name redis6381 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6382.conf --service-name redis6382 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6383.conf --service-name redis6383 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6384.conf --service-name redis6384 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-install D:/Redis-Cluster/Redis-x64-3.2.100/redis.6385.conf --service-name redis6385
注意:
1)redis.6380.conf等配置文件最好使用完整路徑,避免重啟Redis集群出現(xiàn)問題
2)卸載命令為:
D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6380.conf --service-name redis6380 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6381.conf --service-name redis6381 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6382.conf --service-name redis6382 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6383.conf --service-name redis6383 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6384.conf --service-name redis6384 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-uninstall D:/Redis-Cluster/Redis-x64-3.2.100/redis.6385.conf --service-name redis6385
啟動這6個服務(wù),命令如下:
D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6380 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6381 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6382 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6383 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6384 D:/Redis-Cluster/Redis-x64-3.2.100/redis-server.exe --service-start --service-name redis6385
執(zhí)行結(jié)果:
安裝ruby
1)Ruby環(huán)境安裝。
雙擊下載的“rubyinstaller-2.2.4-x64.exe”安裝即可,同樣,為了操作方便,也是建議安裝在盤符根目錄下,如: C:Ruby22-x64 ,安裝時這里選中后兩個選項(xiàng),
意思是將ruby添加到系統(tǒng)的環(huán)境變量中,在cmd命令中能直接使用ruby的命令
2)安裝Ruby環(huán)境下Redis的驅(qū)動
將下載的"Ruby環(huán)境下Redis的驅(qū)動文件(redis-3.2.2.gem)"拷貝到Ruby安裝根目錄(C:Ruby22-x64)下。
然后執(zhí)行安裝命令如下:
gem install --local path_to_gem/filename.gem
創(chuàng)建Redis集群
將下載的“創(chuàng)建Redis集群的ruby腳本文件redis-trib.rb”文件拷貝到Redis安裝根目錄(D:Redis-ClusterRedis-x64-3.2.100)下。
1)使用redis-trib.rb來創(chuàng)建Redis集群
MD下切換到Redis目錄(D:Redis-ClusterRedis-x64-3.2.100)
D:/Redis-Cluster/Redis-x64-3.2.100/redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
D:Redis-ClusterRedis-x64-3.2.100>redis-trib.rb create --replicas 1 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 >>> Creating cluster Connecting to node 127.0.0.1:6380: OK Connecting to node 127.0.0.1:6381: OK Connecting to node 127.0.0.1:6382: OK Connecting to node 127.0.0.1:6383: OK Connecting to node 127.0.0.1:6384: OK Connecting to node 127.0.0.1:6385: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 Adding replica 127.0.0.1:6383 to 127.0.0.1:6380 Adding replica 127.0.0.1:6384 to 127.0.0.1:6381 Adding replica 127.0.0.1:6385 to 127.0.0.1:6382 M: bb6ef615bb0ae13275943caec0db9d30b9f35c5e 127.0.0.1:6380 slots:0-5460 (5461 slots) master M: b4d120f2983ad683f7b68992e1ba414722238db7 127.0.0.1:6381 slots:5461-10922 (5462 slots) master M: 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 127.0.0.1:6382 slots:10923-16383 (5461 slots) master S: 5d154137180284d926ef51a91fc75f9438249ef8 127.0.0.1:6383 replicates bb6ef615bb0ae13275943caec0db9d30b9f35c5e S: ad151680a3e36cf2083ef822be0bdb075a7d36de 127.0.0.1:6384 replicates b4d120f2983ad683f7b68992e1ba414722238db7 S: 9a2260a5a6a2add84b622a453a6a7b86a29d180d 127.0.0.1:6385 replicates 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 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 127.0.0.1:6380)
M: bb6ef615bb0ae13275943caec0db9d30b9f35c5e 127.0.0.1:6380 slots:0-5460 (5461 slots) master M: b4d120f2983ad683f7b68992e1ba414722238db7 127.0.0.1:6381 slots:5461-10922 (5462 slots) master M: 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 127.0.0.1:6382 slots:10923-16383 (5461 slots) master M: 5d154137180284d926ef51a91fc75f9438249ef8 127.0.0.1:6383 slots: (0 slots) master replicates bb6ef615bb0ae13275943caec0db9d30b9f35c5e M: ad151680a3e36cf2083ef822be0bdb075a7d36de 127.0.0.1:6384 slots: (0 slots) master replicates b4d120f2983ad683f7b68992e1ba414722238db7 M: 9a2260a5a6a2add84b622a453a6a7b86a29d180d 127.0.0.1:6385 slots: (0 slots) master replicates 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
備注:
1)--replicas #指定集群中每個主節(jié)點(diǎn)配備幾個從節(jié)點(diǎn),這里設(shè)置為1。
2)?redis-trib.rb工具的使用
1、create:創(chuàng)建集群 2、check:檢查集群 3、info:查看集群信息 4、fix:修復(fù)集群 5、reshard:在線遷移slot 6、rebalance:平衡集群節(jié)點(diǎn)slot數(shù)量 7、add-node:將新節(jié)點(diǎn)加入集群 8、del-node:從集群中刪除節(jié)點(diǎn) 9、set-timeout:設(shè)置集群節(jié)點(diǎn)間心跳連接的超時時間 10、call:在集群全部節(jié)點(diǎn)上執(zhí)行命令 11、import:將外部redis數(shù)據(jù)導(dǎo)入集群
2)檢驗(yàn)是否真的創(chuàng)建成功
輸入以下命令:
redis-trib.rb check 127.0.0.1:6380
?
?如果現(xiàn)實(shí)信息如下,則說明創(chuàng)建的Redis集群是沒問題。
D:Redis-ClusterRedis-x64-3.2.100>redis-trib.rb check 127.0.0.1:6380 Connecting to node 127.0.0.1:6380: OK Connecting to node 127.0.0.1:6383: OK Connecting to node 127.0.0.1:6382: OK Connecting to node 127.0.0.1:6384: OK Connecting to node 127.0.0.1:6385: OK Connecting to node 127.0.0.1:6381: OK >>> Performing Cluster Check (using node 127.0.0.1:6380) M: bb6ef615bb0ae13275943caec0db9d30b9f35c5e 127.0.0.1:6380 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 5d154137180284d926ef51a91fc75f9438249ef8 127.0.0.1:6383 slots: (0 slots) slave replicates bb6ef615bb0ae13275943caec0db9d30b9f35c5e M: 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 127.0.0.1:6382 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: ad151680a3e36cf2083ef822be0bdb075a7d36de 127.0.0.1:6384 slots: (0 slots) slave replicates b4d120f2983ad683f7b68992e1ba414722238db7 S: 9a2260a5a6a2add84b622a453a6a7b86a29d180d 127.0.0.1:6385 slots: (0 slots) slave replicates 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 M: b4d120f2983ad683f7b68992e1ba414722238db7 127.0.0.1:6381 slots:5461-10922 (5462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. D:Redis-ClusterRedis-x64-3.2.100>
3)信息查詢
使用Redis客戶端Redis-cli.exe來查看數(shù)據(jù)記錄數(shù),以及集群相關(guān)信息
D:Redis-ClusterRedis-x64-3.2.100>redis-cli.exe -h 127.0.0.1 -p 6380
127.0.0.1:6380> cluster nodes
5d154137180284d926ef51a91fc75f9438249ef8 127.0.0.1:6383 slave? ? ? ? ? bb6ef615bb0ae13275943caec0db9d30b9f35c5e 0 1560690706115 4 connected
bb6ef615bb0ae13275943caec0db9d30b9f35c5e 127.0.0.1:6380 myself,master? -? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 0? ? ? ? ? ? ?1 connected 0-5460
837779b3965e2c9d4dd4385750aaaaf9a9039fb0 127.0.0.1:6382 master? ? ? ? ?-? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 15606907021143? connected 10923-16383
ad151680a3e36cf2083ef822be0bdb075a7d36de 127.0.0.1:6384 slave? ? ? ? ? b4d120f2983ad683f7b68992e1ba414722238db7 0 1560690705116 5 connected
9a2260a5a6a2add84b622a453a6a7b86a29d180d 127.0.0.1:6385 slave? ? ? ? ? 837779b3965e2c9d4dd4385750aaaaf9a9039fb0 0 1560690703615 6 connected
b4d120f2983ad683f7b68992e1ba414722238db7 127.0.0.1:6381 master? ? ? ? ?-? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 15606907071152? connected 5461-10922
127.0.0.1:6380> dbsize
(integer) 0
127.0.0.1:6380>
?
基礎(chǔ)才是編程人員應(yīng)該深入研究的問題,比如:
1)List/Set/Map內(nèi)部組成原理|區(qū)別
2)mysql索引存儲結(jié)構(gòu)&如何調(diào)優(yōu)/b-tree特點(diǎn)、計算復(fù)雜度及影響復(fù)雜度的因素。。。
3)JVM運(yùn)行組成與原理及調(diào)優(yōu)
4)Java類加載器運(yùn)行原理
5)Java中GC過程原理|使用的回收算法原理
6)Redis中hash一致性實(shí)現(xiàn)及與hash其他區(qū)別
7)Java多線程、線程池開發(fā)、管理Lock與Synchroined區(qū)別
8)Spring IOC/AOP 原理;加載過程的。。。
【+加關(guān)注】。
本文摘自 :https://blog.51cto.com/u