(1)集羣原理node
在Redis集羣中,全部的Redis節點彼此互聯,節點內部使用二進制協議優化傳輸速度和帶寬。 當一個節點掛掉後,集羣中超過半數的節點檢測失效時才認爲該節點已失效。不一樣於Tomcat集羣 須要使用反向代理服務器,Redis 集羣中的任意節點均可以直接和Java客戶端鏈接。Redis 集羣上 的數據分配則是採用哈希槽(HASH SLOT),Redis集羣中內置了16384 個哈希槽,當有數據須要 存儲時,Redis會首先使用CRC16算法對key進行計算,將計算得到的結果對16384取餘,這樣每 一個key都會對應一個取值在0~16383之間的哈希槽,Redis則根據這個餘數將該條數據存儲到對 應的Redis節點上,開發者可根據每一個Redis實例的性能來調整每一個Redis實例上哈希槽的分佈範 圍。redis
(2)集羣規劃算法
本案例在同一臺服務器上用不一樣的端口表示不一樣的Redis服務器(僞分佈式集羣)。 主節點: 192.168.248.144:8001, 192. 168.248.144:8002, 192.1 68.248.144:8003。 從節點: 192.168.248.144:8004, 192. 168.248.144:8005, 192. 168.248.144:8006。shell
(3)集羣配置ruby
Redis集羣管理工具redis-trib.rb依賴Ruby環境,首先須要安裝Ruby環境。bash
安裝步驟:服務器
1.導入公鑰curl
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
2.輸入下面的命令來請求安裝分佈式
\curl -sSL https://get.rvm.io | bash -s stable
3.shell環境更新一下,rvm -v 顯示版本工具
source ~/.bashrc source ~/.bash_profile source /usr/local/rvm/scripts/rvm
4.列出已知的 Ruby 版本
rvm list known
5.安裝rvm
rvm install 2.5.1
6.安裝 redis依賴
7.將下載好的redis編譯安裝
mkdir redisCluster CP -f ./redis-4.0.10.tar.gz ./redisCluster/ cd redisCluster tar -zxvf redis-4.0.10.tar.gz cd redis-4.0.10 make MALLOClibc make install
8 複製redis-trib.rb到redisCluster下
9.在redisCluster下建立6個文件夾(8001-8006),並將redis.conf複製到8001-8006下,更改每個redis.conf
8001爲例: port 8001 #bind 127.0.0.1 cluster-enabled yes cluster-config-file node-8001.conf protected-mode no daemonize yes requirepass 123456 masterauth 123456
10.進入redis-5.0.5啓動6個redis,回到redisCluster目錄下
經過指令找到安裝的redis在ruby環境中的配置client.rb
打開client.rb並修改密碼
11.建立集羣
redis-cli --cluster create 192.168.205.100:8001 192.168.20 5.100:8002 192.168.205.100:8003 192.168.205.100:8004 192.168.205.100:8005 192.168.205.100:8006 --cluster-replicas 1 -a 123456
Can I set the above configuration? (type 'yes' to accept): yes,該處請輸入yes,否則好像分配不了哈希槽
這樣就好了
測試:
[root@localhost redis-5.0.5]# redis-cli -p 8001 -a 123456 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.127.0.0.1:8001> auth 123456 OK 127.0.0.1:8001> CLUSTER nodes 4c3938eb52b31c3dab74d0edb878d6c42a711a7a 192.168.205.100:8003@18003 master - 0 1582461427 000 3 connected 10923-16383b4dfec3ff7490fd7207bdf7f48d0212d8596ba15 192.168.205.100:8001@18001 myself,master - 0 158 2461424000 1 connected 0-5460adb99416cc376b767198aa606042e98b904a1544 192.168.205.100:8002@18002 master - 0 1582461425 834 2 connected 5461-10922f83c3c955420221b4939617e1520d5ba9370b1d8 192.168.205.100:8004@18004 slave adb99416cc376b7 67198aa606042e98b904a1544 0 1582461426000 2 connected2bbfdf6085405753ef5931cb0a883db9043a2f22 192.168.205.100:8006@18006 slave b4dfec3ff7490fd 7207bdf7f48d0212d8596ba15 0 1582461426844 6 connected4603f708c2e106cd2c227a7c7eb6986ca62b293c 192.168.205.100:8005@18005 slave 4c3938eb52b31c3 dab74d0edb878d6c42a711a7a 0 1582461427859 5 connected127.0.0.1:8001> set k1 測試 -> Redirected to slot [12706] located at 192.168.205.100:8003 OK 192.168.205.100:8003> exit [root@localhost redis-5.0.5]# redis-cli -p 8002 -a 123456 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.127.0.0.1:8002> auth 123456 OK 127.0.0.1:8002> get k1 -> Redirected to slot [12706
ok