springmvc 集成JedisCluster 配置
点关注不迷路,欢迎再访!
cluster相对于哨兵模式是去中心化的,它的每个节点都存储了其它集群的信息,因此每个节点都可以做为集群的中心,容错能力强,具有更高的可用性和在线扩容能力。
引入依赖
<--2.9.0 以下版本不支持cluster密码认证-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</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文件
<?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>
<bean id="jedis" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port1}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port2}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port3}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port4}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port5}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${cluster.host1}"></constructor-arg>
<constructor-arg name="port" value="${cluster.port6}"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg name="connectionTimeout" value="3000" />
<constructor-arg name="soTimeout" value="3000" />
<constructor-arg name="maxAttempts" value="1000" />
<constructor-arg name="password" value="${redis.auth}" />
<constructor-arg name="poolConfig"> <ref bean="jedisPoolConfig" />
</constructor-arg>
</bean>
</beans>
测试验证
@Autowired
private JedisCluster jedis;
@Test
public void test(){
jedis.set("abc123", "abc123");
System.out.println("*********************************"+jedis.get("abc123"));
}