集羣:一組通過網絡連接的計算機,共同對外提供服務,像一個獨立的服務器。
redis配置文件
port 7001 # 端口號
bind 172.16.179.130 # ip
daemonize yes # 後臺運行
pidfile 7001.pid # 進程文件
cluster-enabled yes # 可以作爲集羣節點
cluster-config-file 7001_node.conf # 作爲集羣節點的配置文件
cluster-node-timeout 15000 # 集羣連接的超時時間
appendonly yes # 數據文件追加
redis集羣模擬
1 設置配置文件,啓動節點
1) 第一臺服務器,啓動三個節點
python@ubuntu:~/Desktop/redis_cluster/conf$ cat 7001.conf
port 7001
bind 192.168.138.134
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
python@ubuntu:~/Desktop/redis_cluster/conf$ ll
總用量 44
drwxrwxr-x 2 python python 4096 6月 20 15:52 ./
drwxrwxr-x 3 python python 4096 6月 20 15:45 ../
-rwxr-xr-x 1 python python 159 6月 20 15:48 7001.conf*
-rw-r--r-- 1 python python 112 6月 20 15:52 7001_node.conf
-rw-rw-r-- 1 python python 6 6月 20 15:52 7001.pid
-rwxr-xr-x 1 python python 159 6月 20 15:48 7002.conf*
-rw-r--r-- 1 python python 112 6月 20 15:52 7002_node.conf
-rw-rw-r-- 1 python python 6 6月 20 15:52 7002.pid
-rwxr-xr-x 1 python python 159 6月 20 15:49 7003.conf*
-rw-r--r-- 1 python python 112 6月 20 15:52 7003_node.conf
-rw-rw-r-- 1 python python 6 6月 20 15:52 7003.pid
-rw-r--r-- 1 python python 0 6月 20 15:52 appendonly.aof
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server 7001.conf
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server 7002.conf
python@ubuntu:~/Desktop/redis_cluster/conf$ redis-server 7003.conf
2) 第二臺服務器,啓動三個節點
python@ubuntu:~/Desktop$ cat 7004.conf
port 7004
bind 192.168.138.138
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
python@ubuntu:~/Desktop$ redis-server 7004.conf
python@ubuntu:~/Desktop$ redis-server 7005.conf
python@ubuntu:~/Desktop$ redis-server 7006.conf
2 創建集羣
redis的安裝包中包含了redis-trib.rb,用於創建集羣
將命令複製,這樣可以在任何目錄下調用此命令
sudo cp /usr/local/redis/src/redis-trib.rb /usr/local/bin/
因爲該服務是ruby開發,首先安裝ruby環境
sudo apt-get install ruby
創建集羣
redis-trib.rb create --replicas 1 192.168.138.134:7001 192.168.138.134:7002 192.168.138.134:7003 192.168.138.138:7004 192.168.138.138:7005 192.168.138.138:7006
根據log可以看到主從節點
通過測試發現配置成功
Python 與 集羣的交互
from rediscluster import *
if __name__ == '__main__':
try:
# 構建所有的節點,Redis會使⽤CRC16算法,將鍵和值寫到某個節點上
startup_nodes = [
{'host': '192.168.26.128', 'port': '7000'},
{'host': '192.168.26.130', 'port': '7003'},
{'host': '192.168.26.128', 'port': '7001'},
]
# 構建StrictRedisCluster對象
src=StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
# 設置鍵爲name、值爲itheima的數據
result=src.set('name','itheima')
print(result)
# 獲取鍵爲name
name = src.get('name')
print(name)
except Exception as e:
print(e)
擴展:redis集羣存儲數據算法
在哪個服務器上寫數據:CRC16
- redis cluster在設計的時候,就考慮到了去中⼼化,去中間件,也就是說,集羣中 的每個節點都是平等的關係,都是對等的,每個節點都保存各⾃的數據和整個集 羣的狀態。每個節點都和其他所有節點連接,⽽且這些連接保持活躍,這樣就保 證了我們只需要連接集羣中的任意⼀個節點,就可以獲取到其他節點的數據
- Redis集羣沒有並使⽤傳統的⼀致性哈希來分配數據,⽽是採⽤另外⼀種叫做哈希 槽 (hash slot)的⽅式來分配的。redis cluster 默認分配了 16384 個slot,當我們 set⼀個key 時,會⽤CRC16算法來取模得到所屬的slot,然後將這個key 分到哈 希槽區間的節點上,具體算法就是:CRC16(key) % 16384。所以我們在測試的 時候看到set 和 get 的時候,直接跳轉到了7000端⼝的節點
- Redis 集羣會把數據存在⼀個 master 節點,然後在這個 master 和其對應的salve 之間進⾏數據同步。當讀取數據時,也根據⼀致性哈希算法到對應的 master 節 點獲取數據。只有當⼀個master 掛掉之後,纔會啓動⼀個對應的 salve 節點,充 當 master
- 需要注意的是:必須要3個或以上的主節點,否則在創建集羣時會失敗,並且當存 活的主節點數⼩於總節點數的⼀半時,整個集羣就⽆法提供服務了