Redis 哨兵模式搭建

Redis 哨兵模式搭建

1. 配置

vim sentinel.conf
# 关闭指定 ip 访问
protected-mode no
# 守护进程运行
daemonize yes
# 配置日志文件
logfile /usr/local/redis/sentinel/sentinel.log
# 配置工作目录
dir /usr/local/redis/sentinel
# 配置监听 master ip 为127.0.0.1 端口 6379 2个哨兵发现master 挂了开始切换
sentinel monitor mymaster 127.0.0.1 6379 2 
# 配置密码
sentinel auth-pass mymaster xxxx
# 判定 master 无响应的毫秒数
sentinel down-after-milliseconds mymaster 30000
# 并行同步的数量
sentinel parallel-syncs mymaster 1
# 主备切换超时时间
sentinel failover-timeout master-node 180000
# 启动
redis-sentinel sentinel.conf
# 查看哨兵信息
root@node-master init.d]# redis-cli -p 26379
127.0.0.1:26379> sentinel master master-node
 1) "name"
 2) "master-node"
 3) "ip"
 4) "192.168.31.201"
 5) "port"
 6) "6379"
 7) "runid"
 8) "193e5beda4ae7d9fdc32f5c840093a8b4bb8dc85"
 9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "460"
19) "last-ping-reply"
20) "460"
21) "down-after-milliseconds"
22) "10000"
23) "info-refresh"
24) "867"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "1195355"
29) "config-epoch"
30) "0"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379>

2. 集成 SpringBoot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
  redis:
    sentinel:
      master: master-node # master名
      nodes: master-node:26379,node-1:26379,node-2:26379 # 哨兵 ip:port,多个用逗号分隔
    password: root
上一篇:docker 启动项目基础依赖


下一篇:01-短信验证码、动态验证码