zoukankan      html  css  js  c++  java
  • jedis连接池详解(Redis)

    原子性(atomicity):

    一个事务是一个不可分割的最小工作单位,事务中包括的诸操作要么都做,要么都不做。

    Redis所有单个命令的执行都是原子性的,这与它的单线程机制有关;

    Redis命令的原子性使得我们不用考虑并发问题,可以方便的利用原子性自增操作INCR实现简单计数器功能;

    单机模式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    package com.ljq.utils;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    /**   
     * Redis操作接口
     *
     * @author 林计钦
     * @version 1.0 2013-6-14 上午08:54:14   
     */
    public class RedisAPI {
        private static JedisPool pool = null;
         
        /**
         * 构建redis连接池
         
         * @param ip
         * @param port
         * @return JedisPool
         */
        public static JedisPool getPool() {
            if (pool == null) {
                JedisPoolConfig config = new JedisPoolConfig();
                //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
                //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
                config.setMaxActive(500);
                //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
                config.setMaxIdle(5);
                //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
                config.setMaxWait(1000 100);
                //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
                config.setTestOnBorrow(true);
                pool = new JedisPool(config, "192.168.2.191"8888);
            }
            return pool;
        }
         
        /**
         * 返还到连接池
         
         * @param pool 
         * @param redis
         */
        public static void returnResource(JedisPool pool, Jedis redis) {
            if (redis != null) {
                pool.returnResourceObject(redis);
            }
        }
         
        /**
         * 获取数据
         
         * @param key
         * @return
         */
        public static String get(String key){
            String value = null;
             
            JedisPool pool = null;
            Jedis jedis = null;
            try {
                pool = getPool();
                jedis = pool.getResource();
                value = jedis.get(key);
            catch (Exception e) {
                //释放redis对象
                pool.returnBrokenResource(jedis);
                e.printStackTrace();
            finally {
                //返还到连接池
                returnResource(pool, jedis);
            }
             
            return value;
        }
    }

    参考文章:

    http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html

    分布式模式 

     

    ShardedJedis是基于一致性哈希算法实现的分布式Redis集群客户端

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    package com.jd.redis.client;
      
    import java.util.ArrayList;
    import java.util.List;
      
    import redis.clients.jedis.JedisPoolConfig;
    import redis.clients.jedis.JedisShardInfo;
    import redis.clients.jedis.ShardedJedis;
    import redis.clients.jedis.ShardedJedisPool;
    import redis.clients.util.Hashing;
    import redis.clients.util.Sharded;
      
    publicclass RedisShardPoolTest {
         
    static ShardedJedisPoolpool;
         
    static{
             
    JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
             
    config.setMaxActive(500);//最大活动的对象个数
             
      config.setMaxIdle(1000 60);//对象最大空闲时间
            
       config.setMaxWait(1000 10);//获取对象时最大等待时间
           
        config.setTestOnBorrow(true);
             
    String hostA = "10.10.224.44";
            
       int portA = 6379;
            
       String hostB = "10.10.224.48";
            
       int portB = 6379;
             
    List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2);
             
    JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
             
    infoA.setPassword("redis.360buy");
             
    JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
             
    infoB.setPassword("redis.360buy");
             
    jdsInfoList.add(infoA);
             
    jdsInfoList.add(infoB);
             
             
    pool =new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH,
    Sharded.DEFAULT_KEY_TAG_PATTERN);
        //传入连接池配置、分布式redis服务器主机信息、分片规则(存储到哪台redis服务器)
    }
         
         
    /**
         
     * @param args
         
     */
         
    publicstaticvoid main(String[] args) {
             
    for(int i=0; i<100; i++){
               String key =generateKey();
               //key += "{aaa}";
               ShardedJedis jds =null;
               try {
                   jds =pool.getResource();
                   System.out.println(key+":"+jds.getShard(key).getClient().getHost());
                   System.out.println(jds.set(key,"1111111111111111111111111111111"));
               }catch (Exception e) {
                   e.printStackTrace();
               }
               finally{
                   pool.returnResourceObject(jds);
               }
             
    }
         
    }
      
         
    privatestaticintindex = 1;
         
    publicstatic String generateKey(){
             
    return String.valueOf(Thread.currentThread().getId())+"_"+(index++);
         
    }
    }

    参考文章:

    http://blog.csdn.net/lang_man_xing/article/details/38405269

  • 相关阅读:
    查看ASM 使用率
    修改一行和修改全表的TX锁
    OCP-1Z0-053-V12.02-683题
    专栏成书亦可行也!——leo鉴书48
    Java用正则表达式判断是否为IP
    OCP-1Z0-053-V12.02-594题
    OCP-1Z0-053-V12.02-585题
    OCP-1Z0-053-V12.02-569题
    OCP-1Z0-053-V12.02-568题
    OCP-1Z0-053-V13.02-517题
  • 原文地址:https://www.cnblogs.com/zhangtan/p/7607934.html
Copyright © 2011-2022 走看看