Redis 主從,哨兵,集群實戰(四)
- 2019 年 10 月 3 日
- 筆記
下載地址及版本說明
Redis 各版本下載地址: http://download.redis.io/releases/
版本說明:一般來說版本號第二位,偶數是穩定版本,奇數是在開發中的版本
本文基於Redis 版本為:3.2.12
理論依據文章為:
https://blog.csdn.net/sanri1993/article/details/101599701
https://blog.csdn.net/sanri1993/article/details/101620171
主從搭建
一主多從結構
結構
master 6379
slave 6380 -> 6379
slave 6381 -> 6379
搭建過程
- 配置節點
master 6379 redis6379.conf
port 6379 protected-mode no daemonize yes
slave 6380 redis6380.conf
port 6380 protected-mode no daemonize yes slaveof localhost 6379
slave 6381 redis6381.conf
port 6381 protected-mode no daemonize yes slaveof localhost 6379
- 啟動
./redis-server conf/redis6379.conf ./redis-server conf/redis6380.conf ./redis-server conf/redis6381.conf
- 查看是否啟動成功
# 先看進程是否啟動 netstat -tlnp | grep -E "6379|6380|6381" # 查看拓撲結構 127.0.0.1:6379> info Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=10459,lag=0 slave1:ip=127.0.0.1,port=6381,state=online,offset=10459,lag=0
樹狀主從結構
結構
master 6379
slave 6380 -> 6379
slave 6381 -> 6380
搭建過程
和一主多從有變化的配置,其它環節一致
slave 6381 redis6381.conf
port 6381 protected-mode no daemonize yes slaveof localhost 6380
哨兵環境搭建
現切換成一主多從結構 ,在一主多從的情況下,搭建三個哨兵
結構
sentinel 26379 sentinel 26380 sentinel 26381
master 6379
slave 6380 -> 6379
slave 6381 -> 6379
搭建過程
- 配置哨兵節點
sentinel 26379
port 26379 daemonize yes sentinel monitor mymaster 127.0.0.1 6379 2
sentinel 26380
port 26380 daemonize yes sentinel monitor mymaster 127.0.0.1 6379 2
sentinel 26381
port 26381 daemonize yes sentinel monitor mymaster 127.0.0.1 6379 2
- 啟動哨兵
./redis-sentinel conf/sentinel26379.conf ./redis-sentinel conf/sentinel26380.conf ./redis-sentinel conf/sentinel26381.conf
- 查看是否啟動成功
# 查看進程是否啟動成功 ps aux | grep sentinel # 關閉主節點,看是否會選舉一台從節點成為主節點 127.0.0.1:6379> shutdown # 重啟之前的主節點,一段時間後,查看是否成為 slave 節點
集群環境搭建
結構
cluster master ->cluster slave
master 6379 -> slave6389
master 6380 -> slave6390
master 6381 -> slave6391
搭建過程
- 配置節點配置資訊
master 6379
port 6379 protected-mode no daemonize yes cluster-enabled yes cluster-node-timeout 15000 # . 相對於命令運行路徑,最好寫絕對路徑 cluster-config-file ./nodes-6379.conf
其它 master slave 只需要修改埠號即可,然後使用 ./redis-server <configfile>
啟動
- 建立集群通訊
# 將集群的每一個節點建立通訊 192.168.108.128:6379>cluster meet ip port # 查詢集群節點 192.168.108.128:6379>cluster nodes
- 映射數據槽
redis集群有16384個哈希槽,要把所有數據映射到16384槽,需要批量設置槽
# 查詢集群狀態 192.168.108.128:6381> cluster info cluster_state:fail # 顯示為 fail 是還沒有映射槽,還不能提供服務 redis-cli -h 192.168.108.128 -p 6379 cluster addslots {0..5461} redis-cli -h 192.168.108.128 -p 6380 cluster addslots {5462..10922} redis-cli -h 192.168.108.128 -p 6381 cluster addslots {10923..16383}
- 配置主從,在從節點上操作,複製哪一個 master
192.168.152.128:6389> cluster replicate 9b7b0c22f95eb01fb9935ad4b04d396c7f99e881 192.168.152.128:6390> cluster replicate 5351c088472467ae485ed519cea271efda646bfa 192.168.152.128:6391> cluster replicate e718f126278072e1e180c3e518d73e0bc877b3dc
- 測試集群是否正常工作
# 登錄上之後,執行不同的 set 操作,看能否跳轉至其它節點執行命令 ./redis-cli -c
SpringBoot 連接 redis 集群
- 引入 spring-boot-starter-data-redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
- 配置節點資訊
spring.redis.cluster.nodes=192.168.108.128:6379,192.168.108.128:6380,192.168.108.128:6381
一點小推廣
創作不易,希望可以支援下我的開源軟體,及我的小工具,歡迎來 gitee 點星,fork ,提 bug 。
Excel 通用導入導出,支援 Excel 公式
部落格地址:https://blog.csdn.net/sanri1993/article/details/100601578
gitee:https://gitee.com/sanri/sanri-excel-poi
使用模板程式碼 ,從資料庫生成程式碼 ,及一些項目中經常可以用到的小工具
部落格地址:https://blog.csdn.net/sanri1993/article/details/98664034
gitee:https://gitee.com/sanri/sanri-tools-maven