Giter Club home page Giter Club logo

redis-sentinel-client's Introduction

redis-sentinel-client

redis-sentinel(哨兵)模式的分片连接池,根据sentinel集群的master变更自动切换主从,并提供与ShardedJedisPool一样的API。

简述

redis2.x的高可用方案sentinel(哨兵),本身提供redis主从集群的自动管理功能,以及主从自动切换的高可用功能。 而在java客户端使用时需要用到sentinel的客户端,因为调用方式不一样,直接使用jedis老的连接池是无法做到主从切换的效果。 jedis客户端中自带了一个sentinel单实例连接池实现redis.clients.jedis.JedisSentinelPool.java, 使用JedisSentinelPool.java可以很轻松的使用sentinel的功能,达到主从切换的效果,但是jedis包没有提供分片时使用的连接池,并且JedisSentinelPool.java有一个bug,主从切换时消息重复接收,导致连接池初始化2次。

ShardedJedisSentinelPool.java就是在JedisSentinelPool.java的基础上,增加了对分片的支持并修改bug,可以很少的改动老代码进行升级。

工作原理

ShardedJedisSentinelPool.java的实现思路参考jedis包JedisSentinelPool.java,池需要配置sentinel集群地址以及 sentinel集群中的master-name,然后初始化时根据sentinel提供的api去请求master-name当前对应的ip和端口, 然后与普通的jedispool初始化一样,拿着请求回来的ip和端口初始化pool并保存在池中的map(本地路由表)。 同时也根据sentinel的数量启动对应的后台线程,去订阅sentinel集群的master变更消息,收到变更消息之后与 本地路由表对比排除无用的消息,然后开始重新初始化ShardedJedisSentinelPool.java中的连接。

测试代码

     // sentinel配置文件中的master-name列表
    List<String> masters = new ArrayList<String>();
    masters.add("master1");
    masters.add("master2");

    // sentinel集群列表
    Set<String> sentinels = new HashSet<String>();
    sentinels.add("192.168.1.112:26379");

    //初始化连接池
    ShardedJedisSentinelPool pool = new ShardedJedisSentinelPool(masters, sentinels);
    
    //获取jedis客户端
    ShardedJedis jedis = pool.getResource();

代码实现

单实例连接池-JedisSentinelPool.java
分片连接池-ShardedJedisSentinelPool.java

参考资料

Sentinel官方文档
Sentinel集群搭建过程

注意

1.调用ShardedJedis.close()方法需要try-catch,因为当master发生变更后,监控线程会重新初始化连接池中的连接,造成close()方法抛出异常。 2.JedisSentinelPool.java最好不要使用,有重复接收master变更消息,造成pool多次初始化的bug。 使用ShardedJedisSentinelPool.java就可以满足需求了

redis-sentinel-client's People

Contributors

hailin0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.