spring集成redisCluster

  1. 简介

    在spring框架的web项目中使用redisTemplate操作redis cluster。参考文档:http://www.cnblogs.com/moonandstar08/p/5149585.html
  2. 依赖版本

    spring版本:4.1.3.RELEASE。
    spring-data-redis版本:1.7.1.RELEASE。
    jedis版本:2.8.1。
  3. 项目结构

    spring集成redisCluster_第1张图片
  4. redis cluster搭建

    在centOS7环境下进行搭建,记得关防火墙。搭建步骤在官网上有说明,不再赘述。
    官方cluster文档
    http://www.redis.cn/topics/cluster-tutorial.html
    网上参考文档
    http://www.cnblogs.com/kreo/p/4399612.html
  5. spring配置

    • redis.properties

    redis.host1=192.168.139.31
    redis.port1=7000
    redis.password1=
    redis.host2=192.168.139.31
    redis.port2=7001
    redis.password2=
    redis.host3=192.168.139.31
    redis.port3=7002
    redis.password2=
    redis.host4=192.168.139.31
    redis.port4=7003
    redis.password4=
    redis.host5=192.168.139.31
    redis.port5=7004
    redis.password5=
    redis.host6=192.168.139.31
    redis.port6=7005
    redis.password6=
    redis.minIdle=1
    redis.maxIdle=300
    redis.maxActive=600
    redis.maxWait=1000
    redis.testOnBorrow=true
    spring.redis.cluster.max-redirects= 3
    #理论上应该是可以用这个配置的,但是没啥头绪,求指教。
    #spring.redis.cluster.nodes=

    • redis-cluster.xml

    xml version="1.0" encoding= "UTF-8"?>
    < beans xmlns = "http://www.springframework.org/schema/beans"
           xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context"
           xmlns:p = "http://www.springframework.org/schema/p"
           xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
       
           
           < bean id = "clusterRedisNodes1"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host1}" />
                 < constructor-arg value = "${redis.port1}" type = "int" />
           bean >
           < bean id = "clusterRedisNodes2"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host2}" />
                 < constructor-arg value = "${redis.port2}" type = "int" />
           bean >
           < bean id = "clusterRedisNodes3"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host3}" />
                 < constructor-arg value = "${redis.port3}" type = "int" />
           bean >
           < bean id = "clusterRedisNodes4"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host4}" />
                 < constructor-arg value = "${redis.port4}" type = "int" />
           bean >
           < bean id = "clusterRedisNodes5"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host5}" />
                 < constructor-arg value = "${redis.port5}" type = "int" />
           bean >
           < bean id = "clusterRedisNodes6"    class = "org.springframework.data.redis.connection.RedisNode" >
                 < constructor-arg value = "${redis.host6}" />
                 < constructor-arg value = "${redis.port6}" type = "int" />
           bean >
          
       < bean id = "redisClusterConfiguration" class = "org.springframework.data.redis.connection.RedisClusterConfiguration" >
                 < property name = "maxRedirects" value = "${spring.redis.cluster.max-redirects}" >
            property >
            < property name = "clusterNodes" >
                < set >
                    < ref bean = "clusterRedisNodes1" />
                    < ref bean = "clusterRedisNodes2" />
                    < ref bean = "clusterRedisNodes3" />
                    < ref bean = "clusterRedisNodes4" />
                    < ref bean = "clusterRedisNodes5" />
                    < ref bean = "clusterRedisNodes6" />
                set >
            property >
           bean >
           < bean id = "poolConfig" class = "redis.clients.jedis.JedisPoolConfig" >  
         < property name = "minIdle" value = "${redis.minIdle}" />  
           < property name = "maxIdle" value = "${redis.maxIdle}" />    
           < property name = "maxTotal" value = "${redis.maxActive}" />    
           < property name = "maxWaitMillis" value = "${redis.maxWait}" />    
           < property name = "testOnBorrow" value = "${redis.testOnBorrow}" />    
        bean >   
       < bean id = "jedisConnectionFactory"
             class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool = "true" >
             < constructor-arg index = "0" ref = "redisClusterConfiguration" />
             < constructor-arg index = "1" ref = "poolConfig" > constructor-arg >   
       bean >

           
       
       
        < bean id = "stringRedisSerializer"
              class = "org.springframework.data.redis.serializer.StringRedisSerializer" />
        < bean id = "jdkRedisSerializer"
              class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

         
        < bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate"
              p:connectionFactory-ref = "jedisConnectionFactory"
              p:keySerializer-ref = "stringRedisSerializer" p:hashKeySerializer-ref = "stringRedisSerializer"
              p:valueSerializer-ref = "jdkRedisSerializer" p:hashValueSerializer-ref = "jdkRedisSerializer" />
    beans >  

  1. 测试代码

    • Param .java,需要实现序列化接口。
    public class Param implements Serializable{
           /**
           * serialVersionUID
           */
           private static final long serialVersionUID = 7681750316987150888L;

           private Integer id ;

           private String name ;
           private String value ;
           private String description ;
    • service
    @Service
    @Transactional
    public class ParamServiceImpl implements ParamService{

           @Autowired
           private RedisTemplate redis ;
           @Autowired
           private ParamDao paramDao ;
           @Override
           public Param detail(Integer id ) throws Exception{
                ValueOperations valueopsSet = redis .opsForValue();
                Param param = paramDao .detail( id );
                 valueopsSet .set( id .toString(), param );//set键值对
                ValueOperations valueopsGet = redis .opsForValue();
                Object o = valueopsGet .get( id .toString());//get 键值对
                 return param ;
          }

你可能感兴趣的:(redis,java,redis,jedis,spring)