准备服务器3台
192.168.134.71
192.168.134.72
192.168.134.73
创建文件夹,编辑redis.conf文件
cd /usr/local/redis
mkdir log sentinel
vi redis.conf
mkdir masterserver #192.168.134.71创建
mkdir slaveserver #192.168.134.71和192.168.134.72创建
redis.conf文件
##192.168.134.71的redis.conf配置文件
bind 192.168.134.71
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_masterserver.pid"
loglevel notice
logfile "/usr/local/redis/log/masterserver.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/redis/masterserver"
masterauth "oumiga" #master和slave连接的密码
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "oumiga" #登录密码
appendonly no
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events "xE"
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
##192.168.134.72和192.168.134.73的redis.conf配置文件
bind 192.168.134.72 #绑定的ip地址
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile "/var/run/redis_slaveserver.pid"
loglevel notice
logfile "/usr/local/redis/log/slaveserver.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/redis/slaveserver"
masterauth "oumiga" #master和slave连接的密码
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "oumiga" #登录密码
appendonly no
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
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events "xE"
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxclients 4064
slaveof 192.168.134.71 6379 #master节点ip
***注意:redis配置文件的bind属性和slaveof 属性要修改成对应的IP地址
编辑sentinel.conf(哨兵配置文件)
cd /usr/local/redis/sentinel
vi sentinel.conf
sentinel.conf
##192.168.134.71的sentinel.conf文件
bind 192.168.134.71 #绑定的ip(本机ip)
protected-mode no
daemonize yes
port 26379
dir "/usr/local/redis/sentinel"
logfile "/usr/local/redis/log/sentinel.log"
sentinel myid d955df80819e80927ff06ec62f13d1c3764af43f #哨兵id要保持唯一(这里我只写了一个)
sentinel monitor mymaster 192.168.134.71 6379 2
sentinel auth-pass mymaster oumiga #master和slave之间的连接密码,要保持一致
sentinel down-after-milliseconds mymaster 5000 #master失效时间
sentinel config-epoch mymaster 4
sentinel leader-epoch mymaster 4
sentinel current-epoch 4
##192.168.134.72和192.168.134.73的sentinel.conf文件
bind 192.168.134.72 #绑定的ip(本机ip)
protected-mode no
daemonize yes
port 26379
dir "/usr/local/redis/sentinel"
logfile "/usr/local/redis/log/sentinel.log"
sentinel myid 8a85c4b15d694234156739b4f6ea2259afdf46f4 #哨兵id要保持唯一(这里我只写了一个)
sentinel monitor mymaster 192.168.134.71 6379 2
sentinel auth-pass mymaster oumima #master和slave之间的连接密码,要保持一致
sentinel down-after-milliseconds mymaster 5000 #master失效时间
sentinel config-epoch mymaster 4
sentinel leader-epoch mymaster 4
sentinel current-epoch 4
启动所有的redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
启动所有的sentinel(哨兵)服务
/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel/sentinel.conf
登录redis客户端
/usr/local/redis/bin/redis-server -a oumiga -p 192.168.134.71 -p 6379
查询集群信息
##登录成功后
info replication
***注意:请注意配置文件里面带注释的属性
springboot集成redis哨兵采坑记录
如果redis因为服务器的cpu过高、内存过高、硬盘存储空间不足而导致的redis服务读取异常,redis哨兵已经切换redis服务,java客户端还是有一直报读取错误,无法切换到其它服务节点,请尝试将springboot版本升级到2.3.0,或者将redis的start jar包升级到更高版本