reids集群学习(二)使用jedis实现redis集群客户端

上一节我记录了如何搭建redis官方的集群,这节我就开始讲怎么用jedis实现集群环境下的客户端。
jedis中实现集群的客户端类是redis.clients.jedis.JedisCluster,里面有好多个构造方法,上一节中我们搭建的集群环境是有密码的,所以我用了这个构造方法

public JedisCluster(Set 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}" />
        
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
        
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
        
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
        
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        
        <property name="testWhileIdle" value="${redis.testWhileIdle}" />
    bean>

    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        
        
        <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

你可能感兴趣的:(缓存,spring)