Redisson3.14.1及之后连接阿里云redis代理模式,使用分布式锁:ERR unknown command ‘WAIT‘
文章目录
一、问题背景
1、问题原因
阿里云Redis分直连和代理模式,其中代理模式是不支持WAIT命令
的。
目前尝试使用redisson实现分布式锁的时候出现无法执行WAIT命令(实际分布式锁命令是执行成功的,只是3.14.1及之后的redisson版本会报未知WAIT命令的错误)
Redisson 很早就会附加 WAIT 命令,只是从 3.14.1 开始才关注 WAIT 命令的执行结果。
Exception in thread "main" org.redisson.client.RedisException: ERR unknown command 'WAIT'. channel: [id: 0xf842ee1d, L:/192.168.1.20:52123 - R:/192.168.1.10:6379] command: (WAIT), params: [1, 1000]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:343)at org.redisson.client.handler.CommandDecoder.decodeCommandBatch(CommandDecoder.java:247)at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:189)at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:117)at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:102)at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:510)at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:279)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995)at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)at java.lang.Thread.run(Thread.java:750)
阿里云官方强调【暂不支持通过集群架构的Proxy节点(代理模式)执行WAIT命令,如有需要,您可以通过集群架构的直连地址
执行WAIT命令】),从而导致报错。源码层面的原因在于当进行加锁或者解锁操作时,redisson会判断当前集群的redis节点数是否大于0来给命令数组对象增加WAIT命令,这个是所有版本的redisson都有。
2、阿里云对Redisson的支持
阿里云对Redisson的支持态度很模糊,表示不会特意去适配Redisson。
二、解决方案
1、继续使用Redisson3.14.0版本
不去关注 WAIT 命令的执行结果,相当于没有 WAIT 命令。
Redisson3.14.0版本虽然也使用了WAIT命令,但是没有关注其结果,所以不会报错。
2、阿里云redis改为直连模式
阿里云redis跳过代理,改为直连模式。
相当于直接连接redis。
3、升级Redisson版本到 3.47.0+
看这个Issue:https://github.com/redisson/redisson/issues/6716
实测确实可以解决。
Redisson有几个中间版本也是可以解决这个问题的,但是最新版本是没问题的,建议升级最新版本