概述
文章目录
- 一、节点分布总览
- 二、软件配置初始化
- 三、集群配置修改
- 3.1. redis-7002.conf
- 3.2. redis-7003.conf
- 3.3. redis-8001.conf
- 3.4. redis-8002.conf
- 3.5. redis-8003.conf
- 3.6. redis启动
- 四、 节点握手
- 4.1. 节点握手
- 4.2. 操作日志如下:
- 五、槽位分配和配置主从
- 5.1. 槽位分配
- 5.2. 主从复制分配
- 5.3. 集群验证
Redis 分布式集群能解决的问题 | 集群 概念 |
---|---|
解决现有系统单节点并发压力和物理上限问题 | 通过添加服务器的数量,提供相同的服务,从而让服务器达到一个稳定、高效的状态 |
一、节点分布总览
演示案例
3主3从 横向扩展
服务器 | 端口 | 节点说明 |
---|---|---|
192.168.0.114 | 7001 | master |
192.168.0.114 | 7002 | master |
192.168.0.114 | 7003 | master |
192.168.0.114 | 8001 | slave |
192.168.0.114 | 8002 | slave |
192.168.0.114 | 8003 | slave |
二、软件配置初始化
- 下载
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
- 解压
cd /app
tar -zxvf redis-6.2.6.tar.gz
- 编译安装
cd redis-6.2.6/
make install
- 配置抽离
mkdir /app/redis-cluster/
cp /app/redis-6.2.6/redis.conf /app/redis-cluster/
- 编辑配置
vim /app/redis-cluster/redis.conf
-
修改配置7处:
bind127.0.0.1
改为 bind0.0.0.0
port6379
改为 port7001
daemonizeno
改为 daemonizeyes
pidfile /var/run/redis_6379
.pid 改为 pidfile /var/run/redis_7001
.pid
注释打开cluster-enabled yes
注释打开 cluster-node-timeout 15000
注释打开 nodes-6379
.conf 改为nodes-7001
.conf -
集群配置分配
将/app/redis-cluster/redis.conf复制6份并重命名
cd app/redis-cluster/
cp redis.conf redis-7001.conf
cp redis.conf redis-7002.conf
cp redis.conf redis-7003.conf
cp redis.conf redis-8001.conf
cp redis.conf redis-8002.conf
cp redis.conf redis-8003.conf
三、集群配置修改
分别修改redis-7002.conf、redis-7003.conf、redis-8001.conf、redis-8002.conf、redis-8003.conf
3.1. redis-7002.conf
vim redis-7002.conf
port 把7001
修改为7002
将7001
.pid 修改为 7002
.pid
将nodes-7001
.conf 修改为 nodes-7002
.conf
3.2. redis-7003.conf
vim redis-7003.conf
port 把7001
修改为7003
将7001
.pid 修改为 7003.pid
将nodes-7001
.conf 修改为 nodes-7003
.conf
3.3. redis-8001.conf
vim redis-8001.conf
port 把7001
修改为8001
将7001
.pid 修改为 8001
.pid
将nodes-7001
.conf 修改为 nodes-8001
.conf
3.4. redis-8002.conf
vim redis-8002.conf
92行 port 把7001
修改为8002
将7001
.pid 修改为 8002
.pid
将nodes-7001
.conf 修改为 nodes-8002
.conf
3.5. redis-8003.conf
vim redis-8003.conf
port 把7001
修改为8003
将7001
.pid 修改为 8003
.pid
将nodes-7001
.conf 修改为 nodes-8003
.conf
3.6. redis启动
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-7001.conf
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-7002.conf
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-7003.conf
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-8001.conf
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-8002.conf
/app/redis-6.2.6/src/redis-server /app/redis-cluster/redis-8003.conf
查看进程:
ps -ef |grep redis
操作日志如下:
[root@localhost ~]# ps -ef |grep redis
root 7770 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:7001 [cluster]
root 7775 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:7002 [cluster]
root 7780 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:7003 [cluster]
root 7785 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:8001 [cluster]
root 7790 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:8002 [cluster]
root 7795 1 0 11:29 ? 00:00:00 /app/redis-6.2.6/src/redis-server 0.0.0.0:8003 [cluster]
root 7800 7404 0 11:29 pts/1 00:00:00 grep --color=auto redis
[root@localhost ~]#
四、 节点握手
上面仅仅是搭建了集群,6个节点之间还没有关系,下面通过 节点握手让集群各节点之间,发生关系。
4.1. 节点握手
7001节点,依次执行(一条一条复制粘贴)以下命令:
/app/redis-6.2.6/src/redis-cli -h localhost -p 7001
cluster meet 192.168.0.114 7002
cluster meet 192.168.0.114 7003
cluster meet 192.168.0.114 8001
cluster meet 192.168.0.114 8002
cluster meet 192.168.0.114 8003
4.2. 操作日志如下:
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7001
localhost:7001> cluster meet 192.168.0.114 7002
OK
localhost:7001> cluster meet 192.168.0.114 7003
OK
localhost:7001> cluster meet 192.168.0.114 8001
OK
localhost:7001> cluster meet 192.168.0.114 8002
OK
localhost:7001> cluster meet 192.168.0.114 8003
OK
localhost:7001>
五、槽位分配和配置主从
5.1. 槽位分配
/app/redis-6.2.6/src/redis-cli -h localhost -p 7001 cluster addslots {0..5461}
/app/redis-6.2.6/src/redis-cli -h localhost -p 7002 cluster addslots {5462..10922}
/app/redis-6.2.6/src/redis-cli -h localhost -p 7003 cluster addslots {10923..16383}
操作日志:
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7001 cluster addslots {0..5461}
OK
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7002 cluster addslots {5462..10922}
OK
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7003 cluster addslots {10923..16383}
OK
[root@localhost ~]#
登录7001节点
/app/redis-6.2.6/src/redis-cli -h localhost -p 7001
查看集群信息:cluster info
查看节点信息:cluster nodes
操作日志:
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7001
localhost:7001> 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:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:278
cluster_stats_messages_pong_sent:298
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:581
cluster_stats_messages_ping_received:298
cluster_stats_messages_pong_received:283
cluster_stats_messages_received:581
localhost:7001> cluster nodes
c1526c666b10d8e21858121be70ad74e391c9b7c 192.168.0.114:8002@18002 master - 0 1587267512220 4 connected
f990f845ed898dea7b7f8f39befd4f0dc247988a 192.168.0.114:8003@18003 master - 0 1587267510000 5 connected
fd35850bf465cb53b0ffc8f843078c171d14583d 192.168.0.114:7001@17001 myself,master - 0 1587267510000 0 connected 0-5461
3523d3510e7a551929729798f5ea0e9b65d85eea 192.168.0.114:7003@17003 master - 0 1587267511212 2 connected 10923-16383
faaa97d6fc1f163e9eb2549c77b4f6f3c68bc025 192.168.0.114:8001@18001 master - 0 1587267511000 3 connected
72e1d44ffa3b20f17ba6abb3aa8df3a9752e00cc 192.168.0.114:7002@17002 master - 0 1587267512000 1 connected 5462-10922
localhost:7001>
注:目前都是master,节点信息等会要用,这个窗口先不要关闭,
在新开一个窗口,进行下一步操作
5.2. 主从复制分配
复制原则:8001复制7001、8001复制7001、8003复制7003
/app/redis-6.2.6/src/redis-cli -h localhost -p 8001 cluster replicate fd35850bf465cb53b0ffc8f843078c171d14583d
/app/redis-6.2.6/src/redis-cli -h localhost -p 8002 cluster replicate 72e1d44ffa3b20f17ba6abb3aa8df3a9752e00cc
/app/redis-6.2.6/src/redis-cli -h localhost -p 8003 cluster replicate 3523d3510e7a551929729798f5ea0e9b65d85eea
操作日志如下:
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 8001 cluster replicate fd35850bf465cb53b0ffc8f843078c171d14583d
localhost -p 8003 cluster replicate 3523d3510e7a551929729798f5ea0e9b65d85eeaOK
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 8002 cluster replicate 72e1d44ffa3b20f17ba6abb3aa8df3a9752e00cc
OK
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 8003 cluster replicate 3523d3510e7a551929729798f5ea0e9b65d85eea
OK
[root@localhost ~]#
5.3. 集群验证
登录7001节点
/app/redis-6.2.6/src/redis-cli -h localhost -p 7001
查看集群信息:cluster info
查看节点信息:cluster nodes
注:目前3主3从 当主节点挂掉之后,从节点自动上位
操作日志如下:
[root@localhost ~]# /app/redis-6.2.6/src/redis-cli -h localhost -p 7001
localhost:7001> 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:5
cluster_my_epoch:0
cluster_stats_messages_ping_sent:692
cluster_stats_messages_pong_sent:734
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:1431
cluster_stats_messages_ping_received:734
cluster_stats_messages_pong_received:697
cluster_stats_messages_received:1431
localhost:7001> cluster nodes
c1526c666b10d8e21858121be70ad74e391c9b7c 192.168.0.114:8002@18002 slave 72e1d44ffa3b20f17ba6abb3aa8df3a9752e00cc 0 1587267912538 4 connected
f990f845ed898dea7b7f8f39befd4f0dc247988a 192.168.0.114:8003@18003 slave 3523d3510e7a551929729798f5ea0e9b65d85eea 0 1587267911000 5 connected
fd35850bf465cb53b0ffc8f843078c171d14583d 192.168.0.114:7001@17001 myself,master - 0 1587267909000 0 connected 0-5461
3523d3510e7a551929729798f5ea0e9b65d85eea 192.168.0.114:7003@17003 master - 0 1587267913546 2 connected 10923-16383
faaa97d6fc1f163e9eb2549c77b4f6f3c68bc025 192.168.0.114:8001@18001 slave fd35850bf465cb53b0ffc8f843078c171d14583d 0 1587267910000 3 connected
72e1d44ffa3b20f17ba6abb3aa8df3a9752e00cc 192.168.0.114:7002@17002 master - 0 1587267912000 1 connected 5462-10922
localhost:7001>
最后
以上就是冷傲小松鼠为你收集整理的SpringBoot2.x整合Redis 分布式集群_01的全部内容,希望文章能够帮你解决SpringBoot2.x整合Redis 分布式集群_01所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复