1. 下載redis安裝源碼包糠赦,下載鏈接地址:redis download料身。
2. 集群中redis集群節(jié)點(diǎn)端口號(hào)7000~7005麻掸,端口號(hào)即集群下各實(shí)例文件夾母廷,數(shù)據(jù)存儲(chǔ)在 端口號(hào)/data 文件夾中石抡。如圖1:
3. 復(fù)制執(zhí)行腳本檐嚣。如圖2:
4. 創(chuàng)建一個(gè)新的redis實(shí)例。如圖3:
5. 修改redis.conf啰扛。修改配置如下:
port 7000(每個(gè)節(jié)點(diǎn)的端口號(hào))
daemonize yes
bind 127.0.0.1(綁定當(dāng)前機(jī)器 IP)
dir /home/xiaodongsun/Documents/redis-cluster/7000/data/(數(shù)據(jù)文件存放位置)
pidfile /var/run/redis_7000.pid(pid 7000和port要對(duì)應(yīng))
cluster-enabled yes(啟動(dòng)集群模式)
cluster-config-file nodes7000.conf(7000和port要對(duì)應(yīng))
cluster-node-timeout 15000
appendonly yes
6. 復(fù)制5個(gè)實(shí)例嚎京。如圖4:
7. 修改7001~7005的redis.conf配置文件。如圖5:
vim redis.conf
#全局替換
:%s/7000/7001/g
檢查主要是替換4行:
port 7002
dir /usr/local/redis-cluster/7002/data/
cluster-config-file nodes-7002.conf
pidfile /var/run/redis_7002.pid
8. 分別啟動(dòng)6臺(tái)redis實(shí)例隐解。如圖6:
連接其中一臺(tái)測(cè)試:
CLUSTERDOWN Hash slot not served(不是提供散列的列槽)
安裝ruby環(huán)境(redis集群命令行工具redis-trib.rb命令需要借助ruby環(huán)境)
sudo apt-get install ruby
Ubuntu 16.04在搭建Redis Cluster搭建時(shí)鞍帝,使用gem install redis時(shí)出現(xiàn):ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /var/lib/gems/2.3.0 directory.
解決方式(參考官網(wǎng)安裝教程):
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install 2.3.1
rbenv global 2.3.1
ruby -v
重新運(yùn)行:gem install redis
9. 創(chuàng)建集群。如圖7:
創(chuàng)建完成后:
10. 客戶端連接redis集群測(cè)試
集群寫入數(shù)據(jù)報(bào)錯(cuò):
錯(cuò)誤原因: 連接參數(shù)需要添加 "-c"(集群連接方式)
到此基本上已完成初步的集群搭建更多信息參考官網(wǎng):?http://www.redis.cn/topics/cluster-tutorial.html
Java操作redis cluster:https://github.com/xiaodongsun-github/tutorials/blob/master/springboot-redis-cluster/src/main/java/com/example/SpringbootRedisClusterApplication.java