1. 程式人生 > >jedis連接池詳解(Redis)

jedis連接池詳解(Redis)

自增 tid bject pack cti .html red stat 分布式redis

原子性(atomicity):

一個事務是一個不可分割的最小工作單位,事務中包括的諸操作要麽都做,要麽都不做。

Redis所有單個命令的執行都是原子性的,這與它的單線程機制有關;

Redis命令的原子性使得我們不用考慮並發問題,可以方便的利用原子性自增操作INCR實現簡單計數器功能;

單機模式

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 package com.ljq.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Redis操作接口 * * @author 林計欽 * @version 1.0 2013-6-14 上午08:54:14 */ public class RedisAPI { private static JedisPool pool = null; /**
* 構建redis連接池 * * @param ip * @param port * @return JedisPool */ public static JedisPool getPool() { if (pool == null) { JedisPoolConfig config = new JedisPoolConfig(); //控制一個pool可分配多少個jedis實例,通過pool.getResource()來獲取; //如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis實例,則此時pool的狀態為exhausted(耗盡)。
config.setMaxActive(500); //控制一個pool最多有多少個狀態為idle(空閑的)的jedis實例。 config.setMaxIdle(5); //表示當borrow(引入)一個jedis實例時,最大的等待時間,如果超過等待時間,則直接拋出JedisConnectionException; config.setMaxWait(1000 * 100); //在borrow一個jedis實例時,是否提前進行validate操作;如果為true,則得到的jedis實例均是可用的; config.setTestOnBorrow(true); pool = new JedisPool(config, "192.168.2.191", 8888); } return pool; } /** * 返還到連接池 * * @param pool * @param redis */ public static void returnResource(JedisPool pool, Jedis redis) { if (redis != null) { pool.returnResourceObject(redis); } } /** * 獲取數據 * * @param key * @return */ public static String get(String key){ String value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.get(key); } catch (Exception e) { //釋放redis對象 pool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返還到連接池 returnResource(pool, jedis); } return value; } }

參考文章:

http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html

分布式模式

ShardedJedis是基於一致性哈希算法實現的分布式Redis集群客戶端

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 package com.jd.redis.client; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import redis.clients.util.Hashing; import redis.clients.util.Sharded; publicclass RedisShardPoolTest { static ShardedJedisPoolpool; static{ JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置 config.setMaxActive(500);//最大活動的對象個數 config.setMaxIdle(1000 * 60);//對象最大空閑時間 config.setMaxWait(1000 * 10);//獲取對象時最大等待時間 config.setTestOnBorrow(true); String hostA = "10.10.224.44"; int portA = 6379; String hostB = "10.10.224.48"; int portB = 6379; List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2); JedisShardInfo infoA = new JedisShardInfo(hostA, portA); infoA.setPassword("redis.360buy"); JedisShardInfo infoB = new JedisShardInfo(hostB, portB); infoB.setPassword("redis.360buy"); jdsInfoList.add(infoA); jdsInfoList.add(infoB); pool =new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN); //傳入連接池配置、分布式redis服務器主機信息、分片規則(存儲到哪臺redis服務器) } /** * @param args */ publicstaticvoid main(String[] args) { for(int i=0; i<100; i++){ String key =generateKey(); //key += "{aaa}"; ShardedJedis jds =null; try { jds =pool.getResource(); System.out.println(key+":"+jds.getShard(key).getClient().getHost()); System.out.println(jds.set(key,"1111111111111111111111111111111")); }catch (Exception e) { e.printStackTrace(); } finally{ pool.returnResourceObject(jds); } } } privatestaticintindex = 1; publicstatic String generateKey(){ return String.valueOf(Thread.currentThread().getId())+"_"+(index++); } }

參考文章:

http://blog.csdn.net/lang_man_xing/article/details/38405269

jedis連接池詳解(Redis)