Redis 主从复制 和 Redis 哨兵机制

Redis 主从复制

主从复制架构仅仅用来解决数据冗余备份,从节点仅仅用来同步数据
--无法解决:1.master节点出现自动故障转移

[root@localhost ~]# mkdir master slave1 slave2
[root@localhost ~]# cp redis-6.0.9/redis.conf master/
[root@localhost ~]# cp redis-6.0.9/redis.conf slave1/
[root@localhost ~]# cp redis-6.0.9/redis.conf slave2/

小插曲:改名 [root@localhost ~]# mv salve1/ slave1/

编写配置文件 [root@localhost ~]# vim master/redis.conf

1.准备3台机器并修改配置 (准备多个redis节点,做相应的配置)

- master
port 6379
bind 0.0.0.0

- slave1
port 6380
bind 0.0.0.0
salveof masterip masterport

- slave2
port 6381
bind 0.0.0.0
salveof masterip masterport


2.启动3台机器进行测试

- cd /usr/local/redis/bin
- ./redis-server /root/master/redis.conf
- ./redis-server /root/slave1/redis.conf
- ./redis-server /root/slave2/redis.conf

 

Redis哨兵机制
sentinel(哨兵)是redis的高可用性解决方案:由一个或多个sentinel实例 组成的Sentinel系统可以监视任意多个主服务器,
以及这些主服务器属下的所有从服务器,并在被监控的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级
为新的主服务器。简单的说哨兵就是带有自动故障转移功能的主从架构。

--无法解决:1.单节点并发压力问题 2.单节点内存和磁盘的物理上限问题


搭建哨兵架构

[root@localhost ~]# mkdir sentinel
[root@localhost ~]# cd sentinel/
[root@localhost ~]# touch sentinel.conf
[root@localhost ~]# vim sentinel.conf


1.在主节点上创建哨兵机制
-在master对应redis.conf同目录下新建sentinel.conf文件,名字绝对不能错

2.配置哨兵,在sentinel.conf文件填入内容
-sentinel monitor 被监控数据库名字(自己起名字) ip port 1 (1代表一个哨兵)

3.启动哨兵模式进行测试
[root@localhost src]# cp redis-sentinel /usr/redis/bin

- redis-sentinel /root/sentinel/sentinel.conf
说明:这个后面的数字 2.是指当有两个及以上的sentinel服务检测到master宕机,才会去执行主从切换的功能。

启动哨兵在redis源码的src下边有一个redis-sentinel脚本

哨兵的服务端口:26379


通过springboot操作哨兵


#redis sentinel 配置
#master书写是使用哨兵监听的那个名称
spring.redis.sentinel.master=mymaster
#连接的不再是一个具体redis主机,书写的是多个哨兵节点
spring.redis.sentinel.nodes=192.168.139.129:26379

# 注意:bind 0.0.0.0 通过程序连接哨兵必须开启远程连接权限

posted @ 2020-12-07 17:38  缥缈咘咘  阅读(69)  评论(0编辑  收藏  举报