Esempio n. 1
0
  @Test
  public void jedisTest2() {
    JedisSentinelPool sentinelPool = (JedisSentinelPool) ac.getBean("jedisSentinelPool");
    Jedis jedis = sentinelPool.getResource();
    System.out.println("current Host:" + sentinelPool.getCurrentHostMaster());

    String key = "a";

    String cacheData = jedis.get(key);

    if (cacheData == null) {
      jedis.del(key);
    }

    jedis.set(key, "aaa"); // 写入

    System.out.println(jedis.get(key)); // 读取

    System.out.println(
        "current Host:" + sentinelPool.getCurrentHostMaster()); // down掉master,观察slave是否被提升为master

    jedis.set(key, "bbb"); // 测试新master的写入

    System.out.println(jedis.get(key)); // 观察读取是否正常

    sentinelPool.close();
    jedis.close();
  }
Esempio n. 2
0
 /**
  * 测试哨兵模式
  *
  * <p>one master(one sentinel) - one slave
  */
 @Test
 public void testSentinel() {
   // 哨兵初始化
   HostAndPort sentinelAddr = new HostAndPort("192.168.1.201", 26379);
   Set<String> sentinels = new HashSet<String>();
   sentinels.add(sentinelAddr.toString());
   JedisSentinelPool sentinelPool =
       new JedisSentinelPool("mymaster", sentinels, new GenericObjectPoolConfig());
   // 线程初始化
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   AtomicInteger calcCount = new AtomicInteger(0);
   AtomicInteger failCount = new AtomicInteger(0);
   for (int t = 0; t < TIMES; t++) {
     ThreadPool threadPool = new ThreadPool(THREADS);
     SentinelThread sentinelThread = new SentinelThread(sentinelPool, calcCount, failCount);
     threadPool.executeThread(sentinelThread);
     try {
       TimeUnit.SECONDS.sleep(SECONDS);
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
       System.err.println("error !!!");
     }
   }
   sentinelPool.close();
   stopWatch.stop();
   // 打印结果
   System.out.println(
       String.format(
           "redis sentinel work finish, times:%d(milliseconds), fails:%d",
           stopWatch.getTime(), failCount.get()));
 }
 /**
  * del
  *
  * @param key
  */
 public void del(byte[] key) {
   Jedis jedis = jedisPool.getResource();
   try {
     jedis.del(key);
   } finally {
     jedisPool.returnResource(jedis);
   }
 }
 /** flush */
 public void flushDB() {
   Jedis jedis = jedisPool.getResource();
   try {
     jedis.flushDB();
   } finally {
     jedisPool.returnResource(jedis);
   }
 }
 /** size */
 public Long dbSize() {
   Long dbSize = 0L;
   Jedis jedis = jedisPool.getResource();
   try {
     dbSize = jedis.dbSize();
   } finally {
     jedisPool.returnResource(jedis);
   }
   return dbSize;
 }
 /**
  * keys
  *
  * @param regex
  * @return
  */
 public Set<byte[]> keys(String pattern) {
   Set<byte[]> keys = null;
   Jedis jedis = jedisPool.getResource();
   try {
     keys = jedis.keys(pattern.getBytes());
   } finally {
     jedisPool.returnResource(jedis);
   }
   return keys;
 }
 /**
  * set
  *
  * @param key
  * @param value
  * @param expire
  * @return
  */
 public byte[] set(byte[] key, byte[] value, int expire) {
   Jedis jedis = jedisPool.getResource();
   try {
     jedis.set(key, value);
     if (expire != 0) {
       jedis.expire(key, expire);
     }
   } finally {
     jedisPool.returnResource(jedis);
   }
   return value;
 }
Esempio n. 8
0
 @SuppressWarnings("resource")
 public static void main(String[] args) {
   Set<String> hostSet = new HashSet<String>();
   hostSet.add(new HostAndPort("192.168.1.106", 26379).toString());
   hostSet.add(new HostAndPort("192.168.1.106", 26380).toString());
   hostSet.add(new HostAndPort("192.168.1.106", 26381).toString());
   JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", hostSet);
   Jedis master = sentinelPool.getResource();
   master.set("hello", "world");
   master.close();
   Jedis master2 = sentinelPool.getResource();
   String value = master2.get("hello");
   System.out.println("hello: " + value);
   master2.close();
   sentinelPool.destroy();
 }
 /**
  * 判断该键是否存在,存在返回1,否则返回0。
  *
  * @param key
  * @param value
  * @return
  */
 public static boolean exists(final String key) {
   boolean l = false;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.exists(key);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 如果该Key已经存在,append命令将参数Value的数据追加到已存在Value的末尾。
  * 如果该Key不存在,append命令将会创建一个新的Key/Value。返回追加后的Value的长度。
  *
  * @param key
  * @param value
  * @return
  */
 public static Long append(final String key, final String value) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     jedis.append(key, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 获取指定Key的Value,如果该Key不存在,返回null。
  *
  * @param key
  * @return
  */
 public static byte[] get(byte[] key) {
   byte[] l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.get(key);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将字符串值 value 关联到 key 。 如果 key 已经持有其他值, SET 就覆写旧值,无视类型。
  *
  * @param key
  * @param value
  * @return 总是返回 OK ,因为 SET 不可能失败。
  */
 public static String set(String key, Object object) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     jedis.set(SerializeUtils.serialize(key), SerializeUtils.serialize(object));
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将字符串值 value 关联到 key 。 如果 key 已经持有其他值, SET 就覆写旧值,无视类型。
  *
  * @param key
  * @param value
  * @return 总是返回 OK ,因为 SET 不可能失败。
  */
 public static String set(byte[] key, byte[] value) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.set(key, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 返回哈希表 key 中所有域的值。
  *
  * @param key
  * @return 一个包含哈希表中所有值的表。 当 key 不存在时,返回一个空表。
  */
 public static List<String> hvals(String key) {
   List<String> l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.hvals(key);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 查看哈希表 key 中,给定域 field 是否存在。
  *
  * @param key
  * @param field
  * @return
  */
 public static Boolean hexists(String key, String field) {
   Boolean l = false;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.hexists(key, field);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将当前数据库中的mysetkey键移入到ID为dbIndex的数据库中
  *
  * @param key
  * @param dbIndex
  * @return
  */
 public static Long move(byte[] key, int dbIndex) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.move(key, dbIndex);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * EXPIREAT 的作用和 EXPIRE 类似,都用于为 key 设置生存时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。
  *
  * @param key
  * @param unixTime
  * @return
  */
 public static Long expireAt(String key, long unixTime) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.expireAt(key, unixTime);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 设置某个key的过期时间(单位:秒), 在超过该时间后,Key被自动的删除。 如果该Key在超时之前被修改,与该键关联的超时将被移除。
  *
  * @param key
  * @param seconds
  * @return
  */
 public static Long expire(byte[] key, int seconds) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.expire(key, seconds);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 同时将多个 field-value (域-值)对设置到哈希表 key 中。 此命令会覆盖哈希表中已存在的域。 如果 key 不存在,一个空哈希表被创建并执行 HMSET 操作。
  *
  * @param key
  * @param map
  * @return 如果命令执行成功,返回 OK 。 当 key 不是哈希表(hash)类型时,返回一个错误。
  */
 public static String hmset(String key, Map<String, String> map) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.hmset(key, map);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将哈希表 key 中的域 field 的值设置为 value ,当且仅当域 field 不存在。 若域 field 已经存在,该操作无效。 如果 key 不存在,一个新哈希表被创建并执行
  * HSETNX 命令。
  *
  * @param key
  * @param field
  * @param value
  * @return 设置成功,返回 1 。 如果给定域已经存在且没有操作被执行,返回 0 。
  */
 public static Long hsetnx(String key, String field, String value) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.hsetnx(key, field, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 返回 key 所储存的值的类型。
  *
  * @param key
  * @return none (key不存在) string (字符串) list (列表) set (集合) zset (有序集) hash (哈希表)
  */
 public static String type(String key) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.type(key);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。 负数偏移量表示从字符串最后开始计数, -1
  * 表示最后一个字符, -2 表示倒数第二个,以此类推。
  *
  * @param key
  * @param start
  * @param end
  * @return
  */
 public static String getrange(String key, long start, long end) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.getrange(key, start, end);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。 位的设置或清除取决于 value 参数,可以是 0 也可以是 1 。 当 key 不存在时,自动生成一个新的字符串值。
  * 字符串会进行伸展(grown)以确保它可以将 value 保存在指定的偏移量上。当字符串值进行伸展时,空白位置以 0 填充。 offset 参数必须大于或等于 0 ,小于 2^32 (bit
  * 映射被限制在 512 MB 之内)。
  *
  * @param key
  * @param offset
  * @param value
  * @return 指定偏移量原来储存的位
  */
 public static Boolean setbit(String key, long offset, boolean value) {
   Boolean l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.setbit(key, offset, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 用 value 参数覆写(overwrite)给定 key 所储存的字符串值,从偏移量 offset 开始。 不存在的 key 当作空白字符串处理。
  *
  * @param key
  * @param offset
  * @param value
  * @return 被 SETRANGE 修改之后,字符串的长度。
  */
 public static Long setrange(String key, long offset, String value) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.setrange(key, offset, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将字符串值 value 关联到 key , 可以设置过期时间 如果 key 已经持有其他值, SET 就覆写旧值,无视类型。
  *
  * @param key
  * @param seconds
  * @param value
  * @return 设置成功时返回 OK 。 当 seconds 参数不合法时,返回一个错误
  */
 public static String setex(String key, int seconds, String value) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.setex(key, seconds, value);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 返回一组指定Keys的Values的列表。
  *
  * @param keys
  * @return
  */
 public static List<String> mget(String[] keys) {
   List<String> values = new ArrayList<String>();
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     for (String key : keys) {
       values.add(jedis.get(key));
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return values;
 }
 /**
  * 将字符串值 value 关联到 key 。 如果 key 已经持有其他值, SET 就覆写旧值,无视类型。
  *
  * @param key
  * @param value
  * @return 总是返回 OK ,因为 SET 不可能失败。
  */
 public static String set(String key, String value, int expire) {
   String l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.set(key, value);
     if (expire != 0) {
       jedis.expire(key, expire);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return l;
 }
 /**
  * 将字符串值 value 关联到 key 。
  *
  * @param key
  * @param value
  */
 public Object getObject(String key) {
   Object o = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     byte[] bs = jedis.get(SerializeUtils.serialize(key));
     if (bs != null) {
       o = SerializeUtils.unserialize(bs);
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   return o;
 }
Esempio n. 29
0
 @Override
 public void execute() {
   try (Jedis jedis = sentinelPool.getResource()) {
     String ret =
         jedis.setex("" + failCount.incrementAndGet(), 10000, Thread.currentThread().getName());
     if (ret == null || !"OK".equals(ret)) System.err.println("" + failCount.get() + " fail");
   } catch (Exception e) {
     // 记录错误的次数
     failCount.incrementAndGet();
     System.err.println(e.getMessage());
     Thread.currentThread().interrupt();
   }
 } // --> end function execute
 /**
  * 为哈希表 key 中的域 field 的值加上增量 increment 。 增量也可以为负数,相当于对给定域进行减法操作。 如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY
  * 命令。 如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。 对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。 本操作的值被限制在 64
  * 位(bit)有符号数字表示之内。
  *
  * @param key
  * @param field
  * @param increment
  * @return
  */
 public static Long hincrBy(String key, String field, long increment) {
   Long l = null;
   Jedis jedis = null;
   try {
     jedis = jedisPool.getResource();
     l = jedis.hincrBy(key, field, increment);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     if (jedis != null) {
       try {
         jedisPool.returnResource(jedis);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
   // 保证大于0--各库数据不一至可能会有问题。
   if (l < 0) {
     l = hset(key, field, "0");
   }
   return l;
 }