spring集成redisCluster - Go语言中文社区

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. 项目结构

    这里写图片描述
  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" >
       
        <!-- xml方式配置cluster-->    
           < 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 >

           
    <!-- Spring Data Redis 设置 -->
        <!-- redis 序列化策略 ,通常情况下key值采用String序列化策略, -->
        <!-- 如果不指定序列化策略,StringRedisTemplate的key和value都将采用String序列化策略; -->
        < bean id = "stringRedisSerializer"
              class = "org.springframework.data.redis.serializer.StringRedisSerializer" />
        < bean id = "jdkRedisSerializer"
              class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

        <!-- redis template definition p表示对该bean里面的属性进行注入,格式为p:属性名=注入的对象 效果与在bean里面使用<property>标签一样  -->  
        < 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:hashValueSeria
    版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
    原文链接:https://blog.csdn.net/u014196729/article/details/51221614
    站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
    • 发表于 2021-06-12 20:40:19
    • 阅读 ( 1705 )
    • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢