redis下載地址 https://github.com/MSOpenTech/redis/releases ; 下載Redis-x64-3.2.100.zip。html
集羣規劃有三個節點的集羣,每一個節點有一主一備。須要6臺虛擬機。node
把 redis 解壓後,再複製出 5 份,配置 三主三從集羣。 因爲 redis 默認端口號爲 6379,那麼其它5份的端口能夠爲6380,6381,6382,6383,6384。 而且把目錄使用端口號命名git
打開目錄6379下有一個文件 redis.windows.conf,修改裏面的端口號,以及集羣支持配置。github
修改其餘配置支持集羣redis
cluster-enabled yes數據庫
cluster-config-file nodes-6379.confwindows
cluster-node-timeout 15000ruby
appendonly yesapp
若是cluster-enabled 不爲yes, 那麼在使用JedisCluster集羣代碼獲取的時候,會報錯。工具
cluster-node-timeout 調整爲 15000,那麼在建立集羣的時候,不會超時。
cluster-config-file nodes-6379.conf 是爲該節點的配置信息,這裏使用 nodes-端口.conf命名方法。服務啓動後會在目錄生成該文件。
注意:放開註釋後,前面不能有空格
編寫一個 bat 來啓動 redis,在每一個節點目錄下創建 start.bat,內容以下:
@echo 啓動redis開始
::redis路徑
d:
cd D:\mhkycompany\tool\redis\6379\Redis-x64-3.2.100
redis-server.exe redis.windows.conf
echo 啓動redis完成
pause
redis的集羣使用 ruby腳本編寫,因此係統須要有 Ruby 環境 ,下載地址 http://dl.bintray.com/oneclick/rubyinstaller/rubyinstaller-2.2.4-x64.exe
安裝時3個選項都勾選。
下載地址 https://rubygems.org/pages/download, 下載後解壓,當前目錄切換到解壓目錄中,如 D:\Program Files\redis\rubygems-2.6.12 而後在命令行執行 ruby setup.rb。
而後GEM 安裝 Redis :切換到redis安裝目錄,須要在命令行中,執行 gem install redis
下載地址 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能夠從客戶端的查看集羣的信息
=====================================
注意:若是出現建立集羣不成功:
dos命令窗口執行建立集羣命令,出現如下提示:
D:\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 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: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 --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/
至此,redis集羣搭建成功,可在項目中測試redis集羣。
其餘博文參考: https://blog.csdn.net/zsg88/article/details/73715947
https://www.cnblogs.com/super-chao/p/9329018.html
https://www.jianshu.com/p/783b4a319d22