spring集成Jedis Cluster配置

来源:互联网 发布:仙界网络直播间txt下载 编辑:程序博客网 时间:2024/05/01 19:57
  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 0