spring集成Jedis Cluster配置

20 篇文章 0 订阅
6 篇文章 0 订阅
  1. <!-- Jedis链接池配置,注意:Jedis版本建议升级到最新 -->  
  2.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  3.         <property name="maxTotal" value="100" />  
  4.         <property name="maxIdle" value="20" />  
  5.         <property name="minIdle" value="10" />  
  6.         <property name="blockWhenExhausted" value="true"></property>  
  7.         <property name="maxWaitMillis" value="3000" />  
  8.         <property name="testOnBorrow" value="false" />  
  9.         <property name="testOnReturn" value="false" />  
  10.         <property name="testWhileIdle" value="true" />  
  11.         <property name="minEvictableIdleTimeMillis" value="60000" />  
  12.         <property name="timeBetweenEvictionRunsMillis" value="30000" />  
  13.         <property name="numTestsPerEvictionRun" value="-1" />  
  14.     </bean>  
  15.   
  16.     <!-- JedisCluster -->  
  17.     <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">  
  18.         <constructor-arg index="0">  
  19.             <set>  
  20.                 <bean class="redis.clients.jedis.HostAndPort">  
  21.                     <constructor-arg index="0" value="192.168.1.111" />  
  22.                     <constructor-arg index="1" value="7111" type="int" />  
  23.                 </bean>  
  24.                 <bean class="redis.clients.jedis.HostAndPort">  
  25.                     <constructor-arg index="0" value="192.168.1.112" />  
  26.                     <constructor-arg index="1" value="7112" type="int" />  
  27.                 </bean>  
  28.                 <bean class="redis.clients.jedis.HostAndPort">  
  29.                     <constructor-arg index="0" value="192.168.1.113" />  
  30.                     <constructor-arg index="1" value="7113" type="int" />  
  31.                 </bean>  
  32.                 <bean class="redis.clients.jedis.HostAndPort">  
  33.                     <constructor-arg index="0" value="192.168.1.114" />  
  34.                     <constructor-arg index="1" value="7114" type="int" />  
  35.                 </bean>  
  36.                 <bean class="redis.clients.jedis.HostAndPort">  
  37.                     <constructor-arg index="0" value="192.168.1.115" />  
  38.                     <constructor-arg index="1" value="7115" type="int" />  
  39.                 </bean>  
  40.                 <bean class="redis.clients.jedis.HostAndPort">  
  41.                     <constructor-arg index="0" value="192.168.1.116" />  
  42.                     <constructor-arg index="1" value="7116" type="int" />  
  43.                 </bean>  
  44.             </set>  
  45.         </constructor-arg>  
  46.         <constructor-arg index="1" value="2000" type="int"></constructor-arg>  
  47.         <constructor-arg index="2" value="100" type="int"></constructor-arg>  
  48.         <constructor-arg index="3" ref="jedisPoolConfig"></constructor-arg>  
  49.     </bean>  


package lr.test.redis;  
  
import org.apache.commons.logging.Log;  
import org.apache.commons.logging.LogFactory;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
import redis.clients.jedis.JedisCluster;  
  
  
public class RedisClusterSpringTest {  
    private static final Log log = LogFactory.getLog(RedisClusterSpringTest.class);  
  
    public static void main(String[] args) {  
        try {  
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");  
            context.start();  
              
            JedisCluster jedisCluster = (JedisCluster) context.getBean("jedisCluster");  
// JedisCluster jedisCluster = SpringContextHolder.getBean(JedisCluster.class);

            int num = 1000;  
            String key = "yingjun";  
            String value = "";  
            for (int i=1; i <= num; i++){  
                // 存数据  
                //jedisCluster.set(key+i, "yingjun"+i);  
                //jedisCluster.setex(key+i, 60, "yingjun"+i);  
                  
                // 取数据  
                value = jedisCluster.get(key+i);   
                log.info(key+i + "=" + value);  
                  
                // 删除数据  
                //jedisCluster.del(key+i);   
                //value = jedisCluster.get(key+i);   
                //log.info(key+i + "=" + value);  
            }  
  
            context.stop();  
        } catch (Exception e) {  
            log.error("==>RedisSpringTest context start error:", e);  
            System.exit(0);  
        } finally {  
            log.info("===>System.exit");  
            System.exit(0);  
        }  
    }  
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑的窝窝牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值