java - 将 Lettuce 与 RedisTemplate 一起使用会抛出异常

标签 java redis spring-data-redis lettuce

我在运行性能测试并将它与 Jedis 进行比较时,试图找出为什么 Lettuce 给我带来这么多问题。

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

使用 JedisConnectionFactory 时,测试总是无一异常(exception)地通过,但是当我切换到 LettuceConnnectionFactory 时,我无法完成任何测试,因为 channel 初始化总是超时。我将超时时间增加到 30 秒,并调整了关闭计时器,就像在我收到线程中断异常之前一样。然而,即使有 30 秒,它仍然会超时。我尝试过使用共享 native 连接,但没有使用许多不同的超时值,它们都会导致相同的问题。

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

我在这里错过了什么?从我读到的所有内容来看,每个人似乎都更喜欢 Lettuce,但从我的测试来看我不明白为什么。

最佳答案

尝试使用连接池和管道

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

关于java - 将 Lettuce 与 RedisTemplate 一起使用会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50861913/

相关文章:

java - 从 redis SET 中获取成员是否会将所有内容都获取到内存中?

redis - spring data redis - 监听过期事件

java - 动态加载遵循接口(interface)的类

Django、socket.io、node.js - 管理私有(private)消息和群组对话

spring-boot - Spring Boot执行器: Why is the Health status of Redis cluster not correct?

node.js - 套接字 io 连接但不发射

grails - grails-redis 插件的配置

java - 如何让 Reactor Core 的 Flux 缓存工作

java - 使类线程安全

java - Spring Boot 测试 MalformedURLException : unknown protocol: classpath