一、spring-data-redis使用哨兵配置一主多從


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>
配置完成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM