從 https://github.com/tporadowski/redis/releases 下載windows版的redis,自行下載解壓。
關於哨兵模式的講解,強烈推薦 【深入學習redis(4):哨兵】
一主兩從
-
復制三份 redis.windows.conf,分別如下配置
redis-6379.windows.conf
bind 127.0.0.1 port 6379
redis-6380.windows.conf
bind 127.0.1 port 6380 slaveof 127.0.0.1 6379
redis-6381.windows.conf
bind 127.0.1 port 6381 slaveof 127.0.0.1 6379
-
啟動
redis-server.exe redis-6379.windows.conf redis-server.exe redis-6380.windows.conf redis-server.exe redis-6381.windows.conf
-
驗證是否成功,看到從節點狀態 online 說明主從環境搭建成功了
redis-cli.exe -h 127.0.0.1 -p 6379 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=98,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=98,lag=0 master_replid:677245c1292f2244597f22a12c85730f236fa707 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:98 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:98
三個哨兵
-
創建三個配置文件
sentinel-26379.windows.conf
bind 127.0.0.1 port 26379 sentinel monitor mymaster 127.0.0.1 6379 2
sentinel-26380.windows.conf
bind 127.0.0.1 port 26380 sentinel monitor mymaster 127.0.0.1 6379 2
sentinel-26381.windows.conf
bind 127.0.0.1 port 26381 sentinel monitor mymaster 127.0.0.1 6379 2
-
啟動哨兵
redis-server.exe sentinel-26379.windows.conf --sentinel redis-server sentinel-26380.windows.conf --sentinel redis-server sentinel-26381.windows.conf --sentinel
-
驗證是否成功,看到最后 status=ok 說明成功了
127.0.0.1:26379> info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
-
演示故障自動切換
關掉主節點 6379,等一會,就會發現哨兵切換了主節點,重新啟動 6379 節點,它就變成了從節點了
redis-cli.exe -h 127.0.0.1 -p 6379 127.0.0.1:6379> info replication # Replication role:slave master_host:127.0.0.1 master_port:6380 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:56440 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:8f282577c2e4ddeb9794f88757e5dad7870e5e6d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:56440 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:55487 repl_backlog_histlen:954
遇到的問題
代碼客戶端連接redis,報錯 All sentinels down, cannot determine where is mymaster master is running... 或者 Could not get a resource from the pool
參考這篇博客:https://www.jianshu.com/p/098494958892
好了,認真讀過 【深入學習redis(4):哨兵】 ,所以這個問題已經不再是問題了
通過客戶端原理的介紹,可以加深對哨兵功能的理解:
(1)配置提供者:客戶端可以通過哨兵節點+masterName獲取主節點信息,在這里哨兵起到的作用就是配置提供者。
需要注意的是,哨兵只是配置提供者,而不是代理。二者的區別在於:如果是配置提供者,客戶端在通過哨兵獲得主節點信息后,會直接建立到主節點的連接,后續的請求(如set/get)會直接發向主節點;如果是代理,客戶端的每一次請求都會發向哨兵,哨兵再通過主節點處理請求。
舉一個例子可以很好的理解哨兵的作用是配置提供者,而不是代理。在前面部署的哨兵系統中,將哨兵節點的配置文件進行如下修改:
sentinel monitor mymaster 192.168.92.128 6379 2 改為 sentinel monitor mymaster 127.0.0.1 6379 2
然后,將前述客戶端代碼在局域網的另外一台機器上運行,會發現客戶端無法連接主節點;這是因為哨兵作為配置提供者,客戶端通過它查詢到主節點的地址為127.0.0.1:6379,客戶端會向127.0.0.1:6379建立redis連接,自然無法連接。如果哨兵是代理,這個問題就不會出現了。
注意一點:哨兵程序監控端口和 redis 服務端口都要開放給客戶端,否則就訪問不通。
我本地開發環境就是這種情況,可以訪問通監控端口,但是無法訪問redis服務端口。這也再次說明了哨兵只是配置提供者,而不是代理。