單機模式: 分散式模式 ShardedJe、dis是基於一致性哈希演算法實現的分散式Redis集群客戶端 ...
單機模式:
1 package com.ljq.utils; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 /** 8 * Redis操作介面 9 * 10 * @author NiceCui 11 * @version 1.0 2017-6-14 上午08:54:14 12 */ 13 public class RedisAPI { 14 private static JedisPool pool = null; 15 16 /** 17 * 構建redis連接池 18 * 19 * @param ip 20 * @param port 21 * @return JedisPool 22 */ 23 public static JedisPool getPool() { 24 if (pool == null) { 25 JedisPoolConfig config = new JedisPoolConfig(); 26 //控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲取; 27 //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。 28 config.setMaxActive(500); 29 //控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。 30 config.setMaxIdle(5); 31 //表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException; 32 config.setMaxWait(1000 * 100); 33 //在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的; 34 config.setTestOnBorrow(true); 35 pool = new JedisPool(config, "192.168.2.191", 8888); 36 } 37 return pool; 38 } 39 40 /** 41 * 返還到連接池 42 * 43 * @param pool 44 * @param redis 45 */ 46 public static void returnResource(JedisPool pool, Jedis redis) { 47 if (redis != null) { 48 pool.returnResource(redis); 49 } 50 } 51 52 /** 53 * 獲取數據 54 * 55 * @param key 56 * @return 57 */ 58 public static String get(String key){ 59 String value = null; 60 61 JedisPool pool = null; 62 Jedis jedis = null; 63 try { 64 pool = getPool(); 65 jedis = pool.getResource(); 66 value = jedis.get(key); 67 } catch (Exception e) { 68 //釋放redis對象 69 pool.returnBrokenResource(jedis); 70 e.printStackTrace(); 71 } finally { 72 //返還到連接池 73 returnResource(pool, jedis); 74 } 75 76 return value; 77 } 78 }
分散式模式
ShardedJe、dis是基於一致性哈希演算法實現的分散式Redis集群客戶端
1 package com.jd.redis.client; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import redis.clients.jedis.JedisPoolConfig; 7 import redis.clients.jedis.JedisShardInfo; 8 import redis.clients.jedis.ShardedJedis; 9 import redis.clients.jedis.ShardedJedisPool; 10 import redis.clients.util.Hashing; 11 import redis.clients.util.Sharded; 12 13 publicclass RedisShardPoolTest { 14 15 static ShardedJedisPoolpool; 16 17 static{ 18 19 JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置 20 21 config.setMaxActive(500);//最大活動的對象個數 22 23 config.setMaxIdle(1000 * 60);//對象最大空閑時間 24 25 config.setMaxWait(1000 * 10);//獲取對象時最大等待時間 26 27 config.setTestOnBorrow(true); 28 29 String hostA = "10.10.224.44"; 30 31 int portA = 6379; 32 33 String hostB = "10.10.224.48"; 34 35 int portB = 6379; 36 37 List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2); 38 39 JedisShardInfo infoA = new JedisShardInfo(hostA, portA); 40 41 infoA.setPassword("redis.360buy"); 42 43 JedisShardInfo infoB = new JedisShardInfo(hostB, portB); 44 45 infoB.setPassword("redis.360buy"); 46 47 jdsInfoList.add(infoA); 48 49 jdsInfoList.add(infoB); 50 51 52 pool =new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH, 53 Sharded.DEFAULT_KEY_TAG_PATTERN); 54 //傳入連接池配置、分散式redis伺服器主機信息、分片規則(存儲到哪台redis伺服器) 55 } 56 57 58 /** 59 60 * @param args 61 62 */ 63 64 publicstaticvoid main(String[] args) { 65 66 for(int i=0; i<100; i++){ 67 String key =generateKey(); 68 //key += "{aaa}"; 69 ShardedJedis jds =null; 70 try { 71 jds =pool.getResource(); 72 System.out.println(key+":"+jds.getShard(key).getClient().getHost()); 73 System.out.println(jds.set(key,"1111111111111111111111111111111")); 74 }catch (Exception e) { 75 e.printStackTrace(); 76 } 77 finally{ 78 pool.returnResourceObject(jds); 79 } 80 81 } 82 83 } 84 85 86 privatestaticintindex = 1; 87 88 publicstatic String generateKey(){ 89 90 return String.valueOf(Thread.currentThread().getId())+"_"+(index++); 91 92 } 93 }