spring - 如何在 Spring 中修复“无法将此命令分派到 Redis 集群,因为键具有不同的插槽”
问题描述
我需要在 Spring 中使用 Redis Cluster。但是当我在键列表上使用 mget 或 del 时出现以下错误:“无法将此命令分派到 Redis 集群,因为键具有不同的插槽”。使用 JedisCluster 显示我的部分组件代码。
当我使用单键操作但不使用多个键时,它可以工作。
/* Component Code */
public class RedisServiceManager {
@Value("${redis.hosts}")
String hosts;
@Autowired
JedisPoolConfig jedisPoolConfig;
private JedisCluster jedisCluster;
@PostConstruct
private void init() {
List<String> redisHosts = Arrays.asList(hosts.split(","));
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
redisHosts.forEach(redisHost -> {
jedisClusterNode.add(new HostAndPort(redisHost, 6379));
});
jedisCluster = new JedisCluster(jedisClusterNode, jedisPoolConfig);
}
// This works
public String getValueForKey(String key) {
try {
return jedisCluster.get(key);
} catch (Exception e) {
return null;
}
}
// This works
public void delKey(String cacheKey) {
try {
jedisCluster.del(cacheKey);
} catch (Exception e) {
}
}
// This doesn't work
public List<String> getValuesForAllKeys(String... keys) {
try {
return jedisCluster.mget(keys);
} catch (Exception e) {
return new ArrayList<>();
}
}
// This doesn't work
public void delAllKeys(String... keys) {
try {
jedisCluster.del(keys);
} catch (Exception e) {
}
}
}
有人可以帮忙吗?
解决方案
推荐阅读
- angular - Asp.net Core 对 Angular 材料表的响应
- php - WP_Query($args) 在 Wordpress 中不起作用
- powershell - 使用 Powershell 提取 Active Directory 中的用户列表(百万)
- php - 如果从最高顺序选择了一个值,PHP foreach 循环仅显示选定的选项
- php - 如何从数据库中获取作为模型codeigniter中的数组的数据
- javascript - 有没有办法通过使用 redux 和 react-router 更改 url 来强制更新组件?
- php - 传递给模态时显示php隐藏行
- c# - Azure blob DownloadToStream 指定本地文件名
- php - 将传感器数据从 nodemcu 发送到 PHP MySQL
- html - 如何在不越界的情况下将鼠标悬停或下拉以在同一列中显示我的列表?