使用 Lettuce 配置在 springboot 中启用 Redis 集群模式时发生 CROSSSLOT 密钥错误

问题描述 投票:0回答:2

我们刚刚从redis单机迁移到集群,遇到了以下异常:RedisSystemException:执行时出错;嵌套异常是 io.lettuce.core.RedisCommandExecutionException:请求中的 CROSSSLOT 键不会散列到同一插槽。 进一步深入研究后,我发现当请求中的键在 redisTemplate.opsForZSet().intersectAndStore / redisTemplate.opsForZSet().differenceAndStore 和类似的联合交集操作期间未散列到同一插槽时,就会出现此问题。

我遇到了这个称为主题标签的概念,但我没有找到通过我的代码解决它的方法

     @LogMethodCall
  public String getValueByKey(String key) {
    if (BooleanUtils.isTrue(keyExists(key))) {
      return redisTemplate.opsForValue().get(key+"{someconstantstring}");
    }
    return null;
  }

  @LogMethodCall
  public void save(String key, String value) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"{someconstantstring}", value);
    }
  }

  @LogMethodCall
  public void saveWithExpiryInMinutes(String key, String value, long expiryTime) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"someconstantstring", value, expiryTime, TimeUnit.MINUTES);
    }
  }

  @LogMethodCall
  public void saveWithExpiryInSeconds(String key, String value, long expiryTime) {
    if (StringUtils.isNotEmpty(key)) {
      redisTemplate.opsForValue().set(key+"{someconstantstring}", value, expiryTime, TimeUnit.SECONDS);
    }
  }

我对这个概念的理解是否错误?任何带有可能有效的代码片段示例的输入可能会帮助我解决这个问题?

java spring-boot caching spring-data-redis redis-cluster
2个回答
0
投票

我试图为一个 redis key:value 传递不同的键。问题是每次都传递相同的密钥,而不是冒险更改它。例如

redisTemplate.opsForValue().set("{someconstantstring}", value;
    

此博客对此提供了很好的见解:https://hackernoon.com/resolving-the-crossslot-keys-error-with-redis-cluster-mode-enabled


0
投票

我有类似的问题,但不知道如何编辑 redis 函数,因为我使用的是 Spring Security:

Spring Boot / Spring Security 应用程序,出现 AWS Redis Elasticache 生产错误(失败:对等方重置连接和 CROSSSLOT 错误)

© www.soinside.com 2019 - 2024. All rights reserved.