天天看点

SSM整合Redis Cluster集群缓存SSM整合Redis Cluster集群缓存

SSM整合Redis Cluster集群缓存

1、pom引入依赖

<!--jedis连接redis集群非常简单,也可直接操作jedisCluster来增删改查数据库。-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

    <!-- spring-data-redis -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.1.RELEASE</version>
    </dependency>

           

2、spring-redis.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/cache
		http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- redis配置文件已经在spring-mybatis.xml中加载 -->
    <!-- Jedis配置 -->
    <bean id="jedisPoolConfig"
          class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空闲连接数 -->
        <property name="MaxIdle" value="10" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow"
                  value="true" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle"
                  value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted"
                  value="false" />
    </bean>

    <!-- 配置Cluster -->
    <bean id="redisClusterConfiguration"
          class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="maxRedirects" value="3"></property>
        <!-- 节点配置 -->
        <property name="clusterNodes">
            <set>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
                </bean>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host2}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
                </bean>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host3}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
                </bean>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host4}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
                </bean>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host5}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
                </bean>
                <bean
                        class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host6}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port6}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
    <!-- 配置jedis连接工厂 -->
    <bean id="jeidsConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="redisClusterConfiguration" />
        <constructor-arg ref="jedisPoolConfig" />
        <property name="password" value="123456"/>
    </bean>
    <!-- 配置RedisTemplate -->
    <bean id="redisTemplate"
          class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"
                  ref="jeidsConnectionFactory" />
        <!-- String -->
        <property name="keySerializer">
            <bean
                    class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                    class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <!-- hash -->
        <property name="hashKeySerializer">
            <bean
                    class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                    class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>

    <!-- 定义缓存管理器redisCacheManager。 注意:cache-manager默认值是cacheManager,你的缓存管理器id要是命名是cacheManager,这里可以省略
        1.使用注解驱动 -->
    <cache:annotation-driven
            cache-manager="redisCacheManager" />
    <!-- 2.定义缓存管理器 -->
    <bean id="redisCacheManager"
          class="org.springframework.data.redis.cache.RedisCacheManager">
        <!-- 通过构造方法注入redisTemplate -->
        <constructor-arg index="0" ref="redisTemplate" />
        <!-- 定义超时时间,单位秒 -->
        <property name="defaultExpiration" value="5000"></property>
        <!-- 设置缓存器名称 -->
        <property name="cacheNames">
            <list>
                <value>redisCacheManager</value>
            </list>
        </property>
    </bean>

</beans>

           

3、redis.properties配置

#redis
redis.pool.maxTotal=30
redis.pool.maxIdle=10
redis.pool.numTestsPerEvictionRun=1024
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.minEvictableIdleTimeMillis=1800000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.maxWaitMillis=1500
redis.pool.testOnBorrow=true
redis.pool.testWhileIdle=true
redis.pool.blockWhenExhausted=false
redis.maxRedirects=3
redis.host1=192.168.189.100
redis.port1=7001
redis.host2=192.168.189.100
redis.port2=7002
redis.host3=192.168.189.100
redis.port3=7003
redis.host4=192.168.189.100
redis.port4=7004
redis.host5=192.168.189.100
redis.port5=7005
redis.host6=192.168.189.100
redis.port6=7006

           

这时已经配置好了,可以使用@Cacheable,@Cacheput,@CacheEvict等注解。

注意:

@CachePut(value = “redisCacheManager”,key = “‘blog_’+#articleId”)

value 的值只可以是上面 redis 缓存管理器中 redis缓存器名称的值,否则会报错:未找到对应的缓存器名称