你的位置:首页 > Java教程

[Java教程]spring 使用redis集群配置


上面两篇介绍了redis集群的配置合一些基本的概念,所以接下来当然是要在项目中使用咯,redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作redis的架包:

  <dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.7.3</version>  </dependency>

我们需要做的仅仅是在spring配置文件中注入这些基本类,然后自己实现dao,下面是配置文件:

<context:property-placeholder ignore-unresolvable="true" location="classpath:yonyou.properties" />    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">    <property name="maxTotal" value="1000"/>     <property name="maxIdle" value="10"/>     <property name="minIdle" value="1"/>    <property name="maxWaitMillis" value="30000"/>     <property name="testOnBorrow" value="true"/>     <property name="testOnReturn" value="true"/>     <property name="testWhileIdle" value="true"/>    <!-- <property name="testWhileIdle" value="true"/> -->  </bean>      <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" destroy-method="destroy">    <constructor-arg ref="jedisPoolConfig"/>     <constructor-arg>
      <!--如果以后需要扩展集群,只需要复制一份redis,修改端口,然后在这里配置即可--> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="127.0.0.1"/> <constructor-arg index="1" value="6379"/> <constructor-arg index="2" value="instance:01"/> </bean> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="127.0.0.1"/> <constructor-arg index="1" value="6380"/> <constructor-arg index="2" value="instance:02"/> </bean> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="127.0.0.1"/> <constructor-arg index="1" value="6381"/> <constructor-arg index="2" value="instance:03"/> </bean> </list> </constructor-arg> </bean> <!--java帮我们同步sentinel的信息,将主从信息同步到客户端来--> <bean class="redis.clients.jedis.JedisSentinelPool"> <constructor-arg index="0" value="mymaster"/> <constructor-arg index="1"> <set> <value>127.0.0.1:26379</value> </set> </constructor-arg> <constructor-arg index="2" ref="jedisPoolConfig"/> </bean>

上面配置好了以后,我们就相当于将redis集群给映射过来了,下来我们只需要做一些操作,涉及到数据的CRUD.

package com.yonyou.hotusm.module.nosql.redis;import redis.clients.jedis.ShardedJedis;//这个接口是操作sharedJedispublic interface RedisDataSource {    public abstract ShardedJedis getRedisClient();  public void returnResource(ShardedJedis shardedJedis);  public void returnResource(ShardedJedis shardedJedis,boolean broken);}

实现:

package com.yonyou.hotusm.module.nosql.redis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;@Repository("redisDataSource")public class RedisDataSourceImpl implements RedisDataSource {  private static final Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);  @Autowired  private ShardedJedisPool  shardedJedisPool;  public ShardedJedis getRedisClient() {    try {      ShardedJedis shardJedis = shardedJedisPool.getResource();      return shardJedis;    } catch (Exception e) {      log.error("getRedisClent error", e);    }    return null;  }  public void returnResource(ShardedJedis shardedJedis) {    shardedJedisPool.close();    //shardedJedisPool.returnResource(shardedJedis);  }  public void returnResource(ShardedJedis shardedJedis, boolean broken) {    if (broken) {      shardedJedisPool.close();      //shardedJedisPool.returnBrokenResource(shardedJedis);    } else {      shardedJedisPool.close();      //shardedJedisPool.returnResource(shardedJedis);    }  }}

 下面就是操作jedis的具体类了:

@Repository("jedisTemplate")public class JedisTemplate {  private static final Logger log = LoggerFactory.getLogger(JedisTemplate.class);  @Autowired  private RedisDataSource   redisDataSource;  public void disconnect() {    ShardedJedis shardedJedis = redisDataSource.getRedisClient();    shardedJedis.disconnect();  }  /**   * 设置单个值   *   * @param key   * @param value   * @return   */  public String set(String key, String value) {    String result = null;    ShardedJedis shardedJedis = redisDataSource.getRedisClient();    if (shardedJedis == null) {      return result;    }    boolean broken = false;    try {      result = shardedJedis.set(key, value);    } catch (Exception e) {      e.printStackTrace();      log.error(e.getMessage(), e);      broken = true;    } finally {      redisDataSource.returnResource(shardedJedis, broken);    }    return result;  }  /**   * 获取单个值   *   * @param key   * @return   */  public String get(String key) {    String result = null;    ShardedJedis shardedJedis = redisDataSource.getRedisClient();    if (shardedJedis == null) {      return result;    }    boolean broken = false;    try {      result = shardedJedis.get(key);    } catch (Exception e) {      log.error(e.getMessage(), e);      broken = true;    } finally {      redisDataSource.returnResource(shardedJedis, broken);    }    return result;  }

真正的还有很多操作,list,set,哈希什么的,我就不把代码贴出来了,看看api就知道了