springboot操作redis出现的问题
对代码进行加redis分布式锁
public String addRedisCache(String catalogJSON,ValueOperations<String, String> operations){ if (StringUtils.isEmpty(catalogJSON)){ String uuid = UUID.randomUUID().toString(); Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid,300,TimeUnit.SECONDS); if (lock) { // redisTemplate.expire("lock",30, TimeUnit.SECONDS);//设置过期时间 try { Map<String, List<Catelog2Vo>> catalogJsonFromDb = getCatalogJsonFromDb(); catalogJSON = JSON.toJSONString(catalogJsonFromDb); if (!StringUtils.isEmpty(catalogJSON)) { operations.set("catalogJSON", catalogJSON, 1, TimeUnit.DAYS); } }finally { String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end"; redisTemplate.execute(new DefaultRedisScript<Integer>(script,Integer.class),Arrays.asList("lock"),uuid); } //解锁 // String lock1 = operations.get("lock"); // if (lock1.equals(uuid)){ // redisTemplate.delete("lock"); // } //使用lua脚本 }else{ addRedisCache(catalogJSON,operations); } } return catalogJSON; }
使用lua脚本进行解锁出现异常
java.lang.IllegalStateException: null at io.lettuce.core.output.CommandOutput.set(CommandOutput.java:85) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.RedisStateMachine.safeSet(RedisStateMachine.java:358) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.RedisStateMachine.decode(RedisStateMachine.java:139) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:716) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.CommandHandler.decode0(CommandHandler.java:680) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:675) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.CommandHandler.decode(CommandHandler.java:596) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.lettuce.core.protocol.CommandHandler.channelRead(CommandHandler.java:565) ~[lettuce-core-5.2.1.RELEASE.jar:5.2.1.RELEASE] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:355) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:377) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:363) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.45.Final.jar:4.1.45.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.45.Final.jar:4.1.45.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.45.Final.jar:4.1.45.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.45.Final.jar:4.1.45.Final] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
解决方法:
1、new DefaultRedisScript<Integer>(script,Integer.class)修改成 new DefaultRedisScript<>(script,Long.class)
2、修改配置:使用jedis,不使用lettuce
本文来自博客园,作者:金玉良猿,转载请注明原文链接:https://www.cnblogs.com/LLFA/p/15886286.html