《redis集群sentinel主从复制》笔记

时间:2021-07-09 04:34:27

首先了解一下什么Sentinel:http://redisdoc.com/topic/sentinel.html#id2 ,耐着心看一遍,很受用。我也是看了好几遍,然后再结合实例操作验证,回头再看就觉得是那么回事了。


我的Demo案例下载地址:http://download.csdn.net/detail/caiwenfeng_for_23/8696295


  下面我简单说一下配置,放在conf目录下:


《redis集群sentinel主从复制》笔记


从上图中可以看出,我配置了4个Sentinel、3个Master、7个Slave,他们之间的对应关系,我想你通过名字也可以猜到了吧。


关系如下:

1个Master分配1个slave,1个Master分配了2个Slave,1个Master分配了4个Slave;4个Sentinel监视3个Master

Master Slave
6379 7379
6380 7380、7381
6381 7382、7383、7384、7385




Sentinel分配的端口为:26379、26380、26381、26382
Master配置:
daemonize yes
pidfile /home/mayi/test/redis/data/redis-master1.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile ""
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 ./
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
maxmemory 50mb
maxmemory-policy allkeys-lru
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 ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
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


Slave配置文件:
#是否后台运行
daemonize yes
pidfile /home/mayi/test/redis/data/redis-slave1_1.pid
port 7379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile ""
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 ./
slaveof  127.0.0.1 6379
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
 maxclients  10000
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 ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
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

Sentinel配置文件:
#学习网址:http://redisdoc.com/topic/sentinel.html#id2
port 26379
daemonize yes
logfile "/home/mayi/test/redis/logs/sentinel.log"

#6379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000
sentinel parallel-syncs mymaster 1

#6380
sentinel monitor master_6380 127.0.0.1 6380 4
sentinel down-after-milliseconds master_6380 10000
sentinel failover-timeout master_6380 180000
sentinel parallel-syncs master_6380 5

#6381
sentinel monitor master_6381 127.0.0.1 6381 4
sentinel down-after-milliseconds master_6381 10000
sentinel failover-timeout master_6381 180000
sentinel parallel-syncs master_6381 5


PS:具体可以下载demo案例查看conf目录下的配置文件
然后分别启动Master、Slave、Sentinel 即可。  

启动后,可以用redis-cli 连接,输入info命令查看相应信息等。其他操作可以查看http://redisdoc.com/topic/sentinel.html#id2 

下面是Jedis操作Sentinel,代码如下:
package com.sondon.mayi.redis.test;

/**
* @Project : JRedisHello
* @Package : com.sondon.mayi.redis
* @Class : RedisClusterTest.java
* @Company 广州讯动网络科技有限公司
* @Author : 蔡文锋
* @DateTime:2015年5月6日 下午2:33:54
* @Blog:http://blog.csdn.net/caiwenfeng_for_23
* @Description : {}
*/

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

/**
*
* @Project : JRedisHello
* @Package : com.sondon.mayi.redis.test
* @Class : JedisSentinelPoolTest.java
* @Company 广州讯动网络科技有限公司
* @Author : 蔡文锋
* @DateTime:2015年5月13日 下午5:25:18
* @Blog:http://blog.csdn.net/caiwenfeng_for_23
* @Description : {}
*/
public class JedisSentinelPoolTest {

public static void main(String[] args) {
Set<String> sentinels = new HashSet<String>();
sentinels.add(new HostAndPort("127.0.0.1", 26379).toString());
sentinels.add(new HostAndPort("127.0.0.1", 26380).toString());
sentinels.add(new HostAndPort("127.0.0.1", 26381).toString());
sentinels.add(new HostAndPort("127.0.0.1", 26382).toString());

JedisSentinelPool master1_sentinelPool = new JedisSentinelPool("mymaster",sentinels);
JedisSentinelPool master2_sentinelPool=new JedisSentinelPool("master_6380", sentinels);
JedisSentinelPool master3_sentinelPool=new JedisSentinelPool("master_6381", sentinels);

System.out.println("Current master1: "+ master1_sentinelPool.getCurrentHostMaster().toString());
System.out.println("Current master2: "+ master2_sentinelPool.getCurrentHostMaster().toString());
System.out.println("Current master3: "+ master3_sentinelPool.getCurrentHostMaster().toString());

//System.out.println("num_active :"+master1_sentinelPool.getNumActive());
//System.out.println("num_active :"+master2_sentinelPool.getNumActive());
//System.out.println("num_active :"+master3_sentinelPool.getNumActive());
System.out.println("isClosed :"+master1_sentinelPool.isClosed());

//存
Jedis master = master1_sentinelPool.getResource();
master.set("username", "liangzhichao");
//释放master
master1_sentinelPool.returnResourceObject(master);

//取
Jedis master2 = master1_sentinelPool.getResource();
String value = master2.get("username");
System.out.println("username: " + value);
master2.close();


master1_sentinelPool.close();
master2_sentinelPool.close();
master3_sentinelPool.close();

master1_sentinelPool.destroy();
master2_sentinelPool.destroy();
master3_sentinelPool.destroy();

}
}


结果显示:
Current master1: 127.0.0.1:6379
Current master2: 127.0.0.1:6380
Current master3: 127.0.0.1:6381
isClosed :false
username: liangzhichao



PS:先把demo跑起来,再慢慢研究里面的细枝末节。


参考文章:

       Sentinel-Redis高可用方案(一):主从复制

       Sentinel-Redis高可用方案(二):主从切换