springmvc集成Redis Cluster集群
点关注不迷路,欢迎再访!
cluster相对于哨兵模式是去中心化的,它的每个节点都存储了其它集群的信息,因此每个节点都可以做为集群的中心,容错能力强,具有更高的可用性和在线扩容能力。
引入依赖
<--2.9.0 以下版本不支持cluster密码认证-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
配置redis.properties
###############################redis数据库的相关配置##################################
##访问密码
redis.auth = xxxxxx
##控制一个pool最多可以有多少个状态为Idle(空)的jedis实例默认值为8
redis.maxIdle = 200
##可用的最大连接实例数 默认为8
redis.maxActive = 1024
##等待可用连接的最大时间单位为毫秒 默认为-1表示永不超时,一旦超过等待时间则直接抛出
redis.maxWait = 10000
redis.timeOut = 10000
##设置为true则会在borrow一个jedis实例时,提前做validate操作
redis.testOnBorrow = true
##连接最小空闲时间(毫秒)
redis.minEvictableIdleTimeMillis=1800000
##释放连接的扫描间隔(毫秒)
redis.timeBetweenEvictionRunsMillis=30000
##每次释放连接的最大数目
redis.numTestsPerEvictionRun=1024
##最大连接数
redis.maxTotal=30
##在空闲时检查有效性, 默认false
redis.testWhileIdle=true
#集群节点
cluster.host1=ip
cluster.port1=7001
cluster.port2=7002
cluster.port3=7003
cluster.port4=7004
cluster.port5=7005
cluster.port6=7006
配置redis.xml文件
如果redis集群设置了密码,则需在配置文件redis.xml中加入密码设置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- redis连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle }" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow }" />
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
</bean>
<!-- redis集群 -->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="6"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port1}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port2}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode ">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port3}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port4}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port5}" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${cluster.host1}" />
<constructor-arg name="port" value="${cluster.port6}" />
</bean>
</set>
</property>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration" />
<property name="password" value="${redis.auth}" />
</bean>
<!-- StringRedisSerializer模板-->
<!-- <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean> -->
<!-- RedisTemplate模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
</beans>
踩坑环节
1.spring-data-redis 1.8.0以下版本不支持密码
启动项目会报Jedis does not support password protected Redis Cluster configurations!
原因:使用spring-data-redis操作redis集群时由于redis集群设置了密码。
解决方案:升级spring-data-redis版本即可解决
2.spring-data-redis2.0以上的版本中在配置密码的时候不能像老版本直接设置密码值,需要注入一个RedisPassword的bean 在RedisPassword的构造方法中设置密码,需要spring5环境
<!--spring-data-redis2.0以上的配置-->
<bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg name="thePassword" value="testpw"></constructor-arg>
</bean>
<bean id="redisStandaloneConfiguration"
class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="password" ref="redisPassword" />
</bean>
测试验证
@Autowired
private RedisTemplate redisTemplate;
public void test(){
redisTemplate.opsForValue().set("hello", "你好");
redisTemplate.opsForValue().get("hello");
}