上一节我记录了如何搭建redis官方的集群,这节我就开始讲怎么用jedis实现集群环境下的客户端。
jedis中实现集群的客户端类是redis.clients.jedis.JedisCluster,里面有好多个构造方法,上一节中我们搭建的集群环境是有密码的,所以我用了这个构造方法
public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout,
int maxAttempts, String password, final GenericObjectPoolConfig poolConfig) {
super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig);
}
接下来就是spring的配置了
<context:property-placeholder location="classpath:redisConfig.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxIdle}" />
<!--连接池的最大数据库连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!--最大建立连接等待时间-->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--逐出连接的最小空闲时间 默认1800000毫秒(30分钟)-->
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
<!--每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3-->
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
<!--逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1-->
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
<!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<!--在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="${redis.testWhileIdle}" />
</bean>
<!--redis-cluster配置-->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<!--此处用的是jedis的其中一个构造方法-->
<!--JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts,
String password, final GenericObjectPoolConfig poolConfig)-->
<constructor-arg index="0">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7000" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7001" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7002" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7003" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7004" />
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="10.2.72.115" />
<constructor-arg index="1" value="7005" />
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" value="5000" />
<constructor-arg index="2" value="5000" />
<constructor-arg index="3" value="2" />
<constructor-arg index="4" value="root" />
<constructor-arg index="5" ref="poolConfig" />
</bean>
这里是测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-cache.xml"})
public class RedisTest {
@Autowired
JedisCluster jedisCluster;
@Test
public void testJedisCluster() {
System.out.println(jedisCluster.set("test", "test"));
System.out.println(jedisCluster.get("test"));
System.out.println(jedisCluster.get("name"));
System.out.println(jedisCluster.get("age"));
System.out.println(jedisCluster.get("haha"));
System.out.println(jedisCluster.get("haha"));
}
}
注意:我的jedis版本是2.9.0的,spring是4.2.5.RELEASE
具体代码可以到我的github上查看redis-cluster-demo