分布式实现redis锁 出现错误Could not get a resource from the pool

阅读更多
具体错误信息:
Exception in thread "d窗口" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:286)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:469)
at com.redies.demo.lock.RediesLock.tryLock(RediesLock.java:56)
at com.redies.demo.lock.RediesLock.lock(RediesLock.java:39)
at com.redies.demo.lock.RediesLockTest$TicketRunner.run(RediesLockTest.java:50)
at java.lang.Thread.run(Thread.java:745)
Caused by: 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 redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:276)
... 5 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:448)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 8 more
Exception in thread "c窗口" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:286)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:469)
at com.redies.demo.lock.RediesLock.tryLock(RediesLock.java:56)
at com.redies.demo.lock.RediesLock.lock(RediesLock.java:39)
at com.redies.demo.lock.RediesLockTest$TicketRunner.run(RediesLockTest.java:50)
at java.lang.Thread.run(Thread.java:745)
Caused by: 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 redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:276)
... 5 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:448)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 8 more
解决方法:jedis设置最大活动数
connectionFactory.getPoolConfig().setMaxTotal(1000); 默认为8

项目环境:(具体代码如附件)
spring boot 2.0.1.RELEASE
spring-boot-starter-data-redis
用jedis操作redis,使用注解注册bean
@Configuration
public class RedisConfig {
    @Value("${redis.host}")
    private String hostName;
    @Value("${redis.port}")
    private String port;
    @Bean
    JedisConnectionFactory jedisConnectionFactory()
    {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(hostName);
        configuration.setPort(Integer.valueOf(port));
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory(configuration);
        connectionFactory.getPoolConfig().setMaxTotal(1000);//#最大活动对象数
        connectionFactory.getPoolConfig().setMaxWaitMillis(2000);
        return connectionFactory;

    }
具体代码如附件

最开始未设置connectionFactory.getPoolConfig().setMaxWaitMillis(2000);导致异常错误一直未打印,原因是BlockWhenExhausted( connectionFactory.getPoolConfig().setBlockWhenExhausted(false))的默认值为true,阻塞到超时,才会打印相应的错误
  • demo.rar (105.3 KB)
  • 下载次数: 1

你可能感兴趣的:(bean,redis,springboot)