1、maven依赖:
<spring.data.redis.version>1.8.10.RELEASE</spring.data.redis.version>
<spring.session.version>1.3.1.RELEASE</spring.session.version>
<jedis.version>2.9.0</jedis.version>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>${spring.session.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
2.属性配置
#############################################
#Redis主从服务哨兵配置
#################
redis.name=mymaster
redis.sentinel1.host=127.0.0.1
redis.sentinel1.port=26379
redis.sentinel2.host=127.0.0.1
redis.sentinel2.port=26380
redis.sentinel3.host=127.0.0.1
redis.sentinel3.port=26381
#############################################
#Redis公共部分
#################
#密码
redis.auth=123456
#db编号
redis.dbIndex=2
#最大连接数
redis.maxTotal=5000
#最大空闲数
redis.maxIdle=10
#最大等待时长
redis.maxWaitMillis=6000
#超时时间
redis.timeOut=2000
#在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true
3、xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!--redis哨兵 -->
<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<!-- master名称 sentinel.conf里面配置的主节点名称 -->
<constructor-arg name="master" value="${redis.name}" />
<!-- sentinel的ip和端口列表 -->
<constructor-arg name="sentinelHostAndPorts">
<set>
<value>${redis.sentinel1.host}:${redis.sentinel1.port}</value>
<value>${redis.sentinel2.host}:${redis.sentinel2.port}</value>
<value>${redis.sentinel3.host}:${redis.sentinel3.port}</value>
</set>
</constructor-arg>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="password" value="${redis.auth}" />
<property name="database" value="${redis.dbIndex}" />
<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration" />
<constructor-arg name="poolConfig" ref="poolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="${redis.name}" />
<constructor-arg index="1">
<set>
<value>${redis.sentinel1.host}:${redis.sentinel1.port}</value>
<value>${redis.sentinel2.host}:${redis.sentinel2.port}</value>
<value>${redis.sentinel3.host}:${redis.sentinel3.port}</value>
</set>
</constructor-arg>
<constructor-arg index="2" ref="poolConfig" />
<constructor-arg index="3" value="${redis.timeOut}" />
<constructor-arg index="4" value="${redis.auth}" />
<constructor-arg index="5" value="${redis.dbIndex}" />
</bean>
<!-- redis工具类 -->
<bean id="redisUtil" class="com.tensoon.util.RedisUtil">
<constructor-arg index="0" ref="jedisSentinelPool" />
<constructor-arg index="1" ref="redisTemplate" />
</bean>
<!-- 添加redis缓存的manager,设置超时时间一周 -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate" />
<!-- 是否事务提交,如果事务回滚,缓存也回滚,默认false -->
<property name="transactionAware" value="true" />
<property name="defaultExpiration" value="604800" />
</bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager"
key-generator="cacheKeyGenerator" proxy-target-class="true" />
<bean id="cacheKeyGenerator" class="com.tensoon.util.CacheKeyGenerator" />
<!-- 将session放入redis -->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="600" />
</bean>
<!-- 禁止Spring Session执行config命令 -->
<util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP" />
</beans>
本文介绍了如何将Spring与Redis结合,并利用Sentinel进行主从切换。配置包括了maven依赖、Redis主从服务哨兵的属性设置以及相关XML配置,实现了RedisTemplate和JedisSentinelPool的配置,确保在Redis主从故障时能够自动切换。
1万+

被折叠的 条评论
为什么被折叠?



