1、JedisPoolConfig配置
<!-- jedis配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最小空闲数:低于minIdle时,将创建新的连接 -->
<property name="minIdle" value="10" />
<!-- 最大空闲数:空闲连接数大于maxIdle时,将进行回收 -->
<property name="maxIdle" value="100" />
<!-- 最大连接数:能够同时建立的最大连接个数 -->
<property name="maxTotal" value="1000" />
<!-- 最大等待时间:单位ms -->
<property name="maxWaitMillis" value="10000" />
<!-- 使用连接时,检测连接是否成功 -->
<property name="testOnBorrow" value="false" />
</bean>
2、RedisSentinelConfiguration哨兵配置
<bean id="sentinelConfig" class="com.xxx.redis.RedisSentinelConfig">
<constructor-arg name="masterName" value="masterTest" /><!-- mymaster -->
<constructor-arg name="sentinels" value="xxx.xxx.xxx.xxx:12621,xxx.xxx.xxx.xxx:12621,xxx.xxx.xxx.xxx:12621" />
</bean>
RedisSentinelConfig类实现如下:
public class RedisSentinelConfig extends RedisSentinelConfiguration {
public RedisSentinelConfig(String masterName, String sentinels) {
super.setMaster(masterName);
Set<RedisNode> nodes = new HashSet<RedisNode>();
String[] sentinelArray = StringUtils.split(sentinels, ",");
for(String hostAndPort:sentinelArray) {
String[] args = split(hostAndPort.trim(), ":");
nodes.add(new RedisNode(args[0], Integer.valueOf(args[1]).intValue()));
}
super.setSentinels(nodes);
}
}
3、JedisConnectionFactory配置
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="password" value="xxxxxx" />
<property name="usePool" value="true" />
<property name="database" value="6"></property>
<constructor-arg name="sentinelConfig" ref="sentinelConfig"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
4、RedisTemplate配置
<!-- redis template 配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
配置完成