仅供参考,有什么问题可以评论区留言
参考连接:
https://blog.csdn.net/suodod/article/details/117991713
主的配置文件:
注意:我这里把原有的注释、空格都去掉了,给redis配置文件瘦身了
bind 0.0.0.0 #允许所有ip访问redis
protected-mode yes
port 6379 #端口
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes #后台运行redis
pidfile "/var/run/redis_6379.pid"
loglevel notice
logfile "/usr/local/redis/redis.log" #日志输出路径
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/usr/local/redis-6.2.4/src"
masterauth "redis123" #如果设置了redis密码。这个必须加,作用是加入到主节点的密码,不管是主还是从都要加
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "redis123" #访问redis密码
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
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
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
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 4kb
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
jemalloc-bg-thread yes
#下面这些是主从redis建立后自动生成的(不用管):
#save 3600 1
#save 300 100
#save 60 10000
#user default on sanitize-payload
从节点redis配置文件:
#其他都和主节点一样
#save 3600 1
#save 300 100
#save 60 10000
#user default on sanitize-payload #cb8001ad8314aa0abd372e48210e44b1029975a84dbef6e64b5de1f5054e3f4b ~* &* +@all
#从节点只要加这个,关联主redis
replicaof 192.168.6.141 6379
sentinel配置文件:
注意:所有哨兵配置文件都一样
# Example sentinel.conf
port 26379 #sentinel端口
daemonize yes
pidfile "/var/run/redis-sentinel.pid"
logfile "/usr/local/redis/sentinel.log" #日志输出路径
dir "/tmp"
#配置哨兵需要监控的主节点ip和端口,最后的2代表,如果有2个哨兵主观认为主节点down了,那么就客观认为主节点down掉了,开始发起投票选举新主节点的操作。多个主节点配置多个
sentinel monitor mymaster 192.168.6.141 6379 2 #这个会随着主节点的切换,ip会自动重写
#在Redis Sentinel中,sentinel auth-pass参数用于指定Redis服务器(主服务器和副本)的密码,如果它们配置了requirepass。**这个参数不是用来连接其他Sentinel的**,因为Sentinel本身没有认证功能。
sentinel auth-pass mymaster redis123
#配置多少毫秒后没收到主节点的反馈,则主观认为主节点down了
sentinel down-after-milliseconds mymaster 5000
#failover过期时间。当failover开始后,在此时间内仍然没有触发任何failover操作,当前sentinel将会认为此次failoer失败。
sentinel failover-timeout mymaster 30000
acllog-max-len 128
sentinel deny-scripts-reconfig yes
sentinel resolve-hostnames no
sentinel announce-hostnames no
protected-mode no
#下面这些,集群后自动生成的,不用在意
#user default on nopass sanitize-payload ~* &* +@all
#sentinel myid dba4e9423e3edfb3d6c8978eefbddc0ded5c5837
#sentinel config-epoch mymaster 19
#sentinel leader-epoch mymaster 19
#sentinel current-epoch 19
#sentinel known-sentinel mymaster 192.168.6.141 26379 e717d5d9951aea1a5fd34ce6ac2d1239a39ee8c8
#sentinel known-sentinel mymaster 192.168.6.142 26379 d97073b426ef08a92d4fd049240c8c5d4680f0ee
#sentinel known-replica mymaster 192.168.6.140 6379
#sentinel known-replica mymaster 192.168.6.142 6379
启动顺序说一下:
刚开始配置的时候,把认定主的redis先启动!再启动其他的redis服务,然后再启动哨兵sentinel服务
测验一下:
[root@node141 src]# ./redis-cli
127.0.0.1:6379> auth redis123
OK
127.0.0.1:6379> info
springboot中调用redis的相关配置
redis:
sentinel:
master: mymaster #主节点名称,sentinel中有配置
nodes: 192.168.6.140:26379, 192.168.6.141:26379, 192.168.6.142:26379 #填写所有主备的节点ip和sentinel端口
# host: localhost
# port: 6379
database: 2 #用到redis的哪个库
password: redis123 #填写redis密码