侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

【Redis三】基于Redis sentinel的自动failover主从复制

Redis  /  管理员 发布于 2年前   276

在第二篇中使用2.8.17搭建了主从复制,但是它存在Master单点问题,为了解决这个问题,Redis从2.6开始引入sentinel,用于监控和管理Redis的主从复制环境,进行自动failover,即Master挂了后,sentinel自动从从服务器选出一个Master使主从复制集群仍然可以工作,如果Master醒来再次加入集群,只能以从服务器的形式工作。

 

什么是Sentinel

Redis Sentinel is a system designed to help managing Redis instances. It performs the following four tasks
  • Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
  • Notification. Sentinel can notify the system administrator, or another computer program, via an API, that something is wrong with one of the monitored Redis instances.
  • Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.
  • Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.

 

Redis主从复制搭建步骤

 

Redis搭建主从复制的步骤十分简单,以下在Windows上操作。

 

 

1. 解压Redis压缩包至E:\devsoftware\redis-2.8.17

2.在Redis目录中复制两份redis.windows.conf,分别命名为redis.windows.conf.slave1,redis.windows.conf.slave2

3.修改redis.conf.slave1文件,

  • port 6379 改为 port 6380
  • 打开slaveof指令,改为slaveof 127.0.0.1 6379
  • 将快照文件名改为dbfilename dump.slave1.rdb
  • 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave1.aof

4. 修改redis.conf.slave2文件

  • port 6379 改为 port 6381
  • 打开slaveof指令,改为slaveof 127.0.0.1 6379
  • 将快照文件名改为dbfilename dump.slave2.rdb
  • 如果使用AOF方式持久化,则需要将appendfilename appendonly.aof改为appendfilename appendonly.slave2.aof

5. 启动三个Redis服务器,形成一主两从的主从复制副本集

 

通过上面的启动过程,可见只需要redis-server一个启动脚本即可,它读取不同的配置文件,来完成初始化

 

 

  • Master启动时的日志输出

 

 

[21684] 25 Nov 20:54:16 * Slave ask for synchronization[21684] 25 Nov 20:54:16 * Starting BGSAVE for SYNC[21684] 25 Nov 20:54:16 * Foregroud saving started by pid 21684[21684] 25 Nov 20:54:17 * DB saved on disk[21684] 25 Nov 20:54:17 * Background saving terminated with suc[21684] 25 Nov 20:54:17 * Synchronization with slave succeeded

 

  •  Slave1启动时的日志输出
[21068] 25 Nov 20:54:15 * Server started, Redis version 2.4.5[21068] 25 Nov 20:54:15 * DB loaded from disk: 0 seconds[21068] 25 Nov 20:54:15 * The server is now ready to accept connections on port[21068] 25 Nov 20:54:16 - DB 0: 1 keys (0 volatile) in 4 slots HT.[21068] 25 Nov 20:54:16 - 0 clients connected (0 slaves), 1180024 bytes in use[21068] 25 Nov 20:54:16 * Connecting to MASTER...[21068] 25 Nov 20:54:16 * MASTER <-> SLAVE sync started[21068] 25 Nov 20:54:16 * Non blocking connect for SYNC fired the event.[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: receiving 19 bytes from master[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Loading DB in memory[21068] 25 Nov 20:54:17 * MASTER <-> SLAVE sync: Finished with success

 

Redis sentinel 搭建步骤

 

  • 创建sentinel服务器的配置文件,sentinel.conf文件,内容如下
port 26389sentinel monitor mymaster 127.0.0.1 6379 1sentinel down-after-milliseconds mymaster 10000sentinel failover-timeout mymaster 180000sentinel parallel-syncs mymaster 1
 特别注意,在2.6版本还需要额外设置一个属性sentinel can-failover mymaster yes,在2.8版本无需设置这个属性,设置了反而不能启动sentinel服务器

 1. down-after-milliseconds

     master被当前sentinel实例认定为“失效”(SDOWN)的间隔时间

 2. failover-timeout

     failover过期时间,当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel  将会认为此次failoer失败

3. parallel-syncs

    当新master产生时,同时进行slaveof到新master并进行同步复制的slave个数,也就是同时几个slave进行同步。因为在salve执行salveof与新master同步时,将会终止客户端请求,因此这个值需要权衡。此值较大,意味着“集群”终止客户端请求的时间总和和较大,此值较小,意味着“集群”在故障转移期间,多个salve向客户端提供服务时仍然使用旧数据

 

  • 启动sentinel服务器

     使用如下命令启动sentinel服务器

 

 

redis-server.exe sentinel.conf --sentinel
 

 

  • 经过数据同步后,sentinel监测到当前的系统当前有一主两从环境,使用如下命令可以观察到sentinel的状态

 

redis-cli.exe -h 127.0.0.1 -p 26389 info sentinel

 

 

 得到如下结果,结果表明,当前的master监听端口是6381

 

E:\devsoftware\redis-2.8.17>redis-cli.exe -h 127.0.0.1 -p 26389 info sentinel# Sentinelsentinel_masters:1sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0master0:name=mymaster,status=ok,address=127.0.0.1:6381,slaves=2,sentinels=1
 

 

主服务器写入从服务器读取数据测试

  •  往主服务器上写入一个数据,检查从服务器是否同步
E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6379redis 127.0.0.1:6379> set  abc 1OKredis 127.0.0.1:6379> get abc"1"redis 127.0.0.1:6379>Ctrl + C
 

 

  • 两个从服务器的读取操作:

 

 

E:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6380redis 127.0.0.1:6380> get abc"1"redis 127.0.0.1:6380> ^CE:\devsoftware\redis-2.4.5-win32-win64\64bit>redis-cli.exe -h 127.0.0.1 -p 6381redis 127.0.0.1:6381> get abc"1"redis 127.0.0.1:6381>
 

 

从服务器写入,其它服务器读取数据测试


系统提示,


  • 上一条:
    【Redis二】Redis2.8.17搭建主从复制环境
    下一条:
    【Redis四】Redis数据类型
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • redis数据同步工具redis-shake之两服务器之间数据实时同步案例(0个评论)
    • redis中INCR、SETNX、SET三种加锁方式详解(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • redis实现分页(0个评论)
    • Redis应用场景限流算法之一,计数器算法代码(0个评论)
    • 近期文章
    • 在Laravel应用程序如何减少代码重复编写(0个评论)
    • 在laravel项目中提高安全性方式推荐:CSP内容安全策略(0个评论)
    • 在go语言中从值中获取常量名称代码示例(0个评论)
    • 在go语言中如何通过名称获得结构字段和值代码示例(0个评论)
    • 在go语言中用JQuery + html2canvas实现拍摄浏览器的屏幕截图示例(0个评论)
    • 人生感悟分享:讲一个大学毕业生到社畜老狗的蜕变心路历程(0个评论)
    • laravel9框架报错Target class... does not exist解决方式(0个评论)
    • Laravel 9.48版本发布(0个评论)
    • Meta高级工程师现身说法:程序员干得越久,代码写得越少?(0个评论)
    • 本站zongscan祝大家除夕快乐,2023有奔头(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客