首頁  >  文章  >  資料庫  >  windows下如何搭建redis集群

windows下如何搭建redis集群

王林
王林轉載
2021-03-05 09:28:562004瀏覽

windows下如何搭建redis集群

前言:

叢集是指透過新增伺服器的數量,提供相同的服務,讓如武器達到穩定、有效率的狀態。為什麼要使用redis叢集呢? redis集群可以強化的redis的讀寫能力。

下面我們就來正式學習下redis集群。

準備工作:

需要4個元件:Redis、Ruby語言運作環境、Redis的Ruby驅動redis-xxxx.gem、建立Redis叢集的工具redis-trib.rb。使用redis-trib.rb工具來建立Redis集群,由於該檔案是用ruby語言寫的,所以需要安裝Ruby開發環境,以及驅動redis-xxxx.gem。

windows下如何搭建redis集群

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環境下Redis的驅動程式:https://rubygems.org/gems/redis/versions/3.2.2,考慮到相容性,這裡下載的是3.2.2版本

注意:下載在頁面右下角相關連接一項中

windows下如何搭建redis集群

4)下載Redis官方提供的創建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-Cluster\Redis-x64-3.2.100。

安裝Redis,並執行3個實例(Redis叢集需要至少3個以上節點,低於3個無法建立);

透過設定檔來啟動6個不同的Redis實例,由於Redis預設連接埠為6379,所以這裡使用了6380、6381、6382、6383、6384、6385來運行6個Redis實例。

注意:

(1)為了避免不必要的錯誤,設定檔盡量儲存為utf8格式,並且不要包含註解;

(2)設定檔中以下兩種保存日誌的方式(保存在檔案中、儲存到System Log中)請根據需求選擇其中一種即可:

loglevel notice    #日志的记录级别,notice是适合生产环境的
logfile "D:/Redis-Cluster/Redis-x64-3.2.100/Logs/redis6380_log.txt" #指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
syslog-enabled yes    #是否使用系统日志   
syslog-ident redis6380    #在系统日志的标识名

這裡使用了保存在檔案中的方式,所以先在Redis目錄D:\Redis-Cluster\Redis-x64-3.2.100下新Logs資料夾。

在Redis安裝根目錄下,建立編碼格式為​​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

##

port 6380                                 #端口号
loglevel notice                           #日志的记录级别,notice是适合生产环境的
logfile "Logs/redis6380_log.txt"          #指定log的保持路径,默认是创建在Redis安装目录下,如果有子目录需要手动创建,如此处的Logs目录
syslog-enabled yes                        #是否使用系统日志
syslog-ident redis6380                    #在系统日志的标识名
appendonly yes                            #数据的保存为aof格式
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影片教學

設定解釋如下:

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

將上述設定檔儲存到Redis目錄下,並使用這些設定檔安裝6個redis服務,指令如下:

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

注意:

1)redis.6380.conf等設定檔最好使用完整路徑,避免重啟Redis叢集出現問題

# 2)卸載指令為:

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

啟動這6個服務,指令如下:

gem install --local path_to_gem/filename.gem

執行結果:

windows下如何搭建redis集群

##安裝ruby

(1)Ruby環境安裝。

雙擊下載的「rubyinstaller-2.2.4-x64.exe」安裝即可,同樣,為了操作方便,也是建議安裝在磁碟機根目錄下,如: C:\Ruby22-x64 ,安裝時這裡選取後兩個選項,

windows下如何搭建redis集群

意思是將ruby加入系統的環境變數中,在cmd指令中能直接使用ruby的指令

(1)安裝Ruby環境下Redis的驅動程式

將下載的"Ruby環境下Redis的驅動程式檔案(redis-3.2.2.gem)"拷貝到Ruby安裝根目錄(C:\Ruby22- x64)下。

然後執行安裝指令如下:

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
>>> 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.

windows下如何搭建redis集群

建立Redis叢集

將下載的「建立Redis叢集的ruby腳本檔案redis- trib.rb」檔案拷貝到Redis安裝根目錄(D:\Redis-Cluster\Redis-x64-3.2.100)下。

(1)使用redis-trib.rb來建立Redis叢集

MD下切換到Redis目錄(D:\Redis-Cluster\Redis-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 1:27. 0.1:6384 127.0.0.1:6385

、create:创建集群
、check:检查集群
、info:查看集群信息
、fix:修复集群
、reshard:在线迁移slot
、rebalance:平衡集群节点slot数量
、add-node:将新节点加入集群
、del-node:从集群中删除节点
、set-timeout:设置集群节点间心跳连接的超时时间
、call:在集群全部节点上执行命令
、import:将外部redis数据导入集群

備註:

(1)--replicas #指定集群中每个主节点配备几个从节点,这里设置为1。

(2)redis-trib.rb工具的使用

、create:创建集群
、check:检查集群
、info:查看集群信息
、fix:修复集群
、reshard:在线迁移slot
、rebalance:平衡集群节点slot数量
、add-node:将新节点加入集群
、del-node:从集群中删除节点
、set-timeout:设置集群节点间心跳连接的超时时间
、call:在集群全部节点上执行命令
、import:将外部redis数据导入集群

(2)检验是否真的创建成功

输入以下命令:

redis-trib.rb check 127.0.0.1:6380

如果现实信息如下,则说明创建的Redis集群是没问题。

D:\Redis-Cluster\Redis-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-Cluster\Redis-x64-3.2.100>

(3)信息查询

使用Redis客户端Redis-cli.exe来查看数据记录数,以及集群相关信息

windows下如何搭建redis集群

原文作者:cctext

原文链接:https://www.cnblogs.com/yy3b2007com/p/11033009.html

相关推荐:redis数据库教程

以上是windows下如何搭建redis集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除