1. 程式人生 > 其它 >【Azure Redis 快取】Windows版建立 Redis Cluster 實驗 (精簡版)

【Azure Redis 快取】Windows版建立 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服務)。下載地址:https://github.com/MicrosoftArchive/redis/releases

2)Redis 3版本需要使用redis-trib.rb create 來執行叢集的建立工作,也需要從Github中下載 redis-trib.rb檔案(如無法下載,可在本文的附錄中複製)。下載地址:https://raw.githubusercontent.com/MSOpenTech/redis/3.0/src/redis-trib.rb

3)Ruby 執行環境(因為redis-trib.rb是Ruby語言編寫,所以需要在Windows本機中安裝Ruby執行環境)。下載地址:https://rubyinstaller.org/downloads/

4)Ruby Redis驅動 Redis gem。下載地址:

https://rubygems.org/gems/redis/versions/4.5.1

For Redis version 3 or 4, there is the older tool calledredis-trib.rbwhich is very similar. You can find it in thesrcdirectory of the Redis source code distribution. You need to installredisgem to be able to runredis-trib.

Source:https://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:https://redis.io/topics/cluster-tutorial#creating-the-cluster

在Windows系統下搭建Redis叢集: https://www.cnblogs.com/yy3b2007com/p/11033009.html

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!