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/{log,run,conf} && cd /app/sentinel/conf && touch sentinel_6379.conf
vi sentinel_6379.conf
# 保護模式預設關閉
protected-mode no
# 哨兵埠
port 26379
# 後臺執行
daemonize yes
# pid
pidfile " /app/sentinel/run/sentinel.pid"
# log
logfile "/app/sentinel/log/redis-sentine.log"
# 工作目錄
dir "/app/sentine/"
# 設定初始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/conf/sentinel_6379.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