복붙노트

[SPRING] Jedis, jedis 연결을 얻을 수 없습니다 : 풀에서 자원을 가져올 수 없습니다.

SPRING

Jedis, jedis 연결을 얻을 수 없습니다 : 풀에서 자원을 가져올 수 없습니다.

몇 가지 스레드에서 답변을 보았지만 나에게 도움이되지 못했고 때로는 내 문제가 발생했기 때문에이 질문을 던지십시오.

jedis 버전 2.8.0, 스프링 데이터 redis 버전 1.7.5를 사용하고 있습니다. 캐싱 응용 프로그램 용 서버 버전 2.8.4가 있습니다.

내가 redis에 저장되면 요청을 얻을 redis에서 이루어집니다 여러 캐시가 있습니다. 데이터를 저장하고 가져 오기 위해 스프링 데이터 redis API를 사용하고 있습니다.

모든 저장 및 가져 오기는 정상적으로 작동하지만 가끔 예외가 발생하는 경우 :

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool | org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolorg.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:198)
org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:345)
org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92)
org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:166)
org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:88)
org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:49)

내 redis 구성 클래스 :

@Configuration
public class RedisConfiguration {

@Value("${redisCentralCachingURL}")
private String redisHost;

@Value("${redisCentralCachingPort}")
private int redisPort;

@Bean
public StringRedisSerializer stringRedisSerializer() {
  StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  return stringRedisSerializer;
}

@Bean
JedisConnectionFactory jedisConnectionFactory() {
  JedisConnectionFactory factory = new JedisConnectionFactory();
  factory.setHostName(redisHost);
  factory.setPort(redisPort);
  factory.setUsePool(true);
  return factory;
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
  RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  redisTemplate.setConnectionFactory(jedisConnectionFactory());
  redisTemplate.setExposeConnection(true);
  // No serializer required all serialization done during impl
  redisTemplate.setKeySerializer(stringRedisSerializer());
  //`redisTemplate.setHashKeySerializer(stringRedisSerializer());
  redisTemplate.setHashValueSerializer(new GenericSnappyRedisSerializer());
  redisTemplate.afterPropertiesSet();
  return redisTemplate;
}

@Bean
public RedisCacheManager cacheManager() {
  RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
  redisCacheManager.setTransactionAware(true);
  redisCacheManager.setLoadRemoteCachesOnStartup(true);
  redisCacheManager.setUsePrefix(true);
  return redisCacheManager;
 }

 }

누구든지이 문제에 직면했거나 이것에 대해 생각해 본 적이 있습니까? 왜 이런 일이 일어날 수 있습니까?

해결법

  1. ==============================

    1.우리는 RxJava와 동일한 문제에 직면했습니다. 응용 프로그램이 잘 돌아가고 있었지만 시간이 지나면 더 이상 풀에서 연결을 얻을 수 없었습니다. 디버깅 일 후에 우리는 마침내 문제를 일으킨 원인을 알아 냈습니다.

    우리는 RxJava와 동일한 문제에 직면했습니다. 응용 프로그램이 잘 돌아가고 있었지만 시간이 지나면 더 이상 풀에서 연결을 얻을 수 없었습니다. 디버깅 일 후에 우리는 마침내 문제를 일으킨 원인을 알아 냈습니다.

    redisTemplate.setEnableTransactionSupport(true)
    

    어떻게 든 연결을 해제하지 않는 스프링 데이터 redis가 발생했습니다. MULTI / EXEC에 대한 트랜잭션 지원이 필요했지만 결국이 문제를 없애기 위해 구현을 변경했습니다.

    우리는 이것이 우리 편에서의 버그인지 잘못된 사용인지 아직까지는 알지 못합니다.

  2. ==============================

    2.나는 redis.template에서 평범한 제다이로 옮겼다. 풀에 대한 아래의 구성을 추가했습니다 (redis 템플릿에도 추가 가능). 이제 예외가 표시되지 않습니다.

    나는 redis.template에서 평범한 제다이로 옮겼다. 풀에 대한 아래의 구성을 추가했습니다 (redis 템플릿에도 추가 가능). 이제 예외가 표시되지 않습니다.

    jedisPoolConfig.setMaxIdle(30);
    jedisPoolConfig.setMinIdle(10);
    

    redis 템플릿 :

    jedisConnectionFactory.getPoolConfig().setMaxIdle(30);
    jedisConnectionFactory.getPoolConfig()poolConfig.setMinIdle(10);
    

    위의 설정은 redis 템플릿에도 추가 할 수 있습니다.

  3. from https://stackoverflow.com/questions/43492474/jedis-cannot-get-jedis-connection-cannot-get-resource-from-pool by cc-by-sa and MIT license