您的位置:首页 > 数据库 > Redis

redis 集群配置

2017-12-27 15:10 369 查看
spring 和redis的配置

参照 http://blog.csdn.net/u013132051/article/details/53765344 文章配置成功

记录一下:

1、jar包的升级 

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId
<version>2.9.0</version>
</dependency>

2、spring-core 版本 3.3release以上

3、详细配置文件

<?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:cache="http://www.springframework.org/schema/cache" xmlns:int-redis="http://www.springframework.org/schema/integration/redis"
xmlns:redis="http://www.springframework.org/schema/redis" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/integration/redis http://www.springframework.org/schema/integration/redis/spring-integration-redis-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- jedis pool config -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10" />
<property name="maxIdle" value="10" />
<property name="maxWaitMillis" value="10" />
<property name="testOnBorrow" value="false" />
</bean>

<!--1.72 以上支持集群 集群配置 -->
<!-- redisCluster 配置 -->
<bean id="redisClusterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="3"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.0.4.8"></constructor-arg>
<constructor-arg name="port" value="7000"></constructor-arg>
</bean>

<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.0.4.8"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="10.0.4.8"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
</set>
</property>
</bean>

<!-- ReDis连接工厂 -->
<bean id="redis4CacheConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="redisClusterConfig" />
<property name="timeout" value="1000" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>

<!-- 存储序列化 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- <property name="connectionFactory" ref="redisConnectionFactory" /> -->
<property name="connectionFactory" ref="redis4CacheConnectionFactory" />
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>

<!-- turn on declarative caching -->
<cache:annotation-driven />
<!-- declare Redis Cache Manager -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager" c:redis-operations-ref="redisTemplate">
<property name="expires" ref="cacheExpires" />
</bean>
<!-- Sets the expire time (in seconds) for cache regions (by key). -->
<util:map id="cacheExpires">
<entry key="deliveryZone" value="86400" />
<entry key="deliveryCenter" value="86400" />
<entry key="deliveryMethod" value="86400" />
<entry key="PaymentMode" value="3600" />
<entry key="PaymentMethod" value="3600" />
<entry key="SecurityResources" value="3600" />
<entry key="user" value="3600" />
<entry key="role" value="3600" />
<entry key="menuTree" value="3600" />
<entry key="menu" value="3600" />
</util:map>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: