简体   繁体   English

将莴苣与RedisTemplate一起使用会引发异常

[英]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. 我正在使用spring-data-redis 1.8.11.RELEASE,并为接口创建自定义代理bean,以通过RedisTemplate访问redis。 Redis is running in AWS and I use the cluster config endpoint provided by AWS as the node with 3 master and 3 slaves. Redis在AWS中运行,我使用AWS提供的集群配置端点作为具有3个主节点和3个从节点的节点。 Nothing special happens during the performance test. 性能测试期间没有什么特别的事情发生。 I simply call a service that reads a value from redis using RedisTemplate. 我只是调用一个使用RedisTemplate从Redis读取值的服务。

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. 使用JedisConnectionFactory时,测试始终无例外地通过,但是当我切换到LettuceConnnectionFactory时,由于通道初始化总是超时,因此我无法完成任何测试。 I increased the timeout to 30s and adjusted the shutdown timer as before I was getting thread interruption exceptions. 与收到线程中断异常之前一样,我将超时时间增加到30s并调整了关闭计时器。 However, even with 30s it still times out. 但是,即使30秒,它仍然超时。 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的代码:

@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的线程数,但仍然继续收到超时错误:

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

The code to access Redis is simply going through RedisTemplate: 访问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);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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