Jedis使用指南

ゞ 浴缸里的玫瑰 2021-09-22 07:28 509阅读 0赞

简介

Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用。
Jedis源码工程地址:

https://github.com/xetorthio/jedis

使用

想要使用Jedis必须加载jar包或者添加maven依赖,jar包可以自己上网下载,我的是Maven项目,所以在pom.xml中增加如下语句:

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.9.0</version>
  5. <type>jar</type>
  6. <scope>compile</scope>
  7. </dependency>

简单的Jedis实例

在加载Jedis JAR包之后,我们可以直接使用新建一个Jedis实例的方法,来建立一个到Redis的连接,并进行操作,以下是一个简单的jedis实例:

  1. public void setup() {
  2. //连接redis服务器
  3. jedis = new Jedis("172.24.4.183", 6379);
  4. // jedis.auth("redis");//验证密码,如果需要验证的话
  5. }
  6. /**
  7. * 键操作
  8. */
  9. public void testKey() throws InterruptedException{
  10. System.out.println("清空数据:"+jedis.flushDB());
  11. System.out.println("判断某个键是否存在:"+jedis.exists("username"));
  12. System.out.println("新增<'username','xmr'>的键值对:"+jedis.set("username", "xmr"));
  13. System.out.println(jedis.exists("username"));
  14. System.out.println("新增<'password','password'>的键值对:"+jedis.set("password", "123"));
  15. System.out.print("系统中所有的键如下:");
  16. Set<String> keys = jedis.keys("*");
  17. System.out.println(keys);
  18. System.out.println("删除键password:"+jedis.del("password"));
  19. System.out.println("判断键password是否存在:"+jedis.exists("password"));
  20. System.out.println("设置键username的过期时间为5s:"+jedis.expire("username", 8));
  21. TimeUnit.SECONDS.sleep(2);
  22. System.out.println("查看键username的剩余生存时间:"+jedis.ttl("username"));
  23. System.out.println("移除键username的生存时间:"+jedis.persist("username"));
  24. System.out.println("查看键username的剩余生存时间:"+jedis.ttl("username"));
  25. System.out.println("查看键username所存储的值的类型:"+jedis.type("username"));
  26. }

输出结果:

Center

字符串操作

输出结果:

  1. public void testString() throws InterruptedException {
  2. jedis.flushDB();
  3. System.out.println("===========增加数据===========");
  4. System.out.println(jedis.set("key1", "value1"));
  5. System.out.println(jedis.set("key2", "value2"));
  6. System.out.println(jedis.set("key3", "value3"));
  7. System.out.println("删除键key2:" + jedis.del("key2"));
  8. System.out.println("获取键key2:" + jedis.get("key2"));
  9. System.out.println("修改key1:" + jedis.set("key1", "1"));
  10. System.out.println("获取key1的值:" + jedis.get("key1"));
  11. System.out.println("在key3后面加入值:" + jedis.append("key3", "End"));
  12. System.out.println("key3的值:" + jedis.get("key3"));
  13. System.out.println("增加多个键值对:" + jedis.mset("key01", "value01", "key02", "value02", "key03", "value03"));
  14. System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03"));
  15. System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03", "key04"));
  16. System.out.println("删除多个键值对:" + jedis.del(new String[]{"key01", "key02"}));
  17. System.out.println("获取多个键值对:" + jedis.mget("key01", "key02", "key03"));
  18. jedis.flushDB();
  19. System.out.println("===========新增键值对,防止覆盖原先值==============");
  20. System.out.println(jedis.setnx("key1", "value1"));
  21. System.out.println(jedis.setnx("key2", "value2"));
  22. System.out.println(jedis.setnx("key2", "value2-new"));
  23. System.out.println(jedis.get("key1"));
  24. System.out.println(jedis.get("key2"));
  25. System.out.println("===========新增键值对并设置有效时间=============");
  26. System.out.println(jedis.setex("key3", 2, "value3"));
  27. System.out.println(jedis.get("key3"));
  28. TimeUnit.SECONDS.sleep(3);
  29. System.out.println(jedis.get("key3"));
  30. System.out.println("===========获取原值,更新为新值==========");//GETSET is an atomic set this value and return the old value command.
  31. System.out.println(jedis.getSet("key2", "key2GetSet"));
  32. System.out.println(jedis.get("key2"));
  33. System.out.println("获得key2的值的字串:" + jedis.getrange("key2", 2, 4));
  34. }

输出结果:

Center 1

哈希操作

输出结果:

  1. /**
  2. * redis操作Hash
  3. */
  4. public void testHash() {
  5. jedis.flushDB();
  6. Map<String, String> map = new HashMap<>();
  7. map.put("key1", "value1");
  8. map.put("key2", "value2");
  9. map.put("key3", "value3");
  10. map.put("key4", "value4");
  11. jedis.hmset("hash", map);
  12. jedis.hset("hash", "key5", "value5");
  13. System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));//return Map<String,String>
  14. System.out.println("散列hash的所有键为:" + jedis.hkeys("hash"));//return Set<String>
  15. System.out.println("散列hash的所有值为:" + jedis.hvals("hash"));//return List<String>
  16. System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 6));
  17. System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
  18. System.out.println("将key6保存的值加上一个整数,如果key6不存在则添加key6:" + jedis.hincrBy("hash", "key6", 3));
  19. System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
  20. System.out.println("删除一个或者多个键值对:" + jedis.hdel("hash", "key2"));
  21. System.out.println("散列hash的所有键值对为:" + jedis.hgetAll("hash"));
  22. System.out.println("散列hash中键值对的个数:" + jedis.hlen("hash"));
  23. System.out.println("判断hash中是否存在key2:" + jedis.hexists("hash", "key2"));
  24. System.out.println("判断hash中是否存在key3:" + jedis.hexists("hash", "key3"));
  25. System.out.println("获取hash中的值:" + jedis.hmget("hash", "key3"));
  26. System.out.println("获取hash中的值:" + jedis.hmget("hash", "key3", "key4"));
  27. }

输出结果:

Center 2

列表操作

代码如下

  1. public void testList() {
  2. jedis.flushDB();
  3. System.out.println("===========添加一个list===========");
  4. jedis.lpush("lists", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
  5. jedis.lpush("lists", "HashSet");
  6. jedis.lpush("lists", "TreeSet");
  7. jedis.lpush("lists", "TreeMap");
  8. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));//-1代表倒数第一个元素,-2代表倒数第二个元素
  9. System.out.println("lists区间0-3的元素:" + jedis.lrange("lists", 0, 3));
  10. System.out.println("===============================");
  11. // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
  12. System.out.println("删除指定元素个数:" + jedis.lrem("lists", 2, "HashMap"));
  13. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  14. System.out.println("删除下表0-3区间之外的元素:" + jedis.ltrim("lists", 0, 3));
  15. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  16. System.out.println("lists列表出栈(左端):" + jedis.lpop("lists"));
  17. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  18. System.out.println("lists添加元素,从列表右端,与lpush相对应:" + jedis.rpush("lists", "EnumMap"));
  19. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  20. System.out.println("lists列表出栈(右端):" + jedis.rpop("lists"));
  21. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  22. System.out.println("修改lists指定下标1的内容:" + jedis.lset("lists", 1, "LinkedArrayList"));
  23. System.out.println("lists的内容:" + jedis.lrange("lists", 0, -1));
  24. System.out.println("===============================");
  25. System.out.println("lists的长度:" + jedis.llen("lists"));
  26. System.out.println("获取lists下标为2的元素:" + jedis.lindex("lists", 2));
  27. System.out.println("===============================");
  28. jedis.lpush("sortedList", "3", "6", "2", "0", "7", "4");
  29. System.out.println("sortedList排序前:" + jedis.lrange("sortedList", 0, -1));
  30. System.out.println(jedis.sort("sortedList"));
  31. System.out.println("sortedList排序后:" + jedis.lrange("sortedList", 0, -1));
  32. }

输出结果:

Center 3

集合(Set)操作

  1. public void testSet() {
  2. jedis.flushDB();
  3. System.out.println("============向集合中添加元素============");
  4. System.out.println(jedis.sadd("eleSet", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
  5. System.out.println(jedis.sadd("eleSet", "e6"));
  6. System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
  7. System.out.println("删除一个元素e0:" + jedis.srem("eleSet", "e0"));
  8. System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
  9. System.out.println("删除两个元素e7和e6:" + jedis.srem("eleSet", "e7", "e6"));
  10. System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
  11. System.out.println("随机的移除集合中的一个元素:" + jedis.spop("eleSet"));
  12. System.out.println("eleSet的所有元素为:" + jedis.smembers("eleSet"));
  13. System.out.println("eleSet中包含元素的个数:" + jedis.scard("eleSet"));
  14. System.out.println("e1是否在eleSet中:" + jedis.sismember("eleSet", "e1"));
  15. System.out.println("=================================");
  16. System.out.println(jedis.sadd("eleSet1", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5"));
  17. System.out.println(jedis.sadd("eleSet2", "e1", "e2", "e4", "e3", "e0", "e8"));
  18. System.out.println("将eleSet1中删除e1并存入eleSet3中:" + jedis.smove("eleSet1", "eleSet3", "e1"));
  19. System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
  20. System.out.println("eleSet3中的元素:" + jedis.smembers("eleSet3"));
  21. System.out.println("============集合运算=================");
  22. System.out.println("eleSet1中的元素:" + jedis.smembers("eleSet1"));
  23. System.out.println("eleSet2中的元素:" + jedis.smembers("eleSet2"));
  24. System.out.println("eleSet1和eleSet2的交集:" + jedis.sinter("eleSet1", "eleSet2"));
  25. System.out.println("eleSet1和eleSet2的并集:" + jedis.sunion("eleSet1", "eleSet2"));
  26. System.out.println("eleSet1和eleSet2的差集:" + jedis.sdiff("eleSet1", "eleSet2"));//eleSet1中有,eleSet2中没有
  27. }

输出结果:

  1. ============向集合中添加元素============
  2. 8
  3. 1
  4. eleSet的所有元素为:[e0, e5, e3, e8, e7, e2, e1, e4, e6]
  5. 删除一个元素e01
  6. eleSet的所有元素为:[e5, e3, e8, e7, e2, e1, e4, e6]
  7. 删除两个元素e7e62
  8. eleSet的所有元素为:[e1, e4, e3, e5, e2, e8]
  9. 随机的移除集合中的一个元素:e1
  10. eleSet的所有元素为:[e3, e5, e2, e8, e4]
  11. eleSet中包含元素的个数:5
  12. e1是否在eleSet中:false
  13. =================================
  14. 8
  15. 6
  16. eleSet1中删除e1并存入eleSet3中:1
  17. eleSet1中的元素:[e0, e5, e3, e8, e7, e2, e4]
  18. eleSet3中的元素:[e1]
  19. ============集合运算=================
  20. eleSet1中的元素:[e0, e5, e3, e8, e7, e2, e4]
  21. eleSet2中的元素:[e3, e1, e4, e0, e8, e2]
  22. eleSet1eleSet2的交集:[e3, e4, e0, e8, e2]
  23. eleSet1eleSet2的并集:[e2, e1, e4, e0, e3, e5, e7, e8]
  24. eleSet1eleSet2的差集:[e5, e7]

有序集合

  1. public void testSortedSet(){
  2. jedis.flushDB();
  3. Map<String,Double> map = new HashMap<>();
  4. map.put("key2",1.5);
  5. map.put("key3",1.6);
  6. map.put("key4",1.9);
  7. System.out.println(jedis.zadd("zset", 3,"key1"));
  8. System.out.println(jedis.zadd("zset",map));
  9. System.out.println("zset中的所有元素:"+jedis.zrangeByScore("zset", 0,100));
  10. System.out.println("zset中key2的分值:"+jedis.zscore("zset", "key2"));
  11. System.out.println("zset中key2的排名:"+jedis.zrank("zset", "key2"));
  12. System.out.println("删除zset中的元素key3:"+jedis.zrem("zset", "key3"));
  13. System.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1));
  14. System.out.println("zset中元素的个数:"+jedis.zcard("zset"));
  15. System.out.println("zset中分值在1-4之间的元素的个数:"+jedis.zcount("zset", 1, 4));
  16. System.out.println("key2的分值加上5:"+jedis.zincrby("zset", 5, "key2"));
  17. System.out.println("key3的分值加上4:"+jedis.zincrby("zset", 4, "key3"));
  18. System.out.println("zset中的所有元素:"+jedis.zrange("zset", 0, -1));
  19. }

输出结果:

  1. 1
  2. 3
  3. zset中的所有元素:[key2, key3, key4, key1]
  4. zsetkey2的分值:1.5
  5. zsetkey2的排名:0
  6. 删除zset中的元素key31
  7. zset中的所有元素:[key2, key4, key1]
  8. zset中元素的个数:3
  9. zset中分值在1-4之间的元素的个数:3
  10. key2的分值加上56.5
  11. key3的分值加上44.0
  12. zset中的所有元素:[key4, key1, key3, key2]

排序sort

  1. public void testSort(){
  2. jedis.flushDB();
  3. jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap");
  4. System.out.println("collections的内容:"+jedis.lrange("collections", 0, -1));
  5. SortingParams sortingParameters = new SortingParams();
  6. System.out.println(jedis.sort("collections",sortingParameters.alpha()));
  7. System.out.println("===============================");
  8. jedis.lpush("sortedList", "3","6","2","0","7","4");
  9. System.out.println("sortedList排序前:"+jedis.lrange("sortedList", 0, -1));
  10. System.out.println("升序:"+jedis.sort("sortedList", sortingParameters.asc()));
  11. System.out.println("升序:"+jedis.sort("sortedList", sortingParameters.desc()));
  12. System.out.println("===============================");
  13. jedis.lpush("userlist", "33");
  14. jedis.lpush("userlist", "22");
  15. jedis.lpush("userlist", "55");
  16. jedis.lpush("userlist", "11");
  17. jedis.hset("user:66", "name", "66");
  18. jedis.hset("user:55", "name", "55");
  19. jedis.hset("user:33", "name", "33");
  20. jedis.hset("user:22", "name", "79");
  21. jedis.hset("user:11", "name", "24");
  22. jedis.hset("user:11", "add", "beijing");
  23. jedis.hset("user:22", "add", "shanghai");
  24. jedis.hset("user:33", "add", "guangzhou");
  25. jedis.hset("user:55", "add", "chongqing");
  26. jedis.hset("user:66", "add", "xi'an");
  27. sortingParameters = new SortingParams();
  28. sortingParameters.get("user:*->name");
  29. sortingParameters.get("user:*->add");
  30. System.out.println(jedis.sort("userlist",sortingParameters));
  31. }

输出结果:

  1. collections的内容:[LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]
  2. [ArrayList, HashMap, LinkedHashMap, Stack, Vector, WeakHashMap]
  3. ===============================
  4. sortedList排序前:[4, 7, 0, 2, 6, 3]
  5. 升序:[0, 2, 3, 4, 6, 7]
  6. 升序:[7, 6, 4, 3, 2, 0]
  7. ===============================
  8. [24, beijing, 79, shanghai, 33, guangzhou, 55, chongqing]

JedisPool应用

虽然我们可以简单地创建Jedis使用,但每次操作的时候,都建立连接,很耗费性能。解决方法就是从一个连接池中取出连接对象,用完还回去。使用连接池的方案还能解决很多同步性问题。在Jedis中,管理Redis连接的类是JedisPool。要想使用JedisPool需要添加jar包或依赖库,在pom.xml中添加

  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-pool2</artifactId>
  4. <version>2.4.2</version>
  5. </dependency>
  • 普通连接池连接

实现方式有两种,一种是通过配置文件(properties文件),我的文件名是jedisPool.properties:

  1. #最大分配的对象数
  2. redis.pool.maxTotal=1024
  3. #最大能够保持idel状态的对象数
  4. redis.pool.maxIdle=200
  5. #当池内没有返回对象时,最大等待时间
  6. redis.pool.maxWait=1000
  7. #当调用borrow Object方法时,是否进行有效性检查
  8. redis.pool.testOnBorrow=true
  9. #当调用return Object方法时,是否进行有效性检查
  10. redis.pool.testOnReturn=true
  11. #IP
  12. redis.ip=172.24.4.183
  13. #Port
  14. redis.port=6379

java类如下:

  1. public class JedisUtil {
  2. private static Jedis jedis;
  3. private static JedisPool jedisPool = null;
  4. /**
  5. * 初始化Redis连接池
  6. */
  7. static {
  8. ResourceBundle bundle = ResourceBundle.getBundle("jedisPool");
  9. if (bundle == null) {
  10. throw new IllegalArgumentException(
  11. "[jedisPool.properties] is not found!");
  12. }
  13. JedisPoolConfig config = new JedisPoolConfig();
  14. config.setMaxTotal(Integer.valueOf(bundle
  15. .getString("redis.pool.maxTotal")));
  16. config.setMaxIdle(Integer.valueOf(bundle
  17. .getString("redis.pool.maxIdle")));
  18. config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
  19. config.setTestOnBorrow(Boolean.valueOf(bundle
  20. .getString("redis.pool.testOnBorrow")));
  21. config.setTestOnReturn(Boolean.valueOf(bundle
  22. .getString("redis.pool.testOnReturn")));
  23. jedisPool = new JedisPool(config, bundle.getString("redis.ip"),
  24. Integer.valueOf(bundle.getString("redis.port")));
  25. // 从池中获取一个Jedis对象
  26. jedis = jedisPool .getResource();
  27. }
  28. public void add(String sn) {
  29. jedis.sadd("snSet", sn);
  30. jedisPool.destroy();
  31. }
  32. public void remove(String sn) {
  33. jedis.srem("snSet", sn);
  34. }
  35. public boolean isExist(String sn) {
  36. Set<String> snSet = jedis.smembers("snSet");
  37. return snSet.contains(sn);
  38. }
  39. public static void main(String[] args) {
  40. String keys = "name";
  41. // 删数据
  42. jedis.del(keys);
  43. // 存数据
  44. jedis.set(keys, "snowolf");
  45. // 取数据
  46. String value = jedis.get(keys);
  47. System.out.println(value);
  48. // 释放对象池
  49. // jedisPool.returnResource(jedis);
  50. }
  51. }

而直接通过代码实现的话,其实也是一个原理:

  1. public class JedisUtil1 {
  2. private static JedisUtil1 instance = null;
  3. private Jedis jedis;
  4. private static JedisPool jedisPool = null;
  5. //Redis服务器IP
  6. private static String HOST = "172.24.4.183";
  7. //Redis的端口号
  8. private static int PORT = 6379;
  9. //可用连接实例的最大数目,默认值为8;
  10. //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
  11. private static int MAX_ACTIVE = 1024;
  12. //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
  13. private static int MAX_IDLE = 200;
  14. private static int TIMEOUT = 10000;
  15. //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
  16. private static boolean TEST_ON_BORROW = true;
  17. /**
  18. * 初始化Redis连接池
  19. */
  20. static {
  21. try {
  22. JedisPoolConfig config = new JedisPoolConfig();
  23. config.setMaxTotal(MAX_ACTIVE);
  24. config.setMaxIdle(MAX_IDLE);
  25. config.setMaxWaitMillis(TIMEOUT);
  26. config.setTestOnBorrow(TEST_ON_BORROW);
  27. jedisPool = new JedisPool(config, HOST, PORT);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  • Sentinel连接池连接

Sentinel连接池用于应对Redis的Sentinel的主从切换机制,能够正确在服务器宕机导致服务器切换时得到正确的服务器连接,当服务器采用该部署策略的时候推荐使用该连接池进行操作

  1. private static Jedis jedis;
  2. private static JedisSentinelPool jedisSentinelPool = null;
  3. /**
  4. * 初始化Redis连接池
  5. */
  6. static {
  7. ResourceBundle bundle = ResourceBundle.getBundle("jedisSentinePool");
  8. if (bundle == null) {
  9. throw new IllegalArgumentException(
  10. "[jedisSentinePool.properties] is not found!");
  11. }
  12. JedisPoolConfig config = new JedisPoolConfig();
  13. config.setMaxTotal(Integer.valueOf(bundle
  14. .getString("redis.pool.maxTotal")));
  15. config.setMaxIdle(Integer.valueOf(bundle
  16. .getString("redis.pool.maxIdle")));
  17. config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
  18. config.setTestOnBorrow(Boolean.valueOf(bundle
  19. .getString("redis.pool.testOnBorrow")));
  20. config.setTestOnReturn(Boolean.valueOf(bundle
  21. .getString("redis.pool.testOnReturn")));
  22. //监听器列表
  23. Set<String> sentinels = new HashSet<>();
  24. /**
  25. *
  26. //监听器1
  27. sentinels.add(new HostAndPort("172.24.4.183", 26379).toString());
  28. //监听器2
  29. sentinels.add(new HostAndPort("172.24.4.184", 26379).toString());
  30. //实际使用的时候在properties里配置即可:redis.sentinel.hostandports=172.24.4.183:26379, 172.24.4.184:26379
  31. //然后使用 bundle.getString("redis.sentinel.hostandports");获取地址
  32. */
  33. //mastername是服务器上的master的名字,在master服务器的sentinel.conf中配置
  34. String masterName = bundle.getString("redis.sentinel.masterName");
  35. sentinels.add(bundle.getString("redis.sentinel.hostandports"));
  36. //初始化连接池
  37. jedisSentinelPool = new JedisSentinelPool(masterName,
  38. sentinels, config);
  39. // 从池中获取一个Jedis对象
  40. jedis = jedisSentinelPool.getResource();
  41. }
  • ShardedJedisPool连接池分片连接

Memcached完全基于分布式集群,而Redis是Master-Slave,Redis在容灾处理方面可以通过服务器端配置Master-Slave模式来实现。如果想把Reids做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具来实现一致性哈希分布存储,即key分片存储。

shared一致性哈希采用以下方案:

  1. Redis服务器节点划分:将每台服务器节点采用hash算法划分为160个虚拟节点(可以配置划分权重)
  2. 将划分虚拟节点采用TreeMap存储
  3. 对每个Redis服务器的物理连接采用LinkedHashMap存储
  4. 对Key or KeyTag 采用同样的hash算法,然后从TreeMap获取大于等于键hash值得节点,取最邻近节点存储;当key的hash值大于虚拟节点hash值得最大值时,存入第一个虚拟节点

sharded采用的hash算法:MD5 和 MurmurHash两种;默认采用64位的MurmurHash算法;有兴趣的可以研究下~

保留前面的JedisPoolConfig,新增两个Redis的IP(redis1.ip,redis2.ip),完成两个JedisShardInfo实例,并将其丢进List中:

  1. private static ShardedJedis jedis;
  2. private static ShardedJedisPool shardedJedisPool = null;
  3. /**
  4. * 初始化Redis连接池
  5. */
  6. static {
  7. ResourceBundle bundle = ResourceBundle.getBundle("sharedJedisPool");
  8. if (bundle == null) {
  9. throw new IllegalArgumentException(
  10. "[jedisPool.properties] is not found!");
  11. }
  12. JedisPoolConfig config = new JedisPoolConfig();
  13. config.setMaxTotal(Integer.valueOf(bundle
  14. .getString("redis.pool.maxTotal")));
  15. config.setMaxIdle(Integer.valueOf(bundle
  16. .getString("redis.pool.maxIdle")));
  17. config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
  18. config.setTestOnBorrow(Boolean.valueOf(bundle
  19. .getString("redis.pool.testOnBorrow")));
  20. config.setTestOnReturn(Boolean.valueOf(bundle
  21. .getString("redis.pool.testOnReturn")));
  22. JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
  23. bundle.getString("redis1.ip"), Integer.valueOf(bundle.getString("redis.port")));
  24. JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
  25. bundle.getString("redis2.ip"), Integer.valueOf(bundle.getString("redis.port")));
  26. List<JedisShardInfo> list = new LinkedList<>();
  27. list.add(jedisShardInfo1);
  28. list.add(jedisShardInfo2);
  29. shardedJedisPool = new ShardedJedisPool(config,list);
  30. // 从池中获取一个Jedis对象
  31. jedis = shardedJedisPool.getResource();
  32. }

参考:

http://www.boyunjian.com/javadoc/org.apache.servicemix.bundles/org.apache.servicemix.bundles.jedis/2.1.0_1/_/redis/clients/jedis/JedisShardInfo.html

https://yq.aliyun.com/articles/236384

http://blog.csdn.net/dslztx/article/details/46743053

http://flyingsnail.blog.51cto.com/5341669/1371650

http://www.tuicool.com/articles/vaqABb

http://www.importnew.com/19321.html

http://www.cnblogs.com/liuling/p/2014-4-19-04.html

http://www.cnblogs.com/libaoting/p/4418007.html

http://blog.csdn.net/fachang/article/details/7984123

http://blog.csdn.net/aubdiy/article/details/53511410

http://blog.csdn.net/moxiaomomo/article/details/17588483

发表评论

表情:
评论列表 (有 0 条评论,509人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Jedis使用总结

    Jedis使用总结 前段时间细节的了解了Jedis的使用,Jedis是redis的java版本的客户端实现。 本文做个总结,主要分享如下内容:

    相关 jedis使用

    一、Redis Client介绍 1.1、简介 Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用。 Jedis源

    相关 Jedis使用示例

      Jedis 是 Redis 官方首选的 Java 客户端开发包。 工作过程总结的一个示例,贴出来,如下: Java代码   ![收藏代码][icon_star.png