0

我在我的 java 项目中使用了 jedis,有一个 master 和一个 slave,一旦 slave 启动,它就会在 redis_slave.log 中出现:

44764 [2721] 24 Dec 14:07:41.157 * Connecting to MASTER...
44765 [2721] 24 Dec 14:07:41.158 * MASTER <-> SLAVE sync started
44766 [2721] 24 Dec 14:07:41.158 # Error condition on socket for SYNC: Connection refused

在我的java源文件中,我想删除redis中的所有数据,所以我写了这段代码:

public class TestJedisPool {

    private Jedis jedis = null;
    private JedisPool jedisPool = null;

    public TestJedisPool() {
        initialPool();
        jedis = jedisPool.getResource();
        jedis.auth("123456");
    }

    private void initialPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxActive(20);
        config.setMaxIdle(5);
        config.setMaxWait(1000L);
        config.setTestOnBorrow(false);
        jedisPool = new JedisPool(config, "192.168.144.3", 6397);
    }

    private void masterThread() {

        System.out.println(jedis.flushAll());       
        jedisPool.returnResource(jedis);
        jedis.disconnect();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestJedisPool test = new TestJedisPool();
        test.masterThread();
    }
}

并得到这样的异常:

    Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:22)
    at com.oppo.testpool.TestJedisPool.<init>(TestJedisPool.java:15)
    at com.oppo.testpool.TestJedisPool.main(TestJedisPool.java:41)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

任何人都可以帮助我吗?

4

1 回答 1

1

我修改了您的代码,它适用于:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class TestJedisPool {

    static ExecutorService executor = Executors.newSingleThreadExecutor();

    final static ShardedJedisPool redisStatsPool;
    static {
        String host = "127.0.0.1";
        int port = 6379;
        List<JedisShardInfo> redisClickShard = new ArrayList<JedisShardInfo>();
        redisClickShard.add(new JedisShardInfo(host, port));

        JedisPoolConfig config = new JedisPoolConfig();
        config.maxActive = 1000;
        config.maxIdle = 10;
        config.minIdle = 1;
        config.maxWait = 30000;
        config.numTestsPerEvictionRun = 3;
        config.testOnBorrow = true;
        config.testOnReturn =  true;
        config.testWhileIdle =  true;
        config.timeBetweenEvictionRunsMillis = 30000;
        redisStatsPool = new ShardedJedisPool( config, redisClickShard);
    }

    public TestJedisPool() {        

    }

    String[] getRandomNumber(int min, int max){
        String[] test = new String[8];
        for (int i = 0; i < test.length; i++) {
            int partition = min + (int)(Math.random() * ((max - min) + 1)); 
            test[i] = "key"+partition;
        }       
        return test;
    }

    static volatile long  sum = 0;

    public Runnable hincrBy(final String keyname, final String[] keyfields , final long val){
        Runnable job = new Runnable() {
            @Override
            public void run() {
                c++;
                System.out.println(c);
                try {
                    ShardedJedis shardedJedis = redisStatsPool.getResource();
                    final Jedis jedis = shardedJedis.getShard("") ;
                    Pipeline p = jedis.pipelined();
                    for (String keyfield : keyfields) {
                        p.hincrBy(keyname, keyfield, val);  
                        sum += val;
                    }
                    p.sync(); 
                    redisStatsPool.returnResource(shardedJedis);
                } catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        };
        return job;
    }

    static volatile int c = 0;
    static final int MAX = (int) Math.pow(10, 6);
    void masterThread() {       
        for (int i = 0; i < MAX; i++) {
            String[] keynames = getRandomNumber(100, 1000);
            executor.submit(hincrBy("test10^6", keynames, 1L));
        }
        executor.shutdown();
        while(!executor.isTerminated()){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    }
    public int sumTest() {
        int total = 0;
        try {
            ShardedJedis shardedJedis = redisStatsPool.getResource();
            final Jedis jedis = shardedJedis.getShard("") ;
            Map<String,String> map  = jedis.hgetAll("test10^6");
            Set<String> keys = map.keySet();

            for (String keyfield : keys) {
                int v = Integer.parseInt(map.get(keyfield));
                total += v;
            }

            redisStatsPool.returnResource(shardedJedis);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return total;
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestJedisPool test = new TestJedisPool();
        test.masterThread();
        System.out.println(sum);
        System.out.println(test.sumTest());
        System.out.println(test.sumTest() == sum);
    }
}
于 2013-05-10T09:04:30.327 回答