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

[color=red]具体错误信息:[/color]
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
[color=red]解决方法:jedis设置最大活动数[/color]
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);
[color=red] connectionFactory.getPoolConfig().setMaxTotal(1000);//#最大活动对象数[/color]
connectionFactory.getPoolConfig().setMaxWaitMillis(2000);
return connectionFactory;

}
[color=red]具体代码如附件[/color]

最开始未设置connectionFactory.getPoolConfig().setMaxWaitMillis(2000);导致异常错误一直未打印,原因是BlockWhenExhausted( [color=red]connectionFactory.getPoolConfig().setBlockWhenExhausted(false)[/color])的默认值为true,阻塞到超时,才会打印相应的错误
展开阅读全文

没有更多推荐了,返回首页