简体   繁体   中英

Using Lettuce with RedisTemplate throws exceptions

I am trying to figure out why Lettuce is giving me so many problems while I'm running my performance tests comparing it to Jedis.

I'm using spring-data-redis 1.8.11.RELEASE and I create custom proxy beans for interfaces to access redis through RedisTemplate. Redis is running in AWS and I use the cluster config endpoint provided by AWS as the node with 3 master and 3 slaves. Nothing special happens during the performance test. I simply call a service that reads a value from redis using RedisTemplate.

The test always passes with no exceptions when using JedisConnectionFactory, but when I switch to LettuceConnnectionFactory I cannot complete any of the tests because the channel initialization always times out. I increased the timeout to 30s and adjusted the shutdown timer as before I was getting thread interruption exceptions. However, even with 30s it still times out. I've tried with shared native connection and without and many different timeout values and they all result in the same problem.

The code for the connection factory bean:

@Bean
public RedisConnectionFactory lettuceConnectionFactory() {
    RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(Arrays.asList(cacheProps.nodes));
    clusterConfig.setMaxRedirects(6);
    LettuceConnectionFactory factory = new LettuceConnectionFactory(clusterConfig);
    factory.setTimeout(30000);
    factory.setShutdownTimeout(20000);
    return factory;
}

I tried tweaking the thread count for ClientResources, but I still continue to get timeout errors:

ClientResources res = DefaultClientResources.builder()
            .ioThreadPoolSize(10)
            .computationThreadPoolSize(10)
            .build();

The code to access Redis is simply going through RedisTemplate:

BoundHashOperations<Object, Object, Object> hashOps = redisTemplate.boundHashOps(request.getHashKey());
String data = (String) hashOps.get(request.getSubKey());
long timeout = request.getTtl();
try {
    if (timeout != 0) {
        hashOps.expire(timeout, request.getTimeUnit());
    }
    return mapper.readValue(data, request.getType());
} catch (Exception e) {
    logger.error("Exception for hash value read.", e);
}

The root cause of the exceptions while testing:

Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: /x.x.x.x:6379
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:216)
at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:120)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:408)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:402)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:140)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)

What am I missing here? From everything I read, everyone seems to prefer Lettuce, but from my tests I can't see why.

try with connection pool and pipeline

  private GenericObjectPoolConfig getPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    //All below should use the propertysources;
    poolConfig.setMaxTotal(20);
    poolConfig.setMaxIdle(20);
    poolConfig.setMinIdle(0);
    return poolConfig;
  }

  @Bean
  @Primary
  public RedisConnectionFactory redisConnectionFactory() {
    DefaultLettucePool lettucePool = new DefaultLettucePool(redisHost, Integer.valueOf(redisPort).intValue(), getPoolConfig());
    lettucePool.setPassword(redisPass);
    lettucePool.afterPropertiesSet();
    LettuceConnectionFactory clientConfig = new LettuceConnectionFactory(lettucePool);
    clientConfig.afterPropertiesSet();
    return clientConfig;
  }

  @Bean
  public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    return template;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM