spring-data-redis与Jedis整合使用

1.spring-data-redis与Jedis简单整合

spring-data-redis与Jedis简单整合,Redis没有任何集群只是单节点工作,使用连接池
1.创建spring-context-jedis.xml配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/mvc
  8. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.0.xsd"
  13. default-lazy-init="false">
  14. <!-- 连接池配置. -->
  15. <beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
  16. <!-- 连接池中最大连接数。高版本:maxTotal,低版本:maxActive -->
  17. <propertyname="maxTotal"value="8"/>
  18. <!-- 连接池中最大空闲的连接数. -->
  19. <propertyname="maxIdle"value="4"/>
  20. <!-- 连接池中最少空闲的连接数. -->
  21. <propertyname="minIdle"value="1"/>
  22. <!-- 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时。高版本:maxWaitMillis,低版本:maxWait -->
  23. <propertyname="maxWaitMillis"value="5000"/>
  24. <!-- 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除. -->
  25. <propertyname="minEvictableIdleTimeMillis"value="300000"/>
  26. <!-- 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3 -->
  27. <propertyname="numTestsPerEvictionRun"value="3"/>
  28. <!-- “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1. -->
  29. <propertyname="timeBetweenEvictionRunsMillis"value="60000"/>
  30. <!-- testOnBorrow:向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值. -->
  31. <!-- testOnReturn:向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.-->
  32. <!-- testWhileIdle:向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值. -->
  33. <!-- whenExhaustedAction:当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1(0:抛出异常。1:阻塞,直到有可用链接资源。2:强制创建新的链接资源) -->
  34. </bean>
  35. <!-- Spring提供的Redis连接工厂 -->
  36. <beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"destroy-method="destroy">
  37. <!-- 连接池配置. -->
  38. <propertyname="poolConfig"ref="jedisPoolConfig"/>
  39. <!-- Redis服务主机. -->
  40. <propertyname="hostName"value="192.168.110.101"/>
  41. <!-- Redis服务端口号. -->
  42. <propertyname="port"value="6379"/>
  43. <!-- Redis服务连接密码. -->
  44. <!-- <property name="password" value="${redis.password}" /> -->
  45. <!-- 连超时设置. -->
  46. <propertyname="timeout"value="15000"/>
  47. <!-- 是否使用连接池. -->
  48. <propertyname="usePool"value="true"/>
  49. </bean>
  50. <!-- Spring提供的访问Redis类. -->
  51. <beanid="jedisTemplate"class="org.springframework.data.redis.core.RedisTemplate">
  52. <propertyname="connectionFactory"ref="jedisConnectionFactory"/>
  53. <propertyname="keySerializer">
  54. <beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  55. </property>
  56. <propertyname="valueSerializer">
  57. <!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
  58. <beanclass="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
  59. </property>
  60. </bean>
  61. </beans>
2.使用Spring提供的RedisTemplate类
  1. publicstaticvoid main(String[] args)
  2. {
  3. ClassPathXmlApplicationContext applicationContext =newClassPathXmlApplicationContext("spring-context-jedis.xml");
  4. // 获取Spring提供的RedisTemplate类此类封装了Jedis,简化操作
  5. RedisTemplate<String,List<String>> redisTemplate = applicationContext.getBean("jedisTemplate",RedisTemplate.class);
  6. // Spring 提供的各种Redis结构的key-value操作类
  7. ValueOperations<String,List<String>> value = redisTemplate.opsForValue();
  8. HashOperations<String,Object,Object> hash = redisTemplate.opsForHash();
  9. ListOperations<String,List<String>> list = redisTemplate.opsForList();
  10. HyperLogLogOperations<String,List<String>> hyperLogLog = redisTemplate.opsForHyperLogLog();
  11. SetOperations<String,List<String>> set = redisTemplate.opsForSet();
  12. ZSetOperations<String,List<String>> zSet = redisTemplate.opsForZSet();
  13. List<String> listValue =newArrayList<String>();
  14. listValue.add("001");
  15. listValue.add("002");
  16. value.set("list", listValue);
  17. System.out.println(value.get("list"));
  18. // 关闭Spring容器释放资源
  19. applicationContext.close();
  20. }
3.关于RedisTemplate类源码学习
RedisTemplate类的属性
  1. privateboolean enableTransactionSupport =false;
  2. privateboolean exposeConnection =false;
  3. privateboolean initialized =false;
  4. privateboolean enableDefaultSerializer =true;
  5. // 默认的序列化实现
  6. privateRedisSerializer<?> defaultSerializer =newJdkSerializationRedisSerializer();
  7. // 各种操作的序列化方式定义
  8. privateRedisSerializer keySerializer =null;
  9. privateRedisSerializer valueSerializer =null;
  10. privateRedisSerializer hashKeySerializer =null;
  11. privateRedisSerializer hashValueSerializer =null;
  12. privateRedisSerializer<String> stringSerializer =newStringRedisSerializer();
  13. privateScriptExecutor<K> scriptExecutor;
  14. // Spring 提供的各种Redis结构的key-value操作类
  15. // cache singleton objects (where possible)
  16. privateValueOperations<K, V> valueOps;
  17. privateListOperations<K, V> listOps;
  18. privateSetOperations<K, V> setOps;
  19. privateZSetOperations<K, V> zSetOps;
  20. privateHyperLogLogOperations<K, V> hllOps;
在一个应用中RedisTemplate类对象可以是单例的,因为其属性“ valueOpslistOpssetOpszSetOpshllOps ”的各种操作也是线程安全的,源码如下:
获取其 valueOps listOps setOps zSetOps hllOps 属性:
  1. publicValueOperations<K, V> opsForValue()
  2. {
  3. if(valueOps ==null)
  4. {
  5. valueOps =newDefaultValueOperations<K, V>(this);
  6. }
  7. return valueOps;
  8. }
  9. publicListOperations<K, V> opsForList()
  10. {
  11. if(listOps ==null)
  12. {
  13. listOps =newDefaultListOperations<K, V>(this);
  14. }
  15. return listOps;
  16. }
  17. // 省略部分......
其属性“ valueOps listOps setOps zSetOps hllOps ”的各种操作也是线程安全的,例如 valueOps 属性对象默认实现类 DefaultValueOperations < K , V > 源码:
  1. publicvoid set(K key, V value)
  2. {
  3. finalbyte[] rawValue = rawValue(value);
  4. execute(newValueDeserializingRedisCallback(key)
  5. {
  6. protectedbyte[] inRedis(byte[] rawKey,RedisConnection connection)
  7. {
  8. connection.set(rawKey, rawValue);
  9. returnnull;
  10. }
  11. },true);
  12. }
  13. // 省略其他操作......
再看看 execute 的实现如下:
  1. //org.springframework.data.redis.core.AbstractOperations<K, V>
  2. <T> T execute(RedisCallback<T> callback,boolean b){
  3. return template.execute(callback, b);
  4. }
  5. // template.execute实现如下:
  6. // org.springframework.data.redis.core.RedisTemplate<K, V>
  7. public<T> T execute(RedisCallback<T> action,boolean exposeConnection){
  8. return execute(action, exposeConnection,false);
  9. }
  10. // execute实现如下:
  11. // org.springframework.data.redis.core.RedisTemplate<K, V> --- 最终实现
  12. public<T> T execute(RedisCallback<T> action,boolean exposeConnection,boolean pipeline){
  13. Assert.isTrue(initialized,"template not initialized; call afterPropertiesSet() before using it");
  14. Assert.notNull(action,"Callback object must not be null");
  15. RedisConnectionFactory factory = getConnectionFactory();
  16. RedisConnection conn =null;
  17. try{
  18. if(enableTransactionSupport){
  19. // only bind resources in case of potential transaction synchronization
  20. conn =RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
  21. }else{
  22. conn =RedisConnectionUtils.getConnection(factory);
  23. }
  24. boolean existingConnection =TransactionSynchronizationManager.hasResource(factory);
  25. RedisConnection connToUse = preProcessConnection(conn, existingConnection);
  26. boolean pipelineStatus = connToUse.isPipelined();
  27. if(pipeline &&!pipelineStatus){
  28. connToUse.openPipeline();
  29. }
  30. RedisConnection connToExpose =(exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
  31. T result = action.doInRedis(connToExpose);
  32. // close pipeline
  33. if(pipeline &&!pipelineStatus){
  34. connToUse.closePipeline();
  35. }
  36. // TODO: any other connection processing?
  37. return postProcessResult(result, connToUse, existingConnection);
  38. }finally{
  39. if(!enableTransactionSupport){
  40. RedisConnectionUtils.releaseConnection(conn, factory);
  41. }
  42. }
  43. }
此时已经可以看出每次操作都会创建一个新的 RedisConnection 对象使用完成会调用 RedisConnectionUtils . releaseConnection ( conn , factory ) 方法释放连接,若想查看其创建 RedisConnection 连接和 RedisConnectionUtils . releaseConnection ( conn , factory ) 释放连接过程可继续查看其源码,此处就不赘述了。

2.JedisConnectionFactory中使用sentinel集群

1.在 spring-context-jedis.xml 中配置sentinel信息
  1. <!-- Redis sentinel集群配置 -->
  2. <beanid="sentinelConfig"class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  3. <constructor-argindex="0"type="java.lang.String"value="master001"/>
  4. <constructor-argindex="1"type="java.util.Set">
  5. <set>
  6. <value>192.168.110.100:26379</value>
  7. <value>192.168.110.100:36379</value>
  8. <value>192.168.110.100:46379</value>
  9. </set>
  10. </constructor-arg>
  11. </bean>
  12. <!-- Spring提供的Redis连接工厂 -->
  13. <beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"destroy-method="destroy">
  14. <!-- Redis sentinel集群配置 -->
  15. <constructor-argindex="0"type="org.springframework.data.redis.connection.RedisSentinelConfiguration"ref="sentinelConfig"/>
  16. <!-- 连接池配置. -->
  17. <constructor-argindex="1"type="redis.clients.jedis.JedisPoolConfig"ref="jedisPoolConfig"/>
  18. <!-- Redis服务主机. -->
  19. <propertyname="hostName"value="192.168.110.101"/>
  20. <!-- Redis服务端口号. -->
  21. <propertyname="port"value="6379"/>
  22. <!-- Redis服务连接密码. -->
  23. <!-- <property name="password" value="${redis.password}" /> -->
  24. <!-- 连超时设置. -->
  25. <propertyname="timeout"value="15000"/>
  26. <!-- 是否使用连接池. -->
  27. <propertyname="usePool"value="true"/>
  28. </bean>
2.使用测试代码
  1. publicstaticvoid main(String[] args)
  2. {
  3. ClassPathXmlApplicationContext applicationContext =newClassPathXmlApplicationContext("spring-context-jedis.xml");
  4. // 获取Spring提供的RedisTemplate类此类封装了Jedis,简化操作
  5. RedisTemplate<String,String> redisTemplate = applicationContext.getBean("jedisTemplate",RedisTemplate.class);
  6. ValueOperations<String,String> value = redisTemplate.opsForValue();
  7. value.set("K001","V001");
  8. System.out.println(value.get("K001"));
  9. // 关闭Redis Master服务
  10. Scanner scanner =newScanner(System.in);
  11. String input = scanner.nextLine();
  12. System.out.println(input);
  13. value.set("K002","V002");
  14. System.out.println(value.get("K002"));
  15. // 关闭Spring容器释放资源
  16. applicationContext.close();
  17. }
代码输出,注意中间的打印:
  1. 2015-10-315:10:59 redis.clients.jedis.JedisSentinelPool initSentinels
  2. 信息:Trying to find master from available Sentinels...
  3. 2015-10-315:10:59 redis.clients.jedis.JedisSentinelPool initSentinels
  4. 信息:Redis master running at 192.168.110.101:6379, starting Sentinel listeners...
  5. 2015-10-315:10:59 redis.clients.jedis.JedisSentinelPool initPool
  6. 信息:CreatedJedisPool to master at 192.168.110.101:6379
  7. V001
  8. 2015-10-315:11:38 redis.clients.jedis.JedisSentinelPool initPool
  9. 信息:CreatedJedisPool to master at 192.168.110.103:6379
  10. V002

3.JedisConnectionFactory中使用JedisShardInfo

Spring-Data-Redis好像并不支持Redis分片集群,但是JedisConnectionFactory源码中有一个JedisShardInfo属性,源码如下:
  1. privateJedisShardInfo shardInfo;
  2. // 省略......
  3. publicvoid afterPropertiesSet(){
  4. if(shardInfo ==null){
  5. shardInfo =newJedisShardInfo(hostName, port);
  6. if(StringUtils.hasLength(password)){
  7. shardInfo.setPassword(password);
  8. }
  9. if(timeout >0){
  10. setTimeoutOn(shardInfo, timeout);
  11. }
  12. }
  13. if(usePool){
  14. this.pool = createPool();
  15. }
  16. }
  17. // 省略......
  18. protectedJedis fetchJedisConnector(){
  19. try{
  20. if(usePool && pool !=null){
  21. return pool.getResource();
  22. }
  23. Jedis jedis =newJedis(getShardInfo());
  24. // force initialization (see Jedis issue #82)
  25. jedis.connect();
  26. return jedis;
  27. }catch(Exception ex){
  28. thrownewRedisConnectionFailureException("Cannot get Jedis connection", ex);
  29. }
  30. }

http://www.cnblogs.com/LiZhiW/p/4853486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值