參考:
一 所需軟件:Redis、Ruby語言運行環境、Redis的Ruby驅動redis-xxxx.gem、創建Redis集羣的工具redis-trib.rb
二 安裝配置redis
redis下載地址 https://github.com/MSOpenTech/redis/releases ; 下載Redis-x64-3.2.100.zip。
集羣規劃有三個節點的集羣,每個節點有一主一備。需要6臺虛擬機。
把 redis 解壓後,再複製出 5 份,配置 三主三從集羣。 由於 redis 默認端口號爲 6379,那麼其它5份的端口可以爲6380,6381,6382,6383,6384。 並且把目錄使用端口號命名
打開目錄6379下有一個文件 redis.windows.conf,修改裏面的端口號,以及集羣支持配置。
修改其他配置支持集羣
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
appendonly yes
如果cluster-enabled 不爲yes, 那麼在使用JedisCluster集羣代碼獲取的時候,會報錯。
cluster-node-timeout 調整爲 15000,那麼在創建集羣的時候,不會超時。
cluster-config-file nodes-6379.conf 是爲該節點的配置信息,這裏使用 nodes-端口.conf命名方法。服務啓動後會在目錄生成該文件。
編寫一個 bat 來啓動 redis,在每個節點目錄下建立 start.bat,內容如下:
title redis-6380
redis-server.exe redis.windows.conf
三 安裝Ruby
redis的集羣使用 ruby腳本編寫,所以系統需要有 Ruby 環境 ,下載地址 http://dl.bintray.com/oneclick/rubyinstaller/:rubyinstaller-2.3.3-x64.exe
安裝時3個選項都勾選。
四 安裝Redis的Ruby驅動redis-xxxx.gem
下載地址 https://rubygems.org/pages/download, 下載後解壓,當前目錄切換到解壓目錄中,如 D:\Program Files\redis\rubygems-2.6.12 然後在命令行執行 ruby setup.rb。
然後GEM 安裝 Redis :切換到redis安裝目錄,需要在命令行中,執行 gem install redis
五 安裝集羣腳本redis-trib
下載地址 https://raw.githubusercontent.com/antirez/redis/unstable/src/redis-trib.rb
打開該鏈接如果沒有下載,而是打開一個頁面,那麼將該頁面保存爲redis-trib.rb,建議保存到一個Redis的目錄下,例如放到6379目錄下。
集羣的命令爲redis-trib.rb create --replicas 1 127.0.0.1:6379 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
--replicas 1 表示每個主數據庫擁有從數據庫個數爲1。master節點不能少於3個,所以我們用了6個redis
六 啓動每個節點並且執行集羣構建腳本
把每個節點下的 start.bat雙擊啓動, 在切換到redis目錄在命令行中執行 redis-trib.rb create --replicas 1 127.0.0.1:6379 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
備註:有朋友反應上面的語句執行不成功。可以在前面加上ruby再運行。
ruby redis-trib.rb create --replicas 1 127.0.0.1:6379 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
在出現 Can I set the above configuration? (type 'yes' to accept): 請確定並輸入 yes 。成功後的結果如下:
七測試
使用Redis客戶端Redis-cli.exe來查看數據記錄數,以及集羣相關信息
命令 redis-cli –c –h ”地址” –p "端口號" ; c 表示集羣
輸入dbsize查詢
記錄總數
輸入cluster
info可以從客戶端的查看集羣的信息
八、搭建時出現的錯誤
PS C:\develop\redis> ruby redis-trib.rb create --replicas 1 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 127.0.0.1:7006
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
To get help about all subcommands, type:
redis-cli --cluster help
原因是redis-trib.rb的鏈接指向官網最新的版本。從對應版本(redis3.2.0即可)的源碼壓縮包中src文件夾下找到對應的redis-trib.rb文件使用,即可解決問題。
下載redis源碼版壓縮包:http://download.redis.io/releases/
參考博文
搭建:https://blog.csdn.net/zsg88/article/details/73715947
錯誤解決:https://www.cnblogs.com/learnapi/p/9523075.html