Spring整合redis

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合redis相关的知识,希望对你有一定的参考价值。

1.环境

  需要spring.jar、common-pool.jar.jedisjar

2.简介

  分片(shardedjedis)将不同的key分配到不同的redis server上,达到横向扩展的目的。

3.配置application.xml


<!-- 加载redis配置文件 -->
<context:property-placeholder location="classpath:redis.properties"/>
<!-- 连接池配置 -->
<
bean id="poolCfg" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxActive}" /> <property name="maxIdle" value="${redis.pool.maxIdle}" /> <property name="minIdle" value="1"/> <property name="maxWaitMillis" value="${redis.pool.maxWait}" /> <property name="testOnBorrow" value="true"/> <property name="testOnReturn" value="true"/> </bean>
<!-- port记得指定为int类型 --> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="poolCfg"></constructor-arg> <constructor-arg index="1"> <list> <bean name="master" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis1.ip}"></constructor-arg> <constructor-arg index="1" value="${redis1.port}" type="int"></constructor-arg> <property name="password" value="${redis2.auth}"/> </bean> <bean name="slaver" class="redis.clients.jedis.JedisShardInfo"> <constructor-arg index="0" value="${redis2.ip}"></constructor-arg> <constructor-arg index="1" value="${redis2.port}" type="int"></constructor-arg> <property name="password" value="${redis2.auth}"/> </bean> </list> </constructor-arg> </bean>

4.获取资源,释放资源

@Repository("jedisDataResource")
public class JedisDataResourceImpl implements JedisDataResource{

    @Resource(name="shardedJedisPool")
    private ShardedJedisPool sharededJedisPool;
    
    @Override
    public ShardedJedis getResource() {
        ShardedJedis shardedJedis=null;
        try {
            shardedJedis =sharededJedisPool.getResource();
            return shardedJedis;
        } catch (Exception e) {
            if(null != sharededJedisPool)
                sharededJedisPool.close();
        }
        return null;
    }

    @Override
    public void returnResource(ShardedJedis shardedJedis) {
        if(shardedJedis!=null)
            shardedJedis.close();
    }
    
}

5.dao层

public interface UserRedisDao {
    
    /**获取用户名*/
    double getUserName(String key);

}

@Repository("userRedisDao")
public class UserRedisDaoImpl implements UserRedisDao{
    
    @Resource
    private JedisDataResource jedisDataResource;
    
    @Override
    public String getUserName(String key) {
        ShardedJedis shardedJedis = jedisDataResource.getResource();
        if (null == shardedJedis)
            return null;
        try {
            return shardedJedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedisDataResource.returnResource(shardedJedis);
        }
    }

6.service

public interface CommodityRedisService {
    
    String getUserName(String key);
    
}
@Service("userRedisService")
public class UserRedisServiceImpl implements UserRedisService{
        
        @Autowired
        private UserRedisDao UserRedisDao;

        @Override
        public String increaseCollectNum(String key) {
            return userRedisDao.getUserName(key);
        }
}

 

以上是关于Spring整合redis的主要内容,如果未能解决你的问题,请参考以下文章

3分钟搞定springboot整合redis

spring 整合 redis的配置

redis spring怎么整合

Spring Boot 整合Redis 实现缓存

spring整合redis后怎么更改db

Spring整合redis,通过sentinel进行主从切换