redis哨兵模式搭建
Redis哨兵模式搭建
master: 192.168.1.1
slave1: 192.168.1.2
slave2: 192.168.1.3
Redis服务安装
这里Redis版本为5.0.3。
Redis的安装步骤为:解压、编译、配置、启动(以manaster举例安装)
创建目录
mkdir/app/redis_6379/{dump,log,conf,run}
解压安装包
tar -zxvf redis-5.0.3.tar.gz -C /app
cd /app/redis-5.0.3
#make MALLOC=libc
# make install
执行下行命令验证服务启动,ok后ctrl+c关闭
# ./redis-server
编辑配置文件(从节点依次配置)
vi redis.conf
port 6379
tcp-backlog 511
timeout 0
protected-mode no
tcp-keepalive 300
daemonize yes
supervised no
dir "/app/redis_6379/dump"
pidfile "/app/redis_6379/run/redis_6379.pid"
logfile "/app/redis_6379/log/redis.log"
loglevel notice
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
masterauth 123456
requirepass 123456
从节点配置(主节点不用配置)
replicaof 192.168.1.1 6379
启动服务
/app/redis-5.0.3/src/redis-serice /app/redi_6379/conf/redis.conf &
查看是否都启动成功,IP和端口号都正确
ps -ef|grep redis
使用info Replication查看主从复制状态
/app/redis-5.0.3/src/redis-cli -h 192.168.1.1 -p 6379 -a123456
> info Replication
哨兵配置
哨兵模式采用一主二从三哨兵模式。
mkdir /app/sentinel_26379/{log,run,conf,dump} && cd /app/sentinel_26379/conf && touch sentinel_26379.conf
vi sentinel_6379.conf
# 保护模式默认关闭
protected-mode no
# 哨兵端口
port 26379
# 后台运行
daemonize yes
# pid
pidfile " /app/sentinel_26379/run/sentinel_26379.pid"
# log
logfile "/app/sentinel_26379/log/redis-sentinel.log"
# 工作目录
dir "/app/sentine/dump"
# 设置初始master以及法定认为下线人数:
sentinel monitor redis-master 192.168.1.1 6379 2
# 配置认证密码
sentinel auth-pass redis-master 123456
# 设置初始master以及法定认为下线人数:
sentinel down-after-milliseconds redis-master 30000
# 指定在故障转移期间,多少个slave向新的master同步得数量,如果slave是提供查询服务,则应该设置小一点更好
sentinel parallel-syncs redis-master 1
# 指定故障转移超时时间,默认为3分钟
sentinel failover-timeout redis-master 180000
# 禁止修改脚本,避免脚本重置
sentinel deny-scripts-reconfig yes
启动sentinel
/app/redis_5.0.3/src/redis-sentinel /app/sentinel_26379/conf/sentinel_26379.conf &
验证
查看哨兵信息
随意登录一台主机执行 info Sentinel命令,下面以连接192.168.1.1机器为例
/app/redis_5.0.3/src/redis-cli -p 26379 -h 192.168.1.1
> info Sentinel
通过以下命令可以看到主节点是192.168.1.1,从节点是192.168.1.1,192.168.1.2
> sentinel masters
> sentinel slaves redis-master
SpringBoot配置Redis哨兵模式
application.yml 添加Redis配置
spring:
redis:
database: 1
password: 123456
# 哨兵模式
sentinel:
master: redis-master
nodes: 192.168.1.1:26379,192.168.1.2:26379,192.168.1.3:26379