一、redis安装部署
- 下载redis最新安装包
wget http://download.redis.io/releases/redis-6.0.6.tar.gz
- 升级gcc
centos7 默认的 gcc 默认是4.8.5,版本小于 5.3 无法编译,需要先安装gcc新版才能编译
yum -y install gcc-c++
升级gcc #这里要升级一下,否则编译时候会报错
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
- 解压、安装
tar -zxvf redis-6.0.6.tar.gz -C /opt/local
cd /usr/local/redis-6.0.6/
make
cd src/
make install
- 修改默认配置
cp /usr/local/redis-6.0.0/redis.conf /etc/redis/
# 修改redis配置文件
vi /etc/redis/redis.conf
--------------------------------------------------------------------------------
#不限制远程连接
bind 0.0.0.0
#设置连接密码
requirepass "123456"
#允许后台启动redis
daemonize yes
--------------------------------------------------------------------------------
- 启动redis服务
/usr/local/redis-6.0.0/src/redis-server /etc/redis/redis.conf /etc/redis/redis.conf
[root@iZ2zejaz33icbod2k4cvy6Z redis-5.0.0]# redis-cli
--------------------------------------------------------------------------------
127.0.0.1:6379> auth 123456
OK
--------------------------------------------------------------------------------
二、redis cluster集群
2.1 cluster集群介绍
哨兵模式的缺陷
- 在哨兵模式中,仍然只有一个Master节点。当并发写请求较大时,哨兵模式并不能缓解写压力。我们知道只有主节点才具有写能力,那如果在一个集群中,能够配置多个主节点,就可以缓解写压力了,这个就是redis-cluster集群模式。
Redis-cluster集群概念
-
由多个Redis服务器组成的分布式网络服务集群;
-
集群之中有多个Master主节点,每一个主节点都可读可写;
-
节点之间会互相通信,两两相连;
-
Redis集群无中心节点。
集群节点复制
在Redis-Cluster集群中,可以给每一个主节点添加从节点,主节点和从节点直接遵循主从模型的特性。
当用户需要处理更多读请求的时候,添加从节点可以扩展系统的读性能。
故障转移
Redis集群的主节点内置了类似Redis Sentinel的节点故障检测和自动故障转移功能,当集群中的某个主节点下线时,集群中的其他在线主节点会注意到这一点,并对已下线的主节点进行故障转移。
集群进行故障转移的方法和Redis Sentinel进行故障转移的方法基本一样,不同的是,在集群里面,故障转移是由集群中其他在线的主节点负责进行的,所以集群不必另外使用Redis Sentinel
集群分片策略
-
集群将整个数据库分为16384个槽位slot,所有key-value数据都存储在这些slot中的某一个上。一个slot槽位可以存放多个数据,key的槽位计算公式为:slot_number=crc16(key)%16384,其中crc16为16位的循环冗余校验和函数。
-
集群中的每个主节点都可以处理0个至16383个槽,当16384个槽都有某个节点在负责处理时,集群进入上线状态,并开始处理客户端发送的数据命令请求。
集群redirect转向
- 由于Redis集群无中心节点,请求会随机发给任意主节点;
- 主节点只会处理自己负责槽位的命令请求,其它槽位的命令请求,该主节点会返回客户端一个转向错误;
- 客户端根据错误中包含的地址和端口重新向正确的负责的主节点发起命令请求。
2.2 cluster集群安装配置
- 准备工作
# 存放集群日志
mkdir -p /usr/local/redis-6.2.3/log
# 存放集群配置
mkdir -p /etc/redis
- /etc/redis下创建redis1.conf、redis2.conf…redis6.conf集群配置文件
cp /usr/local/redis-6.2.3/redis.conf /etc/redis/
cp /etc/redis/redis.conf /etc/redis/redis1.conf
cp /etc/redis/redis.conf /etc/redis/redis2.conf
cp /etc/redis/redis.conf /etc/redis/redis3.conf
cp /etc/redis/redis.conf /etc/redis/redis4.conf
cp /etc/redis/redis.conf /etc/redis/redis5.conf
cp /etc/redis/redis.conf /etc/redis/redis6.conf
- 配置
bind 0.0.0.0
port 6379 # 6379-6384
daemonize yes
requirepass "123456"
logfile "/usr/local/redis-6.0.0/log/redis1.log" #日志文件
dbfilename "redis1.rdb"
appendonly yes
appendfilename "redis1.aof"
masterauth "123456"
cluster-enabled yes #是否开启集群
cluster-config-file nodes-6379.conf # 生成的node文件,记录集群节点信息,默认为nodes.conf,防止冲突,改为nodes-6379.conf
cluster-node-timeout 20000 #节点连接超时时间
cluster-announce-ip 47.91.232.xxx #集群节点的ip,当前节点的ip
cluster-announce-port 6379 #集群节点映射端口
cluster-announce-bus-port 16379 #集群节点总线端口,节点之间互相通信,常规端口+1万
- 启动六个节点
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis1.conf
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis2.conf
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis3.conf
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis4.conf
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis5.conf
/usr/local/redis-6.2.3/src/redis-server /etc/redis/redis6.conf
- 加入集群(其中一个节点执行即可)
cluster :构建集群全部节点信息
cluster-replicas 1 : 主从节点的比例,1表示1主1从的方式
./redis-cli -a 123456 --cluster create 47.91.232.xxx:6379 47.91.232.xxx:6380 47.91.232.xxx:6381 47.91.232.xxx:6382 47.91.232.xxx:6383 47.91.232.xxx:6384 --cluster-replicas 1
---------------------------------------------------------------------------------
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 47.91.232.223:6383 to 47.91.232.223:6379
Adding replica 47.91.232.223:6384 to 47.91.232.223:6380
Adding replica 47.91.232.223:6382 to 47.91.232.223:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3 47.91.232.223:6379
slots:[0-5460] (5461 slots) master
M: 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b 47.91.232.223:6380
slots:[5461-10922] (5462 slots) master
M: 3158512db2a27bcd9ea74adc892f82b41f88a19f 47.91.232.223:6381
slots:[10923-16383] (5461 slots) master
S: 41892c75cb1d3cffbbab2802dda9cfeebb6dbaf0 47.91.232.223:6382
replicates 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b
S: 3e3c4574e862bb55d4ca843620e00ccc62a3c114 47.91.232.223:6383
replicates 3158512db2a27bcd9ea74adc892f82b41f88a19f
S: 9d8805af2c507950009d1d147367ed0d79fd0788 47.91.232.223:6384
replicates c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 47.91.232.223:6379)
M: c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3 47.91.232.223:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 41892c75cb1d3cffbbab2802dda9cfeebb6dbaf0 47.91.232.223:6382
slots: (0 slots) slave
replicates 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b
M: 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b 47.91.232.223:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 3158512db2a27bcd9ea74adc892f82b41f88a19f 47.91.232.223:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3e3c4574e862bb55d4ca843620e00ccc62a3c114 47.91.232.223:6383
slots: (0 slots) slave
replicates 3158512db2a27bcd9ea74adc892f82b41f88a19f
S: 9d8805af2c507950009d1d147367ed0d79fd0788 47.91.232.223:6384
slots: (0 slots) slave
replicates c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
-------------------------------------------------------------------------------------
- 检查集群状态信息(其中一个节点执行)
./redis-cli -a 123456 --cluster check 47.91.232.xxx:6379
----------------------------------------------------------------------------------------
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
47.91.232.223:6379 (c29f79a0...) -> 0 keys | 5461 slots | 1 slaves.
47.91.232.223:6380 (72c78062...) -> 0 keys | 5462 slots | 1 slaves.
47.91.232.223:6381 (3158512d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 47.91.232.223:6379)
M: c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3 47.91.232.223:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 41892c75cb1d3cffbbab2802dda9cfeebb6dbaf0 47.91.232.223:6382
slots: (0 slots) slave
replicates 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b
M: 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b 47.91.232.223:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 3158512db2a27bcd9ea74adc892f82b41f88a19f 47.91.232.223:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 3e3c4574e862bb55d4ca843620e00ccc62a3c114 47.91.232.223:6383
slots: (0 slots) slave
replicates 3158512db2a27bcd9ea74adc892f82b41f88a19f
S: 9d8805af2c507950009d1d147367ed0d79fd0788 47.91.232.223:6384
slots: (0 slots) slave
replicates c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
----------------------------------------------------------------------------------------
[root@nodec src]# /usr/local/redis-6.2.3/src/redis-cli -p 6379 -c -a 123456
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:438
cluster_stats_messages_pong_sent:426
cluster_stats_messages_sent:864
cluster_stats_messages_ping_received:421
cluster_stats_messages_pong_received:438
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:864
127.0.0.1:6379> cluster nodes
41892c75cb1d3cffbbab2802dda9cfeebb6dbaf0 47.91.232.xxx:6382@16382 slave 72c780626adf202bc6fa9eaeca14e7b4c7d4b19b 0 1623736147294 2 connected
c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3 47.91.232.xxx:6379@16379 myself,master - 0 1623736148000 1 connected 0-5460
72c780626adf202bc6fa9eaeca14e7b4c7d4b19b 47.91.232.xxx:6380@16380 master - 0 1623736148298 2 connected 5461-10922
3158512db2a27bcd9ea74adc892f82b41f88a19f 47.91.232.xxx:6381@16381 master - 0 1623736149303 3 connected 10923-16383
3e3c4574e862bb55d4ca843620e00ccc62a3c114 47.91.232.xxx:6383@16383 slave 3158512db2a27bcd9ea74adc892f82b41f88a19f 0 1623736146290 3 connected
9d8805af2c507950009d1d147367ed0d79fd0788 47.91.232.xxx:6384@16384 slave c29f79a0d9a5ed2b5d73ad8b7fb48bff82540fd3 0 1623736146000 1 connected
127.0.0.1:6379> set name hld
-> Redirected to slot [5798] located at 47.91.232.223:6380
OK
47.91.232.223:6380>
- 测试集群读写命令set/get
key哈希运算计算槽位置
槽在当前节点的话直接插入/读取,否则自动转向到对应的节点
127.0.0.1:6379> set name hld
-> Redirected to slot [5798] located at 47.91.232.223:6380 #计算槽位在6380端口,转向6380端口
OK
47.91.232.223:6380> set age 27
-> Redirected to slot [741] located at 47.91.232.223:6379 #计算槽位在6379端口,转向6379端口
OK
47.91.232.223:6379> get test
-> Redirected to slot [6918] located at 47.91.232.223:6380 #计算槽位在6380端口,转向6380端口
"123456"
47.91.232.223:6380> get age
-> Redirected to slot [741] located at 47.91.232.223:6379 #计算槽位在6379端口,转向6379端口
"27"
47.91.232.223:6379>
- springboot2.x整合cluster配置
cluster:
#命名的最多转发次数
max-redirects: 3
nodes: 8.129.113.233:6381,8.129.113.233:6382,8.129.113.233:6383,8.129.113.233:6384,8.129.113.233:6385,8.129.113.233:6386