## redis 哨兵模式
机器1:192.168.100.221
机器2:192.168.100.222
机器3:192.168.100.223
yum -y install gcc gcc-c++ libstdc++-devel
wget https://download.redis.io/releases/redis-5.0.14.tar.gz
make MALLOC=libc
make install
mkdir -p /data/redis/cluster/
vi /data/redis/cluster/redis.conf 三个配置文件
bind 192.168.100.221
port 6379
daemonize yes
pidfile "/var/run/redis.pid"
logfile "/data/redis/cluster/redis.log"
dir "/data/redis/cluster/"
masterauth 1234
requirepass "1234"
appendonly yes
maxmemory 7gb
vi /usr/lib/systemd/system/redis.service 三个配置文件
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
PIDFile=/var/run/redis.pid
ExecStart=/usr/local/bin/redis-server /data/redis/cluster/redis.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/bin/redis-cli -h 192.168.100.221 -p 6379 shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable redis
systemctl start redis
# 哨兵配置文件
mkdir -p /data/redis/sentinel
mkdir -p /data/redis/logs/
vi /data/redis/cluster/sentinel.conf
daemonize yes
bind 192.168.100.221
port 26379
logfile "/data/redis/logs/sentinel.log"
dir "/data/redis/sentinel"
sentinel deny-scripts-reconfig yes
# master判断为失效至少需要 1 个 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)
sentinel monitor master 192.168.100.223 6379 2
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds master 5000
# 若哨兵在配置值内未能完成故障转移操作,则任务本次故障转移失败
sentinel failover-timeout master 15000
# 指定了在执行故障转移时, 最多可以有多少个slave同时对新的master进行同步, 这个数字越小, 完成故障转移所需的时间就越长
sentinel parallel-syncs master 2
# 如果redis配置了密码,那这里必须配置认证,否则不能自动切换
sentinel auth-pass master 1234
vi /usr/lib/systemd/system/sentinel.service
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/bin/redis-sentinel /data/redis/cluster/sentinel.conf --supervised systemd
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
[Install]
WantedBy=multi-user.target
systemctl enable sentinel
systemctl start sentinel
# 设置主节点为192.168.100.223
221节点: redis-cli -h 192.168.100.221 -p 6379 -a 1234 SLAVEOF 192.168.100.223 6379
222节点: redis-cli -h 192.168.100.222 -p 6379 -a 1234 SLAVEOF 192.168.100.223 6379
# 登录查看集群信息
redis-cli -h 192.168.100.223 -p 6379 -a 1234
192.168.100.223:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.100.221,port=6379,state=online,offset=16017,lag=0
slave1:ip=192.168.100.222,port=6379,state=online,offset=16017,lag=1
master_replid:858a7d5e44069a403aff3c8872bb26aa41737332
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:16017
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:16017
程序连接redis:
使用jedis来连接操作哨兵模式下的redis。连接redis不是直接连接的master的ip,而是来连接的哨兵们,把哨兵ip放在set集合中作
为参数来创建连接池,内部会获取到当前master。
// 哨兵信息,注意填写哨兵的地址
Set<String> sentinels = new HashSet<String>();
sentinels.add("192.168.100.221:26379");
sentinels.add("192.168.100.222:26380");
sentinels.add("192.168.100.223:26381");