`

Java Redis实战之Redis + Jedis

阅读更多

目前Redis大概有3中基于Java语言的Client:

  • Jredis
  • Jedis
  • Redis4J

 

一、简单使用Jedis

需要Jedis就从Maven获取吧!
Maven Pom.xml

 

Xml代码  
  1. <dependency>  
  2.     <groupId>redis.clients</groupId>  
  3.     <artifactId>jedis</artifactId>  
  4.     <version>2.1.0</version>  
  5.     <type>jar</type>  
  6.     <scope>compile</scope>  
  7. </dependency>  
[xml] view plaincopy
 
  1. <dependency>  
  2.     <groupId>redis.clients</groupId>  
  3.     <artifactId>jedis</artifactId>  
  4.     <version>2.1.0</version>  
  5.     <type>jar</type>  
  6.     <scope>compile</scope>  
  7. </dependency>  

 

 

 

如果只是简单使用Jedis,以下这么几行代码足够:

 

Java代码  
  1. Jedis jedis = new Jedis("10.11.20.140");  
  2. String keys = "name";  
  3.   
  4. // 删数据  
  5. jedis.del(keys);  
  6. // 存数据  
  7. jedis.set(keys, "snowolf");  
  8. // 取数据  
  9. String value = jedis.get(keys);  
  10.   
  11. System.out.println(value);  
[java] view plaincopy
 
  1. Jedis jedis = new Jedis("10.11.20.140");  
  2. String keys = "name";  
  3.   
  4. // 删数据  
  5. jedis.del(keys);  
  6. // 存数据  
  7. jedis.set(keys, "snowolf");  
  8. // 取数据  
  9. String value = jedis.get(keys);  
  10.   
  11. System.out.println(value);  

 

 

二、池化使用Jedis

Jedis使用commons-pool完成池化实现。

先做个配置文件:

Properties代码  
  1. #最大分配的对象数  
  2. redis.pool.maxActive=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=10.11.20.140  
  13. #Port  
  14. redis.port=6379  

 

 

 在静态代码段中完成初始化:

 

Java代码  
  1. private static JedisPool pool;  
  2. static {  
  3.     ResourceBundle bundle = ResourceBundle.getBundle("redis");  
  4.     if (bundle == null) {  
  5.         throw new IllegalArgumentException(  
  6.                 "[redis.properties] is not found!");  
  7.     }  
  8.     JedisPoolConfig config = new JedisPoolConfig();  
  9.     config.setMaxActive(Integer.valueOf(bundle  
  10.             .getString("redis.pool.maxActive")));  
  11.     config.setMaxIdle(Integer.valueOf(bundle  
  12.             .getString("redis.pool.maxIdle")));  
  13.     config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));  
  14.     config.setTestOnBorrow(Boolean.valueOf(bundle  
  15.             .getString("redis.pool.testOnBorrow")));  
  16.     config.setTestOnReturn(Boolean.valueOf(bundle  
  17.             .getString("redis.pool.testOnReturn")));  
  18.     pool = new JedisPool(config, bundle.getString("redis.ip"),  
  19.             Integer.valueOf(bundle.getString("redis.port")));  
  20. }  
[java] view plaincopy
 
  1. private static JedisPool pool;  
  2. static {  
  3.     ResourceBundle bundle = ResourceBundle.getBundle("redis");  
  4.     if (bundle == null) {  
  5.         throw new IllegalArgumentException(  
  6.                 "[redis.properties] is not found!");  
  7.     }  
  8.     JedisPoolConfig config = new JedisPoolConfig();  
  9.     config.setMaxActive(Integer.valueOf(bundle  
  10.             .getString("redis.pool.maxActive")));  
  11.     config.setMaxIdle(Integer.valueOf(bundle  
  12.             .getString("redis.pool.maxIdle")));  
  13.     config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));  
  14.     config.setTestOnBorrow(Boolean.valueOf(bundle  
  15.             .getString("redis.pool.testOnBorrow")));  
  16.     config.setTestOnReturn(Boolean.valueOf(bundle  
  17.             .getString("redis.pool.testOnReturn")));  
  18.     pool = new JedisPool(config, bundle.getString("redis.ip"),  
  19.             Integer.valueOf(bundle.getString("redis.port")));  
  20. }  

 

 然后,修改前面那段jedis操作Redis

Java代码  
  1. // 从池中获取一个Jedis对象  
  2. Jedis jedis = pool.getResource();  
  3. String keys = "name";  
  4.   
  5. // 删数据  
  6. jedis.del(keys);  
  7. // 存数据  
  8. jedis.set(keys, "snowolf");  
  9. // 取数据  
  10. String value = jedis.get(keys);  
  11.   
  12. System.out.println(value);  
  13.   
  14. // 释放对象池  
  15. pool.returnResource(jedis);  
[java] view plaincopy
 
  1. // 从池中获取一个Jedis对象  
  2. Jedis jedis = pool.getResource();  
  3. String keys = "name";  
  4.   
  5. // 删数据  
  6. jedis.del(keys);  
  7. // 存数据  
  8. jedis.set(keys, "snowolf");  
  9. // 取数据  
  10. String value = jedis.get(keys);  
  11.   
  12. System.out.println(value);  
  13.   
  14. // 释放对象池  
  15. pool.returnResource(jedis);  

 

 改为从对象池中,获取Jedis实例:

 

Java代码  
  1. // 从池中获取一个Jedis对象  
  2. Jedis jedis = pool.getResource();  
[java] view plaincopy
 
  1. // 从池中获取一个Jedis对象  
  2. Jedis jedis = pool.getResource();  

 

 

 

 切记,最后使用后,释放Jedis对象:

 

Java代码  
  1. // 释放对象池  
  2. pool.returnResource(jedis);  
[java] view plaincopy
 
  1. // 释放对象池  
  2. pool.returnResource(jedis);  

 

 

 

三、一致性哈希

 

Memcached完全基于分布式集群,而RedisMaster-Slave,如果想把Reids,做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具,完成一致性哈希。

PS:Memcached是在Server端完成ShardingRedis只能依靠各个ClientSharding。可能会在Redis 3.0系列支持ServerSharding

 

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

 

Java代码  
  1. JedisShardInfo jedisShardInfo1 = new JedisShardInfo(  
  2.                 bundle.getString("redis1.ip"), Integer.valueOf(bundle                       .getString("redis.port")));  
  3. JedisShardInfo jedisShardInfo2 = new JedisShardInfo(  
  4.                 bundle.getString("redis2.ip"), Integer.valueOf(bundle                       .getString("redis.port")));  
  5.   
  6. List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();  
  7. list.add(jedisShardInfo1);  
  8. list.add(jedisShardInfo2);  
[java] view plaincopy
 
  1. JedisShardInfo jedisShardInfo1 = new JedisShardInfo(  
  2.                 bundle.getString("redis1.ip"), Integer.valueOf(bundle                       .getString("redis.port")));  
  3. JedisShardInfo jedisShardInfo2 = new JedisShardInfo(  
  4.                 bundle.getString("redis2.ip"), Integer.valueOf(bundle                       .getString("redis.port")));  
  5.   
  6. List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();  
  7. list.add(jedisShardInfo1);  
  8. list.add(jedisShardInfo2);  

 

 初始化ShardedJedisPool代替JedisPool:

 

Java代码  
  1. ShardedJedisPool pool = new ShardedJedisPool(config, list);  
[java] view plaincopy
 
  1. ShardedJedisPool pool = new ShardedJedisPool(config, list);  

 

 改由ShardedJedis,获取Jedis对象:

 

Java代码  
  1. // 从池中获取一个Jedis对象  
  2. ShardedJedis jedis = pool.getResource();  
  3. String keys = "name";  
  4. String value = "snowolf";  
  5. // 删数据  
  6. jedis.del(keys);  
  7. // 存数据  
  8. jedis.set(keys, value);  
  9. // 取数据  
  10. String v = jedis.get(keys);  
  11.   
  12. System.out.println(v);  
  13.   
  14. // 释放对象池  
  15. pool.returnResource(jedis);  
[java] view plaincopy
 
  1. // 从池中获取一个Jedis对象  
  2. ShardedJedis jedis = pool.getResource();  
  3. String keys = "name";  
  4. String value = "snowolf";  
  5. // 删数据  
  6. jedis.del(keys);  
  7. // 存数据  
  8. jedis.set(keys, value);  
  9. // 取数据  
  10. String v = jedis.get(keys);  
  11.   
  12. System.out.println(v);  
  13.   
  14. // 释放对象池  
  15. pool.returnResource(jedis);  

 

 

 使用commons-pool连接池

  1. /* 
  2.  * JedisPoolTest.java 
  3.  */  
  4. package com.x.java2000_wl;  
  5.   
  6. import java.util.ResourceBundle;  
  7.   
  8. import org.junit.Assert;  
  9. import org.junit.BeforeClass;  
  10. import org.junit.Test;  
  11.   
  12. import redis.clients.jedis.Jedis;  
  13. import redis.clients.jedis.JedisPool;  
  14. import redis.clients.jedis.JedisPoolConfig;  
  15.   
  16. /** 
  17.  * jedis Pool 操作 
  18.  * @author http://blog.csdn.net/java2000_wl 
  19.  * @version <b>1.0</b> 
  20.  */  
  21. public class JedisPoolTest {  
  22.   
  23.     private static JedisPool jedisPool;  
  24.       
  25.     /** 
  26.      * initPoolConfig 
  27.      * <br>------------------------------<br> 
  28.      * @return 
  29.      */  
  30.     private static JedisPoolConfig initPoolConfig() {  
  31.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();  
  32.         // 控制一个pool最多有多少个状态为idle的jedis实例  
  33.         jedisPoolConfig.setMaxActive(1000);   
  34.         // 最大能够保持空闲状态的对象数  
  35.         jedisPoolConfig.setMaxIdle(300);  
  36.         // 超时时间  
  37.         jedisPoolConfig.setMaxWait(1000);  
  38.         // 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;  
  39.         jedisPoolConfig.setTestOnBorrow(true);   
  40.         // 在还会给pool时,是否提前进行validate操作  
  41.         jedisPoolConfig.setTestOnReturn(true);  
  42.         return jedisPoolConfig;  
  43.     }  
  44.       
  45.     /** 
  46.      * 初始化jedis连接池 
  47.      * <br>------------------------------<br> 
  48.      */  
  49.     @BeforeClass  
  50.     public static void before() {  
  51.         JedisPoolConfig jedisPoolConfig = initPoolConfig();    
  52.         // 属性文件读取参数信息  
  53.         ResourceBundle bundle = ResourceBundle.getBundle("redis_config");  
  54.         String host = bundle.getString("redis.host");  
  55.         int port = Integer.valueOf(bundle.getString("redis.port"));  
  56.         int timeout = Integer.valueOf(bundle.getString("redis.timeout"));  
  57.         String password = bundle.getString("redis.password");  
  58.         // 构造连接池  
  59.         jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);  
  60.     }  
  61.   
  62.     @Test  
  63.     public void testSet() {  
  64.         Jedis jedis = null;   
  65.         // 从池中获取一个jedis实例  
  66.         try {  
  67.             jedis = jedisPool.getResource();  
  68.             jedis.set("blog_pool""java2000_wl");  
  69.         } catch (Exception e) {  
  70.             // 销毁对象  
  71.             jedisPool.returnBrokenResource(jedis);  
  72.             Assert.fail(e.getMessage());  
  73.         } finally {  
  74.             // 还会到连接池  
  75.             jedisPool.returnResource(jedis);  
  76.         }  
  77.     }         
  78.       
  79.     @Test  
  80.     public void testGet() {  
  81.         Jedis jedis = null;   
  82.         try {  
  83.             // 从池中获取一个jedis实例  
  84.             jedis = jedisPool.getResource();  
  85.             System.out.println(jedis.get("blog_pool"));  
  86.         } catch (Exception e) {  
  87.             // 销毁对象  
  88.             jedisPool.returnBrokenResource(jedis);  
  89.             Assert.fail(e.getMessage());  
  90.         } finally {  
  91.             // 还会到连接池  
  92.             jedisPool.returnResource(jedis);  
  93.         }  
  94.     }  
  95. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics