|
目前Redis大概有3中基于Java语言的Client:
一、简单使用Jedis
需要Jedis就从Maven获取吧!
Maven Pom.xml
Xml代码
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
[xml] view plaincopy
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
如果只是简单使用Jedis,以下这么几行代码足够:
Java代码
- Jedis jedis = new Jedis("10.11.20.140");
- String keys = "name";
-
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
-
- System.out.println(value);
[java] view plaincopy
- Jedis jedis = new Jedis("10.11.20.140");
- String keys = "name";
-
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
-
- System.out.println(value);
二、池化使用Jedis
Jedis使用commons-pool完成池化实现。
先做个配置文件:
Properties代码
- #最大分配的对象数
- redis.pool.maxActive=1024
- #最大能够保持idel状态的对象数
- redis.pool.maxIdle=200
- #当池内没有返回对象时,最大等待时间
- redis.pool.maxWait=1000
- #当调用borrow Object方法时,是否进行有效性检查
- redis.pool.testOnBorrow=true
- #当调用return Object方法时,是否进行有效性检查
- redis.pool.testOnReturn=true
- #IP
- redis.ip=10.11.20.140
- #Port
- redis.port=6379
在静态代码段中完成初始化:
Java代码
- private static JedisPool pool;
- static {
- ResourceBundle bundle = ResourceBundle.getBundle("redis");
- if (bundle == null) {
- throw new IllegalArgumentException(
- "[redis.properties] is not found!");
- }
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(Integer.valueOf(bundle
- .getString("redis.pool.maxActive")));
- config.setMaxIdle(Integer.valueOf(bundle
- .getString("redis.pool.maxIdle")));
- config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
- config.setTestOnBorrow(Boolean.valueOf(bundle
- .getString("redis.pool.testOnBorrow")));
- config.setTestOnReturn(Boolean.valueOf(bundle
- .getString("redis.pool.testOnReturn")));
- pool = new JedisPool(config, bundle.getString("redis.ip"),
- Integer.valueOf(bundle.getString("redis.port")));
- }
[java] view plaincopy
- private static JedisPool pool;
- static {
- ResourceBundle bundle = ResourceBundle.getBundle("redis");
- if (bundle == null) {
- throw new IllegalArgumentException(
- "[redis.properties] is not found!");
- }
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(Integer.valueOf(bundle
- .getString("redis.pool.maxActive")));
- config.setMaxIdle(Integer.valueOf(bundle
- .getString("redis.pool.maxIdle")));
- config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
- config.setTestOnBorrow(Boolean.valueOf(bundle
- .getString("redis.pool.testOnBorrow")));
- config.setTestOnReturn(Boolean.valueOf(bundle
- .getString("redis.pool.testOnReturn")));
- pool = new JedisPool(config, bundle.getString("redis.ip"),
- Integer.valueOf(bundle.getString("redis.port")));
- }
然后,修改前面那段jedis操作Redis
Java代码
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- String keys = "name";
-
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
-
- System.out.println(value);
-
- // 释放对象池
- pool.returnResource(jedis);
[java] view plaincopy
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- String keys = "name";
-
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
-
- System.out.println(value);
-
- // 释放对象池
- pool.returnResource(jedis);
改为从对象池中,获取Jedis实例:
Java代码
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
[java] view plaincopy
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
切记,最后使用后,释放Jedis对象:
Java代码
- // 释放对象池
- pool.returnResource(jedis);
[java] view plaincopy
- // 释放对象池
- pool.returnResource(jedis);
三、一致性哈希
Memcached完全基于分布式集群,而Redis是Master-Slave,如果想把Reids,做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具,完成一致性哈希。
PS:Memcached是在Server端完成Sharding,Redis只能依靠各个Client做Sharding。可能会在Redis 3.0系列支持Server端Sharding。
保留前面的JedisPoolConfig,新增两个Redis的IP(redis1.ip,redis2.ip),完成两个JedisShardInfo实例,并将其丢进List中:
Java代码
- JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
- bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
- JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
- bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
-
- List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
- list.add(jedisShardInfo1);
- list.add(jedisShardInfo2);
[java] view plaincopy
- JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
- bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
- JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
- bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
-
- List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
- list.add(jedisShardInfo1);
- list.add(jedisShardInfo2);
初始化ShardedJedisPool代替JedisPool:
Java代码
- ShardedJedisPool pool = new ShardedJedisPool(config, list);
[java] view plaincopy
- ShardedJedisPool pool = new ShardedJedisPool(config, list);
改由ShardedJedis,获取Jedis对象:
Java代码
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
-
- System.out.println(v);
-
- // 释放对象池
- pool.returnResource(jedis);
[java] view plaincopy
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
-
- System.out.println(v);
-
- // 释放对象池
- pool.returnResource(jedis);
使用commons-pool连接池
[java] view plaincopyprint?
- /*
- * JedisPoolTest.java
- */
- package com.x.java2000_wl;
-
- import java.util.ResourceBundle;
-
- import org.junit.Assert;
- import org.junit.BeforeClass;
- import org.junit.Test;
-
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- /**
- * jedis Pool 操作
- * @author http://blog.csdn.net/java2000_wl
- * @version <b>1.0</b>
- */
- public class JedisPoolTest {
-
- private static JedisPool jedisPool;
-
- /**
- * initPoolConfig
- * <br>------------------------------<br>
- * @return
- */
- private static JedisPoolConfig initPoolConfig() {
- JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
- // 控制一个pool最多有多少个状态为idle的jedis实例
- jedisPoolConfig.setMaxActive(1000);
- // 最大能够保持空闲状态的对象数
- jedisPoolConfig.setMaxIdle(300);
- // 超时时间
- jedisPoolConfig.setMaxWait(1000);
- // 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
- jedisPoolConfig.setTestOnBorrow(true);
- // 在还会给pool时,是否提前进行validate操作
- jedisPoolConfig.setTestOnReturn(true);
- return jedisPoolConfig;
- }
-
- /**
- * 初始化jedis连接池
- * <br>------------------------------<br>
- */
- @BeforeClass
- public static void before() {
- JedisPoolConfig jedisPoolConfig = initPoolConfig();
- // 属性文件读取参数信息
- ResourceBundle bundle = ResourceBundle.getBundle("redis_config");
- String host = bundle.getString("redis.host");
- int port = Integer.valueOf(bundle.getString("redis.port"));
- int timeout = Integer.valueOf(bundle.getString("redis.timeout"));
- String password = bundle.getString("redis.password");
- // 构造连接池
- jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
- }
-
- @Test
- public void testSet() {
- Jedis jedis = null;
- // 从池中获取一个jedis实例
- try {
- jedis = jedisPool.getResource();
- jedis.set("blog_pool", "java2000_wl");
- } catch (Exception e) {
- // 销毁对象
- jedisPool.returnBrokenResource(jedis);
- Assert.fail(e.getMessage());
- } finally {
- // 还会到连接池
- jedisPool.returnResource(jedis);
- }
- }
-
- @Test
- public void testGet() {
- Jedis jedis = null;
- try {
- // 从池中获取一个jedis实例
- jedis = jedisPool.getResource();
- System.out.println(jedis.get("blog_pool"));
- } catch (Exception e) {
- // 销毁对象
- jedisPool.returnBrokenResource(jedis);
- Assert.fail(e.getMessage());
- } finally {
- // 还会到连接池
- jedisPool.returnResource(jedis);
- }
- }
- }
|
|
|