【Azure Redis 快取】Windows版創建 Redis Cluster 實驗 (精簡版)
- 2021 年 10 月 28 日
- 筆記
- 【Azure Redis 快取】, Azure Redis, redis cluster
簡介
學習Redis Cluster的第一步,即本地搭建Redis Cluster。但是在Redis的官方文檔中,是介紹在Linux系統中搭建Redis Cluster。本文主要介紹在Windows系統中如何快速創建一個3主/3從的Redis Cluster(Redis集群)。
準備工具
1)在GitHub中下載由Microsoft發布的Windows版Redis文件(當前最新版本為3.2.100, 下載ZIP文件,解壓後雙擊 redis-server.exe 就可以啟用一個單機的Reids服務)。下載地址://github.com/MicrosoftArchive/redis/releases
2)Redis 3版本需要使用 redis-trib.rb create 來執行集群的創建工作,也需要從Github中下載 redis-trib.rb文件(如無法下載,可在本文的附錄中複製)。下載地址://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb
3)Ruby 運行環境(因為redis-trib.rb是Ruby語言編寫,所以需要在Windows本機中安裝Ruby運行環境)。下載地址://rubyinstaller.org/downloads/
4)Ruby Redis驅動 Redis gem。下載地址://rubygems.org/gems/redis/versions/4.5.1
For Redis version 3 or 4, there is the older tool called
redis-trib.rb
which is very similar. You can find it in thesrc
directory of the Redis source code distribution. You need to installredis
gem to be able to runredis-trib
.Source: //redis.io/topics/cluster-tutorial#creating-the-cluster
第一步:準備Redis配置文件(redis.conf)
創建 cluster_test文件夾,這次實驗使用7000,7001,7002,7003,7004,7005 這六個埠。
mkdir cluster-test cd cluster-test mkdir 7000 7001 7002 7003 7004 7005
分別創建好這個6個子文件夾後,創建redis.conf文件,把下面最基本的配置文件內容分別放在在這六個文件夾中
port 7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes
注: 每個子文件夾中的埠需要對應修改為7001,… 7005。
最終效果為:
第二步:分別啟動6個Redis
複製redis-server.exe文件到cluster-test中,然後打開6個CMD窗口,分別進入到7000, … 7005 目錄中。運行啟動Redis Service命令
..\redis-server .\redis.conf
註:需要在6個CMD中對7000,7001,7002,7003,7004,7005 啟動Reids Server
第三步: 安裝Ruby運行環境
雙擊安裝即可,所有選項保持默認。
第四步:安裝Redis的驅動 Redis gem
複製 redis-4.5.1.gem 文件到Ruby的安裝目錄,運行 gem install –local C:\Ruby30-x64\redis-4.5.1.gem ,等待安裝成功。
第五步:redis-trib.rb create 創建集群
把下載的redis-trib.rb文件放在cluster_test目錄中,CMD窗口進入 cluster_test 目錄,執行 redis-trib.rb create
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
當從日誌中看見 [OK] All 16384 slots covered
日誌,表示集群創建完成。表示至少一個主節點可以對16384個槽(slots)提供服務了。
創建動畫圖:
測試驗證
使用 redis-cli.exe 工具(包含Redis的下載ZIP文件中)可以非常容易的查看Cluster Node資訊和 Set, Get Key
redis-cli.exe -c -p 7000 redis 127.0.0.1:7000> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:7002 OK redis 127.0.0.1:7002> set hello world -> Redirected to slot [866] located at 127.0.0.1:7000 OK redis 127.0.0.1:7000> get foo -> Redirected to slot [12182] located at 127.0.0.1:7002 "bar" redis 127.0.0.1:7002> get hello -> Redirected to slot [866] located at 127.0.0.1:7000 "world"
使用 cluster nodes查看節點資訊:
參考資料
Redis Cluster turorial: //redis.io/topics/cluster-tutorial#creating-the-cluster
在Windows系統下搭建Redis集群: //www.cnblogs.com/yy3b2007com/p/11033009.html