Redis 各版本下載地址: http://download.redis.io/releases/node
版本說明:通常來講版本號第二位,偶數是穩定版本,奇數是在開發中的版本redis
本文基於Redis 版本爲:3.2.12
spring
理論依據文章爲:shell
https://blog.csdn.net/sanri1993/article/details/101599701spring-boot
https://blog.csdn.net/sanri1993/article/details/101620171測試
master 6379 .net
slave 6380 -> 63793d
slave 6381 -> 6379code
master 6379 redis6379.confserver
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}
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
<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