Redis入门到精通-Redis与Spring整合

Redis集群与spring的整合

1 Jedis客户端的使用

Jedis下载地址:

https://github.com/xetorthio/jedis

Maven依赖地址:

http://mvnrepository.com/artifact/redis.clients/jedis

直接在Java代码中使用Redis集群

        // 数据库链接池配置
        JedisPoolConfig config = new JedisPoolConfig();  
        config.setMaxTotal(100);  
        config.setMaxIdle(50);  
        config.setMinIdle(20);  
        config.setMaxWaitMillis(6 * 1000);  
        config.setTestOnBorrow(true);  
        
        // Redis集群的节点集合
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7111));
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7112));
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7113));
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7114));
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7115));
        jedisClusterNodes.add(new HostAndPort("10.0.31.144", 7116));
        
        // 根据节点集创集群链接对象
        //JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);
        // 集群各节点集合,超时时间,最多重定向次数,链接池
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, 2000, 100, config);
        int num = 1000;
        String key = "test";
        String value = "";
        for (int i=1; i <= num; i++){
            // 存数据
            jedisCluster.set(key+i, "test"+i);
            // 取数据
            value = jedisCluster.get(key+i);
            System.out.println(key+i + "=" + value);
            // 删除数据
            //jedisCluster.del(key+i); 
            //value = jedisCluster.get(key+i); 
            //System.out.println(key+i + "=" + value);
        }

2 Spring配置Jedis链接Redis3.x集群

Spring-redis.xml配置文件

 version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="100" />
        <property name="maxIdle" value="20" />
        <property name="minIdle" value="10" />
        <property name="blockWhenExhausted" value="true">property>
        <property name="maxWaitMillis" value="3000" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <property name="minEvictableIdleTimeMillis" value="60000" />
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <property name="numTestsPerEvictionRun" value="-1" />
    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.0.31.144" />
                    <constructor-arg index="1" value="7111" type="int" />
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="10.0.31.144" />
                    <constructor-arg index="1" value="7112" type="int" />
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="10.0.31.144" />
                    <constructor-arg index="1" value="7113" type="int" />
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="10.0.31.144" />
                    <constructor-arg index="1" value="7114" type="int" />
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="10.0.31.144" />
                    <constructor-arg index="1" value="7115" type="int" />
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="10.0.31.144" />
                    <constructor-arg index="1" value="7116" type="int" />
                bean>
            set>
        constructor-arg>
        <constructor-arg index="1" value="2000" type="int">constructor-arg>
        <constructor-arg index="2" value="100" type="int">constructor-arg>
        <constructor-arg index="3" ref="jedisPoolConfig">constructor-arg>
    bean>

beans>

测试Client.java代码

            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-redis.xml");
            context.start();
            JedisCluster jedisCluster = (JedisCluster) context.getBean("jedisCluster");
            int num = 1000;
            String key = "test";
            for (int i=1; i <= num; i++){
                // 取数据
                System.out.println(jedisCluster.get(key+i));
            }
            context.stop();

你可能感兴趣的:(Redis入门到精通-Redis与Spring整合)