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緩存器名稱的值,否則會報錯:未找到對應的緩存器名稱