CodisLabs / jodis

A java client for codis based on Jedis and Curator
MIT License
216 stars 97 forks source link

启动codis客户端初始化 异常 #64

Closed Force-King closed 4 years ago

Force-King commented 5 years ago

我自己新创建的spring boot 测试codis项目没问题,将codis初始化连接池代码放到实际项目(spring mvc)中,就会报错,请问是怎么回事? 哪位大神帮帮忙,谢谢🙏 报错如下:

redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:51)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at io.codis.jodis.RoundRobinJedisPool.getResource(RoundRobinJedisPool.java:218)
    at com.br.hermes.center.service.impl.IndexServiceV3Impl.initLimitProductMap(IndexServiceV3Impl.java:2780)
    at com.br.hermes.center.quartz.SendMailQuartz.refreshFlowControllConfig(SendMailQuartz.java:608)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
    at java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.NoSuchElementException: Unable to validate object
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:506)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 19 more

我的初始化连接池代码:

@Configuration
public class CodisClientHA {

    private Logger logger = LogManager.getLogger(CodisClientHA.class);

    @Value("${codis.zkAddr}")
    private String zkAddr;

    @Value("${codis.zk.proxy.dir}")
    private String zkProxyDir;

    @Value("${codis.password}")
    private String password;

    @Value("${codis.timeout}")
    private int timeout;

    @Value("${codis.maxActive}")
    private int max_active;

    @Value("${codis.maxIdle}")
    private int max_idle;

    @Value("${codis.minIdle}")
    private int min_idle;

    @Value("${codis.maxWait}")
    private long max_wait;

    @Bean
    public JedisResourcePool getPool() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(max_idle);
        poolConfig.setMaxTotal(max_active);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true); 
        poolConfig.setMaxWaitMillis(max_wait);
        poolConfig.setBlockWhenExhausted(false); 
        JedisResourcePool pool = RoundRobinJedisPool.create().poolConfig(poolConfig)
                .curatorClient(zkAddr, timeout).zkProxyDir(zkProxyDir).build();
        logger.info("------------------- Codis connection pool init succeed -------------------");
        return pool;
    }

配置:

codis.zkAddr=192.168.1.10:2181,192.168.1.11:2181,192.168.1.12:2181
codis.zk.proxy.dir=/jodis/codis
codis.timeout=30000
codis.password=
codis.maxIdle=100
codis.maxActive=1000
codis.maxWait=1000
codis.minIdle=50

使用时:

@Autowired
private JedisResourcePool jedisPool;

   @RequestMapping(value = "/codis/test/get")
    public String get(String key) {
        logger.info("codis get by key:{}",key);
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            logger.error("codis exception, get failed! key ={} ",key,e);
            return null;
        }
    }

    @RequestMapping(value = "/codis/test/set")
    public void set(String key, String value) {
        logger.info("codis set, key:{},value:{}",key,value);
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
        } catch (Exception e) {
            logger.error("codis exception, set failed! key = {}, value = {}.", key, value,e);
        }
    }
KobeAndLebron commented 4 years ago

是不是Slot没有分配给组?

Force-King commented 4 years ago

是不是Slot没有分配给组?

不是,是因为 代理的路径不对,已解决。