windows環境下搭建Redis叢集
轉載請註明出處,原文章地址: https://www.cnblogs.com/tommy-huang/p/6240083.html
Redis叢集:
如果部署到多臺電腦,就跟普通的叢集一樣;因為Redis是單執行緒處理的,多核CPU也只能使用一個核,
所以部署在同一臺電腦上,通過執行多個Redis例項組成叢集,然後能提高CPU的利用率。
在Windows系統下搭建Redis叢集:
需要4個部件:
Redis、Ruby語言執行環境、Redis的Ruby驅動redis-xxxx.gem、建立Redis叢集的工具redis-trib.rb
安裝Redis,並執行3個例項(Redis叢集需要至少3個以上節點,低於3個無法建立);
使用redis-trib.rb工具來建立Redis叢集,由於該檔案是用ruby語言寫的,所以需要安裝Ruby開發環境,以及驅動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中)請根據需求選擇其中一種即可:
loglevel notice #日誌的記錄級別,notice是適合生產環境的 logfile "D:/Redis/Logs/redis6380_log.txt" #指定log的保持路徑,預設是建立在Redis安裝目錄下,如果有子目錄需要手動建立,如此處的Logs目錄
syslog-enabled yes #是否使用系統日誌
syslog-ident redis6380 #在系統日誌的標識名
這裡使用了儲存在檔案中的方式,所以先在Redis目錄D:/Redis下新建Logs資料夾
redis.6380.conf 內容如下:
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 內容如下:
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 內容如下:
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
配置內容的解釋如下:
View Code將上述配置檔案儲存到Redis目錄下,並使用這些配置檔案安裝3個redis服務,命令如下:
注意:redis.6380.conf等配置檔案最好使用完整路徑,避免重啟Redis叢集出現問題,博主的安裝目錄為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
執行結果:
2.下載並安裝ruby
2.1. 下載路徑如下:
http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe
下載後,雙擊安裝即可,同樣,為了操作方便,也是建議安裝在碟符根目錄下,如: C:\Ruby22-x64 ,安裝時這裡選中後兩個選項,
意思是將ruby新增到系統的環境變數中,在cmd命令中能直接使用ruby的命令
2.2.下載ruby環境下Redis的驅動,考慮到相容性,這裡下載的是3.2.2版本
https://rubygems.org/gems/redis/versions/3.2.2
注意:下載在頁面右下角相關連線一項中
安裝該驅動,命令如下:
gem install --local path_to_gem/filename.gem
實際操作如下:
2.3.下載Redis官方提供的建立Redis叢集的ruby指令碼檔案redis-trib.rb,路徑如下:
https://raw.githubusercontent.com/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都會發生改變,如下圖:
3.建立Redis叢集
CMD下切換到Redis目錄,使用redis-trib.rb來建立Redis叢集:
redis-trib.rb create --replicas 0 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382
執行結果:
當出現提示時,需要手動輸入yes,輸入後,當出現以下內容,說明已經建立了Redis叢集
檢驗是否真的建立成功,輸入以下命令:
redis-trib.rb check 127.0.0.1:6380
出現以下資訊,說明建立的Redis叢集是沒問題的
使用Redis客戶端Redis-cli.exe來檢視資料記錄數,以及叢集相關資訊
D:/Redis/redis-cli.exe -c -p 6380
-c 表示 cluster
-p 表示 port 埠號
輸入dbsize查詢 記錄總數
dbsize
或者一次輸入完整命令:
D:/Redis/redis-cli.exe -c -p 6380 dbsize
結果如下:
輸入cluster info可以從客戶端的檢視叢集的資訊:
cluster info
結果如下: