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

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

Redis  /  管理员 发布于 5年前   594

在第二篇中使用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数据类型
  • 昵称:

    邮箱:

    1条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Redis中能实现的功能、常见应用介绍(0个评论)
    • 2024年Redis面试题之一(0个评论)
    • 在redis缓存常见出错及解决方案(0个评论)
    • 在redis中三种特殊数据类型:地理位置、基数(cardinality)估计、位图(Bitmap)使用场景介绍浅析(2个评论)
    • Redis 删除 key用 del 和 unlink 有啥区别?(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    • 2023-04
    • 2023-07
    • 2024-01
    • 2024-02
    Top

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

    侯体宗的博客