http://blog.csdn.net/it_man/article/details/9730605
2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报
用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET
等。基于这些限制,有必要考虑Redis!
相关链接:
Redis实战之征服 Redis + Jedis + Spring (一)
Redis实战之征服 Redis + Jedis + Spring (二)
Redis实战之征服 Redis + Jedis + Spring (三)
言归正传,目前Redis大概有3中基于Java语言的Client:
- Jredis
- Jedis
- Redis4J
这里只说Jedis,因为它是官方提供的唯一Redis Client For Java Provider!
一、简单使用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);
四、Spring封装参考
Ok,完成上述代码足够完成简单任务,如果有必要,可以用Spring封装初始化:
Xml代码
- <context:property-placeholder location="classpath:redis.properties" />
- <bean
- id="jedisPoolConfig"
- class="redis.clients.jedis.JedisPoolConfig"
- >
- <property
- name="maxActive"
- value="${redis.pool.maxActive}" />
- <property
- name="maxIdle"
- value="${redis.pool.maxIdle}" />
- <property
- name="maxWait"
- value="${redis.pool.maxWait}" />
- <property
- name="testOnBorrow"
- value="${redis.pool.testOnBorrow}" />
- </bean>
- <bean
- id="shardedJedisPool"
- class="redis.clients.jedis.ShardedJedisPool"
- >
- <constructor-arg
- index="0"
- ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis1.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis2.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- </list>
- </constructor-arg>
- </bean>
[xml] view plaincopy
- <context:property-placeholder location="classpath:redis.properties" />
- <bean
- id="jedisPoolConfig"
- class="redis.clients.jedis.JedisPoolConfig"
- >
- <property
- name="maxActive"
- value="${redis.pool.maxActive}" />
- <property
- name="maxIdle"
- value="${redis.pool.maxIdle}" />
- <property
- name="maxWait"
- value="${redis.pool.maxWait}" />
- <property
- name="testOnBorrow"
- value="${redis.pool.testOnBorrow}" />
- </bean>
- <bean
- id="shardedJedisPool"
- class="redis.clients.jedis.ShardedJedisPool"
- >
- <constructor-arg
- index="0"
- ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis1.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg
- index="0"
- value="${redis2.ip}" />
- <constructor-arg
- index="1"
- value="${redis.port}"
- type="int" />
- </bean>
- </list>
- </constructor-arg>
- </bean>
代码可以更简洁一些:
Java代码
- 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 = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
- System.out.println(v);
- // 释放对象池
- pool.returnResource(jedis);
- assertEquals(value, v);
- }
[java] view plaincopy
- 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 = "snowolf";
- // 删数据
- 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,以后有机会再深入研究。
相关链接:
Redis实战之征服 Redis + Jedis + Spring (一)
Redis实战之征服 Redis + Jedis + Spring (二)
Redis实战之征服 Redis + Jedis + Spring (三)