• 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;
    //这个接口是操作sharedJedis
    public 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就知道了

  • 相关阅读:
    忙活了半宿,写了个小玩意
    luogu P5171 Earthquake
    luogu P1850 换教室
    luogu P2507 [SCOI2008]配对 |动态规划
    luogu P3830 [SHOI2012]随机树
    luogu P3959 宝藏
    牛客竞赛-比赛
    牛客竞赛-Who killed Cock Robin
    luogu P3807 【模板】卢卡斯定理
    牛客竞赛 -被3整除的子序列
  • 原文地址:https://www.cnblogs.com/ruiati/p/5972116.html
Copyright © 2020-2023  润新知