【问题标题】:Connection pooling issues with Spring RedisTemplate?Spring RedisTemplate 的连接池问题?
【发布时间】:2017-11-07 09:49:40
【问题描述】:

我有一个 Spring Boot 应用程序,它与两个不同的 Redis 集群(在 Amazon Elasticache 上)通信。我正在使用 spring-data-redis 1.6.4。这是我用于不同 Redis 配置的代码:

@Configuration
public class RedisConfig {
  @Bean
  @Primary
  public JedisConnectionFactory clusterAJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setHostName(clusterAUrl);
    jedisConnectionFactory.setPort(clusterAPort);
    jedisConnectionFactory.setUsePool(true);
    return jedisConnectionFactory;
  }

  @Bean
  public JedisConnectionFactory clusterBJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setHostName(clusterBUrl);
    jedisConnectionFactory.setPort(clusterBPort);
    jedisConnectionFactory.setUsePool(true);
    return jedisConnectionFactory;
  }

  @Bean(name="clusterARedisTemplate")
  public RedisTemplate<String, Object> clusterARedisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();

    redisTemplate.setConnectionFactory(clusterAJedisConnectionFactory());
    redisTemplate.setKeySerializer( new StringRedisSerializer() );
    redisTemplate.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
    redisTemplate.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );

    return redisTemplate;
  }

  @Bean(name="clusterBRedisTemplate")
  public RedisTemplate<String, Object> clusterBRedisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();

    redisTemplate.setConnectionFactory(clusterBJedisConnectionFactory());
    redisTemplate.setKeySerializer( new StringRedisSerializer() );
    redisTemplate.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
    redisTemplate.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );

    return redisTemplate;
  }
}

然后,在我的代码中,我有类似的东西可以使用它:

@Service
RedisService {

  private final RedisConfig redisConfig;
    private final ObjectMapper mapper;

  @Autowired
    public RedisCache(RedisConfig redisConfig, ObjectMapper mapper) {
    this.redisConfig = redisConfig;
        this.mapper = mapper;
  }

  @Async
    public void saveValueInClusterA(String cacheKey, MyObject myObject) {
            try {
        String cacheValue = mapper.writeValueAsString(myObject);
                redisConfig.clusterARedisTemplate().opsForValue().set(cacheKey, cacheValue, 1, TimeUnit.HOURS);
            } catch ( Exception e ) {
                LOGGER.error(...);
            }
    }

    public MyObject getValueFromClusterA(String cacheKey) {
        MyObject myObject = null;
        try {
            String cachedEntry = redisConfig.clusterARedisTemplate().opsForValue().get(cacheKey).toString();
            myObject = mapper.readValue(cachedEntry, MyObject.class);
        } catch ( Exception e ) {
            LOGGER.error (...);
        }
        return myObject;
    }

  @Async
    public void saveValueInClusterB(String cacheKey, MyObject myObject) {
            try {
        String cacheValue = mapper.writeValueAsString(myObject);
                redisConfig.clusterBRedisTemplate().opsForValue().set(cacheKey, cacheValue, 1, TimeUnit.HOURS);
            } catch ( Exception e ) {
                LOGGER.error(...);
            }
    }

    public MyObject getValueFromClusterB(String cacheKey) {
        MyObject myObject = null;
        try {
            String cachedEntry = redisConfig.clusterBRedisTemplate().opsForValue().get(cacheKey).toString();
            myObject = mapper.readValue(cachedEntry, MyObject.class);
        } catch ( Exception e ) {
            LOGGER.error (...);
        }
        return myObject;
    }

}

这在正常负载下工作正常。但是,当我进行负载测试并进行线程转储时,我看到大多数线程都在等待这样的事情:

"XNIO-2 task-973" #1547 prio=5 os_prio=0 tid=0x00007f472c41d800 nid=0x2d4e waiting on condition [0x00007f4680851000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000004ab53fb58> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
    at redis.clients.util.Pool.getResource(Pool.java:48)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:155)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58)
    at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)
    at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:178)
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
    at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:86)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:182)
    at com.mypkg.services.RedisService.saveValueInClusterA(RedisService.java:97)
    at com.mypkg.services.RedisService$$FastClassBySpringCGLIB$$aa4c9d31.invoke( )
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:651)
    at com.mypkg.services.RedisService$$EnhancerBySpringCGLIB$$a879b180.saveValueInClusterA( )
    at com.mypkg.services.impl.MyImpl.method2(MyImpl.java:745)
    at com.mypkg.services.impl.MyImpl.method1(MyImpl.java:419)
    at sun.reflect.GeneratedMethodAccessor487.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
          …..
          …..
          …..
          …..
          …..
          …..
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
   Locked ownable synchronizers:
    - <0x00000004b41253a0> (a java.util.concurrent.ThreadPoolExecutor$Worker)

我将最大池大小设置为 128,尽管 AWS 控制台说在任何时候最多只有 35 个连接到 redis 集群。这里发生了什么?我的redis配置错了吗?还是每次使用后都需要释放连接?我认为 Redis 模板在内部处理所有这些。我连接到多个 redis 集群的事实是否会导致一些问题?

谢谢。

【问题讨论】:

  • 嘿,醉拳,我偶然发现了类似的延迟问题,我怀疑 redis 是问题所在,我有点迷失了找到根本原因。您在这种情况下使用线程转储的方法可以帮助我找到根本原因!谢谢。

标签: spring-boot spring-data-redis


【解决方案1】:

底层的连接池是一个阻塞池,如果池用完就会阻塞。如果您有足够的并发请求并且您的池大小小于并发请求的数量,则很容易发生这种情况。

增加池大小以解决问题。

附带说明:您可能想要升级您的 Spring Data Redis 版本,因为 1.6.4 已经过时了一段时间。此外,切换到 Lettuce 驱动程序不需要池化。您的代码显示了不包括阻塞/事务性 Redis 命令的操作,因此总共有两个连接应该没问题(一个连接到您的第一个 Elasticache,第二个连接到您的第二个 Elasticache)。

【讨论】:

  • 我尝试将最大池大小设置为 128。我仍然收到错误消息,即使 AWS 控制台显示我在任何时候最多只有 35 个连接。
  • 知道如何创建一个 Lettuce RedisConnection 以将键作为字符串,将值作为字节 []?我在stackoverflow.com/questions/47292145/… 上问过这个问题
猜你喜欢
  • 2016-09-14
  • 1970-01-01
  • 2011-04-01
  • 2012-09-25
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多