1、配置文件中添加
#redis集群配置
spring:
redis:
database: 0
password:
cluster:
#加入所有redis节点
nodes: 192.168.1.138:6381,192.168.1.138:6382,192.168.1.138:6383,192.168.1.138:6384,192.168.1.138:6385,192.168.1.138:6386
lettuce:
pool:
max-active: 16
max-idle: 32
max-wait: -1
min-idle: 8
redis的集群读写操作,数据都会被切成3份,放在不同的master里面
redis的主从,里面的数据一定是一样的
2、启动应用发现报错
Exception in thread "Thread-10" 2021-07-26 13:20:56.860 [] INFO [Thread-2] com.example.service.TaskHotProductService :刷新缓存完成
org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: CROSSSLOT Keys in request don't hash to the same slot
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:54)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:52)
at org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:41)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
at org.springframework.data.redis.connection.lettuce.LettuceConnection.convertLettuceAccessException(LettuceConnection.java:268)
at org.springframework.data.redis.connection.lettuce.LettuceZSetCommands.convertLettuceAccessException(LettuceZSetCommands.java:903)
at org.springframework.data.redis.connection.lettuce.LettuceZSetCommands.zUnionStore(LettuceZSetCommands.java:624)
at org.springframework.data.redis.connection.DefaultedRedisConnection.zUnionStore(DefaultedRedisConnection.java:868)
at org.springframework.data.redis.core.DefaultZSetOperations.lambda$unionAndStore$26(DefaultZSetOperations.java:424)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
at org.springframework.data.redis.core.DefaultZSetOperations.unionAndStore(DefaultZSetOperations.java:424)
at com.example.service.TaskWeiBoDayHourService.refreshDay(TaskWeiBoDayHourService.java:95)
at com.example.service.TaskWeiBoDayHourService.refreshData(TaskWeiBoDayHourService.java:71)
at com.example.service.TaskWeiBoDayHourService.lambda$1(TaskWeiBoDayHourService.java:36)
at java.lang.Thread.run(Thread.java:748)
原因是在执行:
//把当前的时间key,并且把后推23个小时,共计近24个小时,求出并集存入Constants.DAY_KEY中
redisTemplate.opsForZSet().unionAndStore(Constants.HOUR_KEY + hour , otherKeys, Constants.DAY_KEY);
合并各个集合的key时,涉及多key操作,而要执行多key操作,在redis集群里被限制在同一个哈希槽中才能执行
3、解决办法
用HashTag标记,HashTag即是用{}包裹key的一个子串,只有在{}的内容才计算哈希值,这样可以保证落在同一个哈希槽中
public static final String HOUR_KEY = "{rank}:hour:";
public static final String DAY_KEY = "{rank}:day";
public static final String WEEK_KEY = "{rank}:week";
public static final String MONTH_KEY = "{rank}:month";
参考资料:
https://blog.csdn.net/sinat_29843547/article/details/76508055
https://blog.csdn.net/one312/article/details/105269380
注:最新代码上传至https://github.com/csj50/myboot