zoukankan      html  css  js  c++  java
  • 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);
            }
    }
  • 相关阅读:
    [转].net mvc + vuejs 的项目结构
    Outlook IMAP 修改PST文件存储路径
    VS2017 性能优化方法
    查询存储过程所需参数
    如何保障微服务架构下的数据一致性
    sqlserver批量给用户配置存储过程权限
    vue中刷新当前页面或重新加载的两种方法
    vue history模式下的微信支付,及微信支付授权目录的填写,处理URL未注册
    Vue 四行代码实现无感知上拉加载更多
    2019年前端必用正则(js)
  • 原文地址:https://www.cnblogs.com/rookie404/p/5878052.html
Copyright © 2011-2022 走看看