linux下的redis+spring集群整合(七)

来源:互联网 发布:ios 可变字典添加数据 编辑:程序博客网 时间:2024/06/06 00:52

其实这里只是简单的模拟了一下redis的集群,就是在两台linux下部署redis将其通过spring绑定到一起,简单的实现集群的效果。只需要修改下spring中的配置信息,将两台redis通过List存到shardedJedisPool中即可。
spring的配置:

<context:property-placeholder location="classpath:redis2.properties" />  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">      <property name="maxTotal"  value="${redis.pool.maxActive}" />     <property name="maxIdle"    value="${redis.pool.maxIdle}" />      <property name="maxWaitMillis"    value="${redis.pool.maxWait}" />      <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />  </bean>  <bean  id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" >      <constructor-arg  index="0" ref="jedisPoolConfig" />      <constructor-arg index="1">          <list>              <bean class="redis.clients.jedis.JedisShardInfo">                  <constructor-arg                      index="0"                      value="${redis.ip}" />                  <constructor-arg                      index="1"                      value="${redis.port}"                      type="int" />              </bean>             <bean class="redis.clients.jedis.JedisShardInfo">                  <constructor-arg                      index="0"                      value="${redis.ip2}" />                  <constructor-arg                      index="1"                      value="${redis.port}"                      type="int" />              </bean>          </list>      </constructor-arg>  </bean>  </beans>

测试代码:

import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;

public class Test {

public static void main(String[] args) {     ApplicationContext app= new ClassPathXmlApplicationContext("spring.xml");        ShardedJedisPool pool=(ShardedJedisPool) app.getBean("shardedJedisPool");    ShardedJedis jedis = pool.getResource();      String keys = "name";    jedis.del(keys);    jedis.set(keys, "yjl");    String value = jedis.get(keys);    System.out.println(value);    pool.returnBrokenResource(jedis);//释放连接池}

}

0 0