Windows下搭建redis 哨兵环境

https://github.com/tporadowski/redis/releases 下载windows版的redis,自行下载解压。

关于哨兵模式的讲解,强烈推荐 【深入学习redis(4):哨兵】

一主两从

  1. 复制三份 redis.windows.conf,分别如下配置

    redis-6379.windows.conf

    bind 127.0.0.1
    port 6379
    

    redis-6380.windows.conf

    bind 127.0.1
    port 6380
    slaveof 127.0.0.1 6379
    

    redis-6381.windows.conf

    bind 127.0.1
    port 6381
    slaveof 127.0.0.1 6379
    
  2. 启动

    redis-server.exe redis-6379.windows.conf
    redis-server.exe redis-6380.windows.conf
    redis-server.exe redis-6381.windows.conf
    
  3. 验证是否成功,看到从节点状态 online 说明主从环境搭建成功了

    redis-cli.exe -h 127.0.0.1 -p 6379
    
    127.0.0.1:6379> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=6380,state=online,offset=98,lag=0
    slave1:ip=127.0.0.1,port=6381,state=online,offset=98,lag=0
    master_replid:677245c1292f2244597f22a12c85730f236fa707
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:98
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:98
    

三个哨兵

  1. 创建三个配置文件

    sentinel-26379.windows.conf

    bind 127.0.0.1
    port 26379
    sentinel monitor mymaster 127.0.0.1 6379 2
    

    sentinel-26380.windows.conf

    bind 127.0.0.1
    port 26380
    sentinel monitor mymaster 127.0.0.1 6379 2
    

    sentinel-26381.windows.conf

    bind 127.0.0.1
    port 26381
    sentinel monitor mymaster 127.0.0.1 6379 2
    
  2. 启动哨兵

    redis-server.exe sentinel-26379.windows.conf --sentinel
    redis-server sentinel-26380.windows.conf --sentinel
    redis-server sentinel-26381.windows.conf --sentinel
    
  3. 验证是否成功,看到最后 status=ok 说明成功了

    127.0.0.1:26379> info sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
    
  4. 演示故障自动切换

    关掉主节点 6379,等一会,就会发现哨兵切换了主节点,重新启动 6379 节点,它就变成了从节点了

    redis-cli.exe -h 127.0.0.1 -p 6379
    
    127.0.0.1:6379> info replication
    # Replication
    role:slave
    master_host:127.0.0.1
    master_port:6380
    master_link_status:up
    master_last_io_seconds_ago:1
    master_sync_in_progress:0
    slave_repl_offset:56440
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:8f282577c2e4ddeb9794f88757e5dad7870e5e6d
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:56440
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:55487
    repl_backlog_histlen:954
    

遇到的问题

代码客户端连接redis,报错 All sentinels down, cannot determine where is mymaster master is running... 或者 Could not get a resource from the pool

参考这篇博客:https://www.jianshu.com/p/098494958892

待明天验证,先睡觉了

原文地址:https://www.cnblogs.com/haicheng92/p/15754642.html