Redis集群:如果部署到多臺電腦,就跟普通的集群一樣;因為Redis是單線程處理的,多核CPU也只能使用一個核, 所以部署在同一臺電腦上,通過運行多個Redis實例組成集群,然后能提高CPU的利用率。 在Windows系統(tǒng)下搭建Redis集群:需要4個部件: Redis、Ruby語言運行環(huán)境、Redis的Ruby驅(qū)動redis-xxxx.gem、創(chuàng)建Redis集群的工具redis-trib.rb 安裝Redis,并運行3個實例(Redis集群需要至少3個以上節(jié)點,低于3個無法創(chuàng)建); 使用redis-trib.rb工具來創(chuàng)建Redis集群,由于該文件是用ruby語言寫的,所以需要安裝Ruby開發(fā)環(huán)境,以及驅(qū)動redis-xxxx.gem 1.下載并安裝Redis其GitHub路徑如下:https://github.com/MSOpenTech/redis/releases/ Redis提供msi和zip格式的下載文件,這里下載zip格式 3.0.504版本 將下載到的Redis-x64-3.0.504.zip解壓即可,為了方便使用,建議放在盤符根目錄下,并修改目錄名為Redis,如:C:\Redis 或者D:\Redis 通過配置文件來啟動3個不同的Redis實例,由于Redis默認端口為6379,所以這里使用了6380、6381、6382來運行3個Redis實例。 注意:為了避免不必要的錯誤,配置文件盡量保存為utf8格式,并且不要包含注釋; 配置文件中以下兩種保存日志的方式(保存在文件中、保存到System Log中)請根據(jù)需求選擇其中一種即可: loglevel notice #日志的記錄級別,notice是適合生產(chǎn)環(huán)境的 logfile "D:/Redis/Logs/redis6380_log.txt" #指定log的保持路徑,默認是創(chuàng)建在Redis安裝目錄下,如果有子目錄需要手動創(chuàng)建,如此處的Logs目錄 syslog-enabled yes #是否使用系統(tǒng)日志 redis.6380.conf 內(nèi)容如下: port 6380
loglevel notice
logfile "D:/Redis/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 內(nèi)容如下: port 6381
loglevel notice
logfile "D:/Redis/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 內(nèi)容如下: port 6382
loglevel notice
logfile "D:/Redis/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 配置內(nèi)容的解釋如下: ![]() 將上述配置文件保存到Redis目錄下,并使用這些配置文件安裝3個redis服務,命令如下: 注意:redis.6380.conf等配置文件最好使用完整路徑,避免重啟Redis集群出現(xiàn)問題,博主的安裝目錄為D:/Redis D:/Redis/redis-server.exe --service-install D:/Redis/redis.6380.conf --service-name redis6380
D:/Redis/redis-server.exe --service-install D:/Redis/redis.6381.conf --service-name redis6381
D:/Redis/redis-server.exe --service-install D:/Redis/redis.6382.conf --service-name redis6382 啟動這3個服務,命令如下: D:/Redis/redis-server.exe --service-start --service-name Redis6380
D:/Redis/redis-server.exe --service-start --service-name Redis6381
D:/Redis/redis-server.exe --service-start --service-name Redis6382 執(zhí)行結(jié)果: 2.下載并安裝ruby2.1. 下載路徑如下:http://dl./oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe 下載后,雙擊安裝即可,同樣,為了操作方便,也是建議安裝在盤符根目錄下,如: C:\Ruby22-x64 ,安裝時這里選中后兩個選項, 意思是將ruby添加到系統(tǒng)的環(huán)境變量中,在cmd命令中能直接使用ruby的命令 2.2.下載ruby環(huán)境下Redis的驅(qū)動,考慮到兼容性,這里下載的是3.2.2版本https:///gems/redis/versions/3.2.2 注意:下載在頁面右下角相關(guān)連接一項中 安裝該驅(qū)動,命令如下: gem install --local path_to_gem/filename.gem 實際操作如下: 2.3.下載Redis官方提供的創(chuàng)建Redis集群的ruby腳本文件redis-trib.rb,路徑如下:https://raw./MSOpenTech/redis/3.0/src/redis-trib.rb 打開該鏈接如果沒有下載,而是打開一個頁面,那么將該頁面保存為redis-trib.rb 建議保存到Redis的目錄下。 注意:因為redis-trib.rb是ruby代碼,必須用ruby來打開,若redis-trib.rb無法識別,需要手動選擇該文件的打開方式: **選擇ruby為的打開方式后,redis-trib.rb的logo都會發(fā)生改變,如下圖: 3.創(chuàng)建Redis集群CMD下切換到Redis目錄,使用redis-trib.rb來創(chuàng)建Redis集群: redis-trib.rb create --replicas 0 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 執(zhí)行結(jié)果: 當出現(xiàn)提示時,需要手動輸入yes,輸入后,當出現(xiàn)以下內(nèi)容,說明已經(jīng)創(chuàng)建了Redis集群 檢驗是否真的創(chuàng)建成功,輸入以下命令: redis-trib.rb check 127.0.0.1:6380 出現(xiàn)以下信息,說明創(chuàng)建的Redis集群是沒問題的 使用Redis客戶端Redis-cli.exe來查看數(shù)據(jù)記錄數(shù),以及集群相關(guān)信息 D:/Redis/redis-cli.exe -c -p 6380 -c 表示 cluster -p 表示 port 端口號 輸入dbsize查詢 記錄總數(shù) dbsize 或者一次輸入完整命令: D:/Redis/redis-cli.exe -c -p 6380 dbsize 結(jié)果如下: 輸入cluster info可以從客戶端的查看集群的信息: cluster info 結(jié)果如下: |
|