zoukankan      html  css  js  c++  java
  • (转)ShardedJedisPool的使用

    package com.test;

    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;

    public class RedisShardPoolTest {
        
        static ShardedJedisPool pool;

        static{
            JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
            config.setMaxActive(500);//最大活动的对象个数
            config.setMaxIdle(1000 * 60);//对象最大空闲时间
            config.setMaxWait(1000 * 10);//获取对象时最大等待时间
            config.setTestOnBorrow(true);
            String hostA = "192.168.0.100";
            int portA = 6379;
            String hostB = "192.168.0.115";
            int portB = 6379;
            List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2);
            JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
            infoA.setPassword("admin");
            JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
            infoB.setPassword("admin");
            jdsInfoList.add(infoA);
            jdsInfoList.add(infoB);
            pool =new ShardedJedisPool(config, jdsInfoList);
         }

           

        /**
        
         * @param args
        
         */
        
        public static void main(String[] args) {
            for(int i=0; i<100; i++){
                String key = generateKey();
                ShardedJedis jds = null;
                try {
                    jds = pool.getResource();
                    System.out.println(key+":"+jds.getShard(key).getClient().getHost());
                    System.out.println(jds.set(key,Math.random()+""));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    pool.returnResource(jds);
                }
            }
        }
        
        private static int index = 1;
        public static String generateKey(){
            return String.valueOf(Thread.currentThread().getId())+"_"+(index++);
        }
    }

  • 相关阅读:
    xtrabackup
    spark对机器的要求
    hbase的总结
    TO B公司高效能的组织建设实践
    如何给客户展示实力
    什么样的IT队伍是好队伍
    程序员如何使用OKR
    云原生
    Flink 的18个小知识点
    apt 常用命令
  • 原文地址:https://www.cnblogs.com/tiancai/p/9522033.html
Copyright © 2011-2022 走看看