使用带有RedisTemplate的Lettuce会抛出异常

时间:2018-06-14 16:18:14

标签: java redis spring-data-redis lettuce

我想弄清楚为什么Lettuce在运行我的性能测试时会给我这么多问题,并将它与Jedis进行比较。

我使用spring-data-redis 1.8.11.RELEASE并为界面创建自定义代理bean,以通过RedisTemplate访问redis。 Redis在AWS中运行,我使用AWS提供的集群配置端点作为具有3个主服务器和3个从服务器的节点。在性能测试中没有什么特别的事情发生。我只是调用一个使用RedisTemplate从redis读取值的服务。

使用JedisConnectionFactory时,测试总是没有异常,但是当我切换到LettuceConnnectionFactory时,我无法完成任何测试,因为通道初始化总是超时。我将超时时间增加到30秒并调整了关机计时器,就像我遇到线程中断异常一样。然而,即使有30秒,它仍然超时。我已尝试使用共享本机连接,并且没有和许多不同的超时值,它们都会导致同样的问题。

连接工厂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;
}

我尝试调整ClientResources的线程数,但我仍然继续得到超时错误:

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

访问Redis的代码只是通过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);
}

测试时异常的根本原因:

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)

我在这里缺少什么?从我读到的所有内容来看,每个人似乎都喜欢生菜,但从我的测试中我看不出原因。

1 个答案:

答案 0 :(得分:0)

尝试使用连接池和管道

  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;
  }