Redis--主从复制

  redis的复制功能是支持多个数据库之间的数据同步。一类是主数据库(master)一类是从数据库(slave),主数据库可以进行读写操作,当发生写操作的时候自动将数据同步到从数据库,而从数据库一般是只读的,并接收主数据库同步过来的数据,一个主数据库可以有多个从数据库,而一个从数据库只能有一个主数据库

  • 通过redis的复制功能可以很好的实现数据库的读写分离,提高服务器的负载能力主数据库主要进行写操作,而从数据库负责读操作

redis主从复制

  • 主从复制过程:

主从复制过程

配置redis主从

  • 修改配置文件redis.conf,只修改从数据库

    1
    2
    slaveof ip port
    slaveof no one 可以把从数据库转为主数据库
  • 示例代码:配置主从

  • 节点分配
  1. 192.168.33.88 端口6379为主节点
  2. 192.168.33.89 为从节点
    在从节点配置
  • 192.168.33.88

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ###############启动后info查看#################

    127.0.0.1:6379> info
    //......
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=192.168.33.89,port=6379,state=online,offset=7979,lag=1
    master_repl_offset:7979
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:2
    repl_backlog_histlen:7978
    //......
  • 192.168.33.89

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ###############启动后info查看##################
    //.......
    # Replication
    role:slave
    master_host:192.168.33.88
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:8
    master_sync_in_progress:0
    slave_repl_offset:7965
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    //......
  • 注意:
    如果你使用主从复制,那么要确保你的master激活了持久化或者确保它不会在当掉后自动重启
    slave是master的完整备份,因此如果master通过一个空数据集重启,slave也会被清掉。

redis的Sentinel

redis的sentinel系统用于管理多个redis服务器,该系统主要执行三个任务:

  1. 监控(Monitoring): Redis Sentinel实时监控主服务器和从服务器运行状态。
  2. 提醒(Notification):当被监控的某个 Redis 服务器出现问题时, Redis Sentinel 可以向系统管理员发送通知, 也可以通过 API 向其他程序发送通知。
  3. 自动故障转移(Automatic failover):当一个主服务器不能正常工作时,Redis Sentinel 可以将一个从服务器升级为主服务器, 并对其他从服务器进行配置,让它们使用新的主服务器。当应用程序连接Redis 服务器时, Redis Sentinel会告之新的主服务器地址和端口。

  sentinel

  • 配置sentinel
  1. 修改sentinel.conf文件
    sentinel monitor mymaster 192.168.1.170 6379 2 ##指定到主节点ip和端口

  2. 启动sentinel
    redis-sentinel sentinel.conf

通过订阅指定的频道信息,当服务器出现故障得时候通知管理员
客户端可以将 Sentinel 看作是一个只提供了订阅功能的 Redis 服务器,你不可以使用 PUBLISH 命令向这个服务器发送信息,但你可以用 SUBSCRIBE 命令或者 PSUBSCRIBE 命令, 通过订阅给定的频道来获取相应的事件提醒。
一个频道能够接收和这个频道的名字相同的事件。 比如说, 名为 +sdown 的频道就可以接收所有实例进入主观下线(SDOWN)状态的事件。

  • 示例代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    ############这里配置两个sentinel,同时修改配置###########
    1. 在安装目录下复制文件到/etc/ 即# cp sentinel.conf /etc/
    2. vim 修改 sentinel monitor mymaster 192.168.33.88 6379 2
    3. 重启配置#redis-sentinel /etc/sentinel.conf

    第一步:启动momo1==》192.168.33.88
    [root@momo1 redis]# redis-sentinel /etc/sentinel.conf
    6971:X 29 Apr 05:26:07.499 * Increased maximum number of open files to 10032 (it was originally set to 1024).
    _._
    _.-``__ ''-._
    _.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
    .-`` .-```. ```\/ _.,_ ''-._
    ( ' , .-` | `, ) Running in sentinel mode
    |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
    | `-._ `._ / _.-' | PID: 6971

    `-._ `-._ `-./ _.-' _.-'
    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' | http://redis.io
    `-._ `-._`-.__.-'_.-' _.-'

    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' |
    `-._ `-._`-.__.-'_.-' _.-'

    `-._ `-.__.-' _.-'
    `-._ _.-'
    `-.__.-'


    6971:X 29 Apr 05:26:07.881 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    6971:X 29 Apr 05:26:07.881 # Sentinel runid is c24c259a092430387b2a8f2ad03b1b0bd4e0abd2
    6971:X 29 Apr 05:26:07.881 # +monitor master mymaster 192.168.33.88 6379 quorum 2

    第二步:启动momo2==》192.168.33.89
    [root@momo2 redis-3.0.6]# redis-sentinel /etc/sentinel.conf
    9694:X 29 Apr 05:27:07.226 * Increased maximum number of open files to 10032 (it was originally set to 1024).
    _._
    _.-``__ ''-._
    _.-`` `. `_. ''-._ Redis 3.0.6 (00000000/0) 64 bit
    .-`` .-```. ```\/ _.,_ ''-._
    ( ' , .-` | `, ) Running in sentinel mode
    |`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
    | `-._ `._ / _.-' | PID: 9694

    `-._ `-._ `-./ _.-' _.-'
    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' | http://redis.io
    `-._ `-._`-.__.-'_.-' _.-'

    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' |
    `-._ `-._`-.__.-'_.-' _.-'

    `-._ `-.__.-' _.-'
    `-._ _.-'
    `-.__.-'


    9694:X 29 Apr 05:27:07.252 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    9694:X 29 Apr 05:27:07.252 # Sentinel runid is 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1
    9694:X 29 Apr 05:27:07.252 # +monitor master mymaster 192.168.33.88 6379 quorum 2
    9694:X 29 Apr 05:27:08.847 * -dup-sentinel master mymaster 192.168.33.88 6379 #duplicate of 192.168.33.88:26379 or c24c259a092430387b2a8f2ad03b1b0bd4e0abd2
    9694:X 29 Apr 05:27:08.847 * +sentinel sentinel 192.168.33.88:26379 192.168.33.88 26379 @ mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:27:09.304 * -dup-sentinel master mymaster 192.168.33.88 6379 #duplicate of 127.0.0.1:26379 or 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1
    9694:X 29 Apr 05:27:09.304 * +sentinel sentinel 127.0.0.1:26379 127.0.0.1 26379 @ mymaster 192.168.33.88 6379

    第三步:如果在主节点192.168.33.88(momo1)设置值在89(momo2)中获取
    192.168.33.88:6379> set momo1 88
    OK
    192.168.33.89:6379> get momo1
    "88"

    第四步:如果我们kill掉主节点
    默认30s后

    ##momo1
    6971:X 29 Apr 05:35:36.799 # +sdown master mymaster 192.168.33.88 6379
    6971:X 29 Apr 05:35:36.930 # +new-epoch 1
    6971:X 29 Apr 05:35:36.933 # +vote-for-leader 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1 1
    6971:X 29 Apr 05:35:38.045 # +config-update-from sentinel 192.168.33.89:26379 192.168.33.89 26379 @ mymaster 192.168.33.88 6379
    6971:X 29 Apr 05:35:38.045 # +switch-master mymaster 192.168.33.88 6379 192.168.33.89 6379
    6971:X 29 Apr 05:35:38.046 * +slave slave 192.168.33.88:6379 192.168.33.88 6379 @ mymaster 192.168.33.89 6379

    ##momo2
    9694:X 29 Apr 05:35:36.868 # +sdown master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:36.926 # +odown master mymaster 192.168.33.88 6379 #quorum 3/2
    9694:X 29 Apr 05:35:36.926 # +new-epoch 1
    9694:X 29 Apr 05:35:36.926 # +try-failover master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:36.952 # +vote-for-leader 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1 1
    9694:X 29 Apr 05:35:36.952 # 127.0.0.1:26379 voted for 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1 1
    9694:X 29 Apr 05:35:36.969 # 192.168.33.88:26379 voted for 271dc5b6ca168ffcbdaafc95aa298bffb9ddbfd1 1
    9694:X 29 Apr 05:35:37.019 # +elected-leader master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.019 # +failover-state-select-slave master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.079 # +selected-slave slave 192.168.33.89:6379 192.168.33.89 6379 @ mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.079 * +failover-state-send-slaveof-noone slave 192.168.33.89:6379 192.168.33.89 6379 @ mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.156 * +failover-state-wait-promotion slave 192.168.33.89:6379 192.168.33.89 6379 @ mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.990 # +promoted-slave slave 192.168.33.89:6379 192.168.33.89 6379 @ mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:37.991 # +failover-state-reconf-slaves master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:38.075 # +failover-end master mymaster 192.168.33.88 6379
    9694:X 29 Apr 05:35:38.075 # +switch-master mymaster 192.168.33.88 6379 192.168.33.89 6379
    9694:X 29 Apr 05:35:38.076 * +slave slave 192.168.33.88:6379 192.168.33.88 6379 @ mymaster 192.168.33.89 6379

    ###这一句话就表示了选择了新的主节点192.168.33.89 6379
    +switch-master mymaster 192.168.33.88 6379 192.168.33.89 6379

    第五步:如果我们再次启动88这台服务器,他将不再是master而是slave
    9694:X 29 Apr 05:36:08.160 # +sdown slave 192.168.33.88:6379 192.168.33.88 6379 @ mymaster 192.168.33.89 6379
    9694:X 29 Apr 05:38:54.076 # -sdown slave 192.168.33.88:6379 192.168.33.88 6379 @ mymaster 192.168.33.89 6379
    9694:X 29 Apr 05:39:04.021 * +convert-to-slave slave 192.168.33.88:6379 192.168.33.88 6379 @ mymaster 192.168.33.89 6379

java操作sentinel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

/**
* 使用sentinel操作主从架构中的数据
* @throws Exception
*/

@Test
public void TestSentinel() throws Exception {
JedisPoolConfig poolConfig = new JedisPoolConfig();
//TODO--
String masterName = "mymaster"; //名称

Set<String> sentinels = new HashSet<String>();;
sentinels.add("192.168.33.88:26379");
sentinels.add("192.168.33.89:26379");

//SentinelPool
JedisSentinelPool jsp = new JedisSentinelPool(masterName , sentinels, poolConfig );

//可以获取主节点信息
HostAndPort currentHostMaster = jsp.getCurrentHostMaster();
System.out.println(currentHostMaster.getHost()+"--"+currentHostMaster.getPort());

Jedis jedis = jsp.getResource();//获取Jedis
String value = jedis.set("xiaoxiao","momo");
System.out.println(value);
jedis.close();
}

//返回结果
192.168.33.89--6379
OK

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器