单机模式:
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 }