spring data redis 操作redis 单机版和集群

maven 配置

             

  1.   <redis.clients.version>2.9.0</redis.clients.version>  
  2. <spring.data.redis.version>1.7.2.RELEASE</spring.data.redis.version>  
  3. <dependency>  
  4. <groupId>org.springframework.data</groupId>  
  5. <artifactId>spring-data-redis</artifactId>  
  6. <version>${spring.data.redis.version}</version>  
  7. </dependency>  
  8. <dependency>  
  9. <groupId>redis.clients</groupId>  
  10. <artifactId>jedis</artifactId>  
  11. <version>${redis.clients.version}</version>  
  12. </dependency>  


spring-context-redis-single.xml 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4. xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  6. http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  7.   
  8. <description>Jedis Single Configuration 单机</description>  
  9. <context:annotation-config />  
  10. <!-- 加载配置属性文件  按需加载-->  
  11. <context:property-placeholder ignore-unresolvable="true" location="classpath*:redis-single.properties" />  
  12. <!-- Jedis 连接池配置 -->  
  13. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  14. <property name="maxIdle" value="${redis.maxIdle}" />   
  15. <property name="maxTotal" value="${redis.maxTotal}" />   
  16. </bean>  
  17. <!-- Jedis ConnectionFactory 数据库连接配置 -->  
  18. <bean id="jedisConnectionFactory"  
  19. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  20. <property name="hostName" value="${redis.host}" />  
  21. <property name="port" value="${redis.port}" />  
  22. <property name="poolConfig" ref="jedisPoolConfig" />  
  23. </bean>  
  24. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  25. <property name="connectionFactory" ref="jedisConnectionFactory" />  
  26. </bean>  
  27. </beans>  


redis-single.properties

#single configuration
redis.host=127.0.0.1
redis.port=6379
redis.maxIdle=100
redis.maxTotal=600

这样就是对单机redis的操作


--------------------------------------------------------华丽的分割线 下面集群的操作 主要是jedisConnectionFactory的提供方式不同--------------------------------------------------

spring-context-jedis-cluster.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4. xsi:schemaLocation="  
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  6. http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  7.   
  8.   
  9. <description>Jedis Cluster Configuration集群</description>  
  10. <!-- 加载配置属性文件 按需加载 -->  
  11. <context:property-placeholder ignore-unresolvable="true" location="classpath:redis-cluster.properties" />  
  12. <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">  
  13. <property name="maxRedirects" value="${redis.maxRedirects}"></property>  
  14. <property name="clusterNodes">  
  15. <set>  
  16. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  17. <constructor-arg name="host" value="${redis.host1}"></constructor-arg>  
  18. <constructor-arg name="port" value="${redis.port1}"></constructor-arg>  
  19. </bean>  
  20. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  21. <constructor-arg name="host" value="${redis.host2}"></constructor-arg>  
  22. <constructor-arg name="port" value="${redis.port2}"></constructor-arg>  
  23. </bean>  
  24. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  25. <constructor-arg name="host" value="${redis.host3}"></constructor-arg>  
  26. <constructor-arg name="port" value="${redis.port3}"></constructor-arg>  
  27. </bean>  
  28. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  29. <constructor-arg name="host" value="${redis.host4}"></constructor-arg>  
  30. <constructor-arg name="port" value="${redis.port4}"></constructor-arg>  
  31. </bean>  
  32. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  33. <constructor-arg name="host" value="${redis.host5}"></constructor-arg>  
  34. <constructor-arg name="port" value="${redis.port5}"></constructor-arg>  
  35. </bean>  
  36. <bean class="org.springframework.data.redis.connection.RedisClusterNode">  
  37. <constructor-arg name="host" value="${redis.host6}"></constructor-arg>  
  38. <constructor-arg name="port" value="${redis.port6}"></constructor-arg>  
  39. </bean>  
  40. </set>  
  41. </property>  
  42. </bean>  
  43. <bean id="jedisPoolConfig"   class="redis.clients.jedis.JedisPoolConfig">  
  44.         <property name="maxIdle" value="${redis.maxIdle}" />   
  45. <property name="maxTotal" value="${redis.maxTotal}" />   
  46.    </bean>  
  47. <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  >  
  48. <constructor-arg ref="redisClusterConfiguration" />  
  49. <constructor-arg ref="jedisPoolConfig" />  
  50. </bean>  
  51.   
  52. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  53. <property name="connectionFactory" ref="jeidsConnectionFactory" />  
  54. </bean>  
  55. </beans> 

redis-cluster.properties

#cluster configuration
redis.host1=xx.xx.xx.xx
redis.port1=7000


redis.host2=xx.xx.xx.xx
redis.port2=7001


redis.host3=xx.xx.xx.xx
redis.port3=7002


redis.host4=xx.xx.xx.xx
redis.port4=7000


redis.host5=xx.xx.xx.xx
redis.port5=7001


redis.host6=xx.xx.xx.xx
redis.port6=7002
redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600

redisTemplate 操作redis 非常便捷,对于set list hash  事务封装的特别完善。

如果想扩展 可以采用这种方式。附本人github : https://github.com/734839030/redis-client-core 对redis源码的学习例子。

 RedisTemplate redisTemplate = (RedisTemplate) factory.getBean("redisTemplate");

redisTemplate.execute(new RedisCallback() {
public Object doInRedis(RedisConnection connection){
              connection.set("key7".getBytes(),(System.currentTimeMillis()+"").getBytes());
              System.err.println("11");
              return 1L;
          }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值