最近一直在使用Redis作为缓存数据库,在使用当中,刚开始没有注意配置问题。
1、纯粹的注入单机模式
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1" />
<property name="maxTotal" value="5" />
<property name="blockWhenExhausted" value="true" />
<property name="maxWaitMillis" value="30000" />
<property name="testOnBorrow" value="true" />
</bean> <bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="此处填写redis-host" />
<property name="port" value="此处填写redis-port" />
<property name="database" value="1" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true" />
<property name="timeout" value="10000"></property>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean> <alias name="redisTemplate" alias="cacheRedisTemplate"/>
<alias name="redisTemplate" alias="transportRedisTemplate"/> <!-- 使用StringRedisTemplate配置 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean>
2、Redis集群模式,集群模式又分为两种,哨兵模式实现 和 RedisClusterConfiguration实现,配置如下
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg index="0"> <!-- 哨兵模式:RedisSentinelConfiguration,也是可以支持集群
<bean class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg>
<set>
<value>www.baidu.com:26379</value>
<value>www.cnblogs.com:26479</value>
</set>
</bean>
--> <!-- RedisClusterConfiguration, -->
<bean class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<constructor-arg>
<list>
<value>127.0.0.1:8001</value>
<value>127.0.0.2:8002</value>
<value>127.0.0.3:8003</value>
<value>127.0.0.4:8004</value>
<value>127.0.0.5:8005</value>
<value>127.0.0.6:8006</value>
</list>
</constructor-arg>
<property name="maxRedirects" value="5" />
</bean> </constructor-arg>
<constructor-arg index="1">
<bean class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="100"/>
<property name="maxIdle" value="10"/>
<property name="minIdle" value="1"/>
<property name="maxWaitMillis" value="30000"/>
</bean>
</constructor-arg>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean> <alias name="redisTemplate" alias="cacheRedisTemplate"/> <alias name="redisTemplate" alias="transportRedisTemplate"/> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean>
哨兵模式主要是监听、检测 master 的状态,如果监听到状态异常,则使用M-S进行切换,将其中的Slave作为master,将master(异常)作为Slave,转换之后相对应的配置文件也会随着改变;
其他相关参考:
https://blog.csdn.net/donggang1992/article/details/50981341
https://blog.csdn.net/zhousenshan/article/details/51822253