Windows Redis集羣 RedisCluster

Windows下搭建 Redis 集羣

 Redis集羣

 如果部署到多臺電腦,就跟普通的集羣一樣;因爲Redis是單線程處理的,多核CPU也只能使用一個核,

所以部署在同一臺電腦上,通過運行多個Redis實例組成集羣,然後能提高CPU的利用率。

在Windows系統下搭建Redis集羣:

    需要3個部件:

  Redis、Ruby語言運行環境、創建Redis集羣的工具redis-trib.rb

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

     使用redis-trib.rb工具來創建Redis集羣,由於該文件是用ruby語言寫的,所以需要安裝Ruby開發環境

1.下載並安裝Redis

     如下路徑:https://github.com/MicrosoftArchive/redis/releases

  1. 將下載到的Redis-x64-3.2.100.zip解壓即可,放在根目錄下,並修改目錄名爲Redis,如:C:\Redis。
  2. 通過配置文件來啓動3個不同的Redis實例,這裏使用了6379、6380、6381來運行3個Redis實例。
  3. 打開目錄6379下有一個文件 redis.windows.conf,修改裏面的端口號,以及集羣支持配置。

注意:爲了避免不必要的錯誤,配置文件儘量保存爲utf8格式; 

修改redis.windows.conf配置端口,三個實例分別改成6379、6380、6381。如:

port 6379

修改其他配置支持集羣,找到對應的參數去掉註釋:

cluster-enabled yes                       #是否開啓集羣
cluster-config-file nodes-6379.conf       #是爲該節點的配置信息。服務啓動後會在目錄生成該文件。
cluster-node-timeout 15000                #那麼在創建集羣的時候,不會超時。
appendonly yes                            #數據的保存爲aof格式

編寫一個 bat 來啓動 redis,在每個節點目錄下建立 start.bat,內容如下:

title redis-6379
redis-server.exe redis.windows.conf

如此一個Redis實例配置完成了,複製該實例,並分別修改端口爲6380、6381。就獲得了三個實例。

2.下載並安裝ruby

  2.1. 下載路徑如下:http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.3.3.exe

      下載後,雙擊安裝即可,同樣,爲了操作方便,也是建議安裝在盤符根目錄下,如: C:\Ruby22-x64 ,安裝時這裏選中後兩個選項,

  意思是將ruby添加到系統的環境變量中,在cmd命令中能直接使用ruby的命令

   2.2.下載Redis官方提供的創建Redis集羣的ruby腳本文件redis-trib.rb,路徑如下:

    https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

           打開該鏈接如果沒有下載,而是打開一個頁面,那麼將該頁面保存爲redis-trib.rb

           保存到C:\Redis的目錄下。

  3.創建Redis集羣  

     cmd切換到C:\Redis目錄,使用redis-trib.rb來創建Redis集羣:

redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 

  執行結果:

C:\Redis>redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
>>> Creating cluster
Connecting to node 127.0.0.1:6379: OK
Connecting to node 127.0.0.1:6380: OK
Connecting to node 127.0.0.1:6381: OK
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
M: 22349aa7569a82d1e6a34e9e639bccd8c43181b9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: 73f9e74d98b6e9c875628dc74b686f9a22218fb5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 6a52b024e3cc9e8ba307dc41f21522287bbc7703 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept):

     當出現提示時,需要手動輸入yes,輸入後,當出現以下內容,說明已經創建了Redis集羣

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:6379)
M: 22349aa7569a82d1e6a34e9e639bccd8c43181b9 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: 73f9e74d98b6e9c875628dc74b686f9a22218fb5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 6a52b024e3cc9e8ba307dc41f21522287bbc7703 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

C:\Redis>

測試    

 檢驗是否真的創建成功,輸入以下命令:

命令 redis-cli –c –h ”地址” –p "端口號" ;  c 表示集羣

C:\Redis\6379>redis-cli -c -h 127.0.0.1 -p 6379
127.0.0.1:6379>

 

輸入dbsize查詢 記錄總數

127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379>

輸入cluster info可以從客戶端的查看集羣的信息

127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:355
cluster_stats_messages_received:355
127.0.0.1:6379>

參考:

https://blog.csdn.net/zsg88/article/details/73715947

https://www.cnblogs.com/tommy-huang/p/6240083.html

https://www.cnblogs.com/yaozb/p/6911395.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章