一、Redis6.X高可用之Cluster介绍
背景
Sentinel解决了主从架构自动迁移的能力
但是Master节点写能力和存储能力受限
使用Redis的集群Cluster就是为了解决单机Redis容量问题
什么是集群
一组相互独立,通过高速网络互联的计算机,它们构成了一个组并以单一系统的模式加以管理。
Redis集群模式介绍
Cluster模式是Redis3.0开始提供
采用无中心结构,每个节点保存数据和整个集群状态,每个阶段性都和其他所有节点连接
至少6节点才能保证高可用,即三主三从;各节点互相通信,扩展性强
数据分散存储到各个节点
说明
旧版本需要使用Ruby语言进行构建,新版5之后可以直接用redis-cli即可
Redis集群预分好16384个槽,当需要在redis集群中放一个key-value时,根据CRC16(key)mod 16384的值,决定放到哪个槽
二、集群配置文件
redis.conf
bind 0.0.0.0
port 6379
daemonize yes
requirepass "123456"
logfile "/usr/local/redis/log/redis.log"
dbfilename "test.rdb"
dir "/usr/local/redis/data"
appendonly yes
appendfilename "appendonly.aof"
masterauth "123456"
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 20000
## 不同机器填写各自ip
cluster-announce-ip 192.168.0.114
cluster-announce-port 6379
## 集群节点总线端口
cluster-announce-bus-port 16379
依次在6台机器配置上述配置后启动redis
## 启动redis
./redis-server /data/redis/cluster/redis.conf
## 创建集群(任意一个节点执行)
./redis-cli -a 123456 --cluster create 192.168.0.114:6379 192.168.0.115:6379 192.168.0.116:6379 192.168.0.117:6379 192.168.0.118:6379 192.168.0.119:6379 --cluster-replicas 1
启动一直连接中检查配置文件是否正确和各机器能否正常通信
三、SpringBoot整合redis集群配置
spring:
cache:
type: redis
redis:
password: 123456
client-type: lettuce
lettuce:
pool:
max-active: 10
max-idle: 10
min-idle: 0
max-wait: -1ms
cluster:
max-redirects: 3
nodes: 192.168.0.114:6379,192.168.0.115:6379,192.168.0.116:6379,192.168.0.117:6379,192.168.0.118:6379,192.168.0.119:6379