今天再学习一下Redis的池化操作jedis,接着上一篇文章 Redis教程 —— Java操作Redis增删改查(基础操作)
我是用的是jedis2.9版本,池化操作依赖与common-pool2.jar包
一、池化使用Jedis
在classpath目录下创建一个config文件夹,文件夹中创建一个redis.properties配置文件redis.host=127.0.0.1
redis.port=6379
redis.pass=my_redis
redis.maxIdle=200
redis.maxTotal=512
redis.maxWaitMillis=3000
redis.testOnBorrow=true
在静态代码段中完成初始化,并进行基本操作:
运行之前现开启Redis服务redis.exeimport java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 池化使用Jedis
* @author X-rapido
*/
public class Demo {
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("config/redis");
if (bundle == null) {
throw new IllegalArgumentException("[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.maxIdle")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.testOnBorrow")));
pool = new JedisPool(config, bundle.getString("redis.host"), Integer.valueOf(bundle.getString("redis.port")));
}
public static void main(String[] args) {
// 从池中获取一个Jedis对象
Jedis jedis = pool.getResource();
jedis.del("name"); // 删数据
jedis.set("name", "程序喵ibloger.net"); // 存数据
String value = jedis.get("name"); // 取数据
System.out.println(value);
// 释放对象池
pool.returnResource(jedis);
}
}
运行结果
程序喵ibloger.net
这种操作一般用在非集群情况下,如果集群环境该如何操作呢,看下面,一致性哈希
一致性哈希
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中:
初始化ShardedJedisPool代替JedisPool:import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* 分布式集群池化使用Jedis
*
* @author X-rapido
*/
public class ClusterDemo {
private static ShardedJedisPool pool ;
static {
ResourceBundle bundle = ResourceBundle.getBundle("config/redis");
if (bundle == null) {
throw new IllegalArgumentException("[redis.properties] is not found!");
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.maxIdle")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.testOnBorrow")));
// 分布式集群,一致性hash配置示例 state
JedisShardInfo jedisShardInfo1 = new JedisShardInfo(bundle.getString("redis.host"),Integer.valueOf(bundle.getString("redis.port")));
// JedisShardInfo jedisShardInfo2 = new JedisShardInfo(bundle.getString("redis2.ip"),Integer.valueOf(bundle.getString("redis2.port")));
List list = new LinkedList();
list.add(jedisShardInfo1);
// list.add(jedisShardInfo2);
pool = new ShardedJedisPool(config, list);
}
public static void main(String[] args) {
// 从池中获取一个Jedis对象
ShardedJedis jedis = pool.getResource();
jedis.del("name"); // 删数据
jedis.set("name", "程序喵ibloger.net"); // 存数据
String value = jedis.get("name"); // 取数据
System.out.println(value);
// 释放对象池
pool.returnResource(jedis);
}
}
运行结果
程序喵ibloger.net
上面代码,我在本机测试就注释了另个jedisShardInfo2 ,根据自身需求增减
Spring封装参考
Ok,完成上述代码足够完成简单任务,如果有必要,可以用Spring封装初始化:
代码可以更简洁一些:private ApplicationContext app;
private ShardedJedisPool pool;
@Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.xml");
pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
}
@Test
public void test() {
// 从池中获取一个Jedis对象
ShardedJedis jedis = pool.getResource();
String keys = "name";
String value = "程序喵";
jedis.del(keys); // 删数据
jedis.set(keys, value); // 存数据
String v = jedis.get(keys); // 取数据
System.out.println(v);
// 释放对象池
pool.returnResource(jedis);
assertEquals(value, v);
}
当然,Spring提供了对于Redis的专门支持:spring-data-redis,使用关键字“spring-data-redis”站内搜索查看文章
参考:http://snowolf.iteye.com/blog/1633196