`
greemranqq
  • 浏览: 965891 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

redis sentinel

阅读更多

一.序言

     redis 前面介绍了下master-salve ,但是其实它还无法完成故障自动切换的的效果。redis 2.8+ 已经提供了一种相对稳定的机制,防止单点:sentinel  ,地址:http://redis.io/topics/sentinel

 

 

二.基本功能点

    1.Monitor :它能监控redis 实例是否运行正常

    2.Notification : 发现监控的redis 实例错误,它能能通过API,通知另一个机器

    3.Automatic failover:如果master没按预期的运行,那么它会自动将salve提升为master,并提供连接。并且其他的salve 也会连接到新的master 上。

    4.Configuration provider :Sentinel 作为连接客户端的中心,客户端连接上sentinel 并提供一个可用的服务,如果服务挂掉,会自动转移到新的可用服务上。

 

三.sentinel 支持分布式集群

     它本身就支持集群部署,通常我们要2n+1个节点,和ZK原理类似,如果有一半节点发现redis master不可用,那么就会认为不可用,则转移节点。这样能减少误判。

     目前稳定版是2.8 和 3.0 版本是最稳定的。

 

四.简单配置

     1. 有个redis-sentinel 启动器,和 redis-server 类似的,启动方式 redis-sentinel   sentinel.conf 

         

 redis-sentinel   sentinel.conf 

 

 

     2. sentinel.conf 配置,这里我也用了3个节点配置

        

# 端口,每个sentinel 实例不同,端口不同
port 26379

# 监控的master节点信息, 2:表示需要2个监控着同意 判定失败
# 2 只是一个配置值,必须是大多数 就行了,少了 会自动更改的
sentinel monitor mymaster 127.0.0.1 6379 2
# 6秒ping 不通,认为失败
sentinel down-after-milliseconds mymaster 60000
# 故障超时时间,英语不好,就不解释了
sentinel failover-timeout mymaster 180000
# 我的理解是,故障恢复后,新的master 和 savle 之间同步线程数,
# 因为要进行新一次的同步,slave 很多,这个值越小越慢
sentinel parallel-syncs mymaster 1

# salve 转移的配置,resque 没找到说明- -
sentinel monitor resque 127.0.0.1 10001 4
sentinel down-after-milliseconds resque 10000
sentinel failover-timeout resque 180000
sentinel parallel-syncs resque 5

 

 

   3.启动redis 实例 和 sentinel 监控

   

# 路径就不贴了
redis-server  redis-6379.conf
redis-server  redis-10001.conf

redis-sentinel redis-sentinel-23769.conf
redis-sentinel redis-sentinel-23768.conf
redis-sentinel redis-sentinel-23767.conf

 

  4. 能启动就OK了

 

五.jredis 测试

  

@org.junit.Test
    public   void sentinePool(){
        // 连接监控就行了
        Set sentinels = new HashSet();
        sentinels.add(new HostAndPort("localhost", 26379).toString());
        sentinels.add(new HostAndPort("localhost", 26378).toString());
        sentinels.add(new HostAndPort("localhost", 26377).toString());
        JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", sentinels);
        Jedis jredis = sentinelPool.getResource();
        System.out.println(jredis.keys("*"));

    }

 

    测试反馈:

    1.key数据全部获取(很少数据)

    2.手动停掉master  6379 ,几秒后 连接OK,转移途中会有异常

    3.重启6379 成为了salve,数据同步OK。

    4.反复操作,能保证可用性,但是挂掉的时候有几秒连不上。

    5.同时停掉redis,再重启,恢复到重启前,MS结果不变,因为conf 有持久化的ID

 

六.其他:

     当你通过配置这个时候,sentinel.conf 的文件会改写,持久化一些ID进去,这是该改写后的文件: 

    

# 其中一个文件的
port 26377
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3

sentinel known-slave mymaster 127.0.0.1 10001
sentinel known-sentinel mymaster 127.0.0.1 26379 627853b7495425dc3a558ba981f5cbcd619b6417
sentinel known-sentinel mymaster 127.0.0.1 26378 0a825a64892c136b033f1a961f3f4feb92ef2402
sentinel monitor resque 127.0.0.1 10001 4
# Generated by CONFIG REWRITE
dir "/Users/qqr"
sentinel down-after-milliseconds resque 10000
sentinel parallel-syncs resque 5
sentinel config-epoch resque 0
sentinel leader-epoch resque 0
sentinel known-slave resque 127.0.0.1 6379
sentinel known-sentinel resque 127.0.0.1 26379 627853b7495425dc3a558ba981f5cbcd619b6417
sentinel known-sentinel resque 127.0.0.1 26378 0a825a64892c136b033f1a961f3f4feb92ef2402
sentinel current-epoch 3

 

 

小结:

      1.这仅仅是个简单试用,很多配置还是看原文代码

      2.原理是通过发布订阅完成监控,挺好的~。~,很多东西还没深入,有错请指出

     

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics