redisson兼容有密码和无密码两种redis场景
redisson配置兼容有/无密码redis
·
背景:
springboot 2.6.2
redisson-spring-boot-starter 3.16.4
由于本地的redis是没有密码的,线上的redis是有密码的,在该版本下在配置文件中配置了spring.redis.password会导致本地报错,即使配置文件中对该配置值为空:
Caused by: org.redisson.client.RedisException: ERR Client sent AUTH, but no password is set. channel: [id: 0xffdc4bd7, L:/127.0.0.1:56453 - R:localhost/127.0.0.1:6379] command: (AUTH), params: (password masked)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:370)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:198)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:137)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:113)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:507)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
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)
... 4 common frames omitted
解决办法:
可以引入一个config自定义器,当配置文件中该值为空时将密码设置为null,这样就会走无密码认证
@Bean
public RedissonAutoConfigurationCustomizer redissonAutoConfigurationCustomizer() {
return configuration -> {
if (StringUtils.isEmpty(configuration.useSingleServer().getPassword())) {
configuration.useSingleServer().setPassword(null);
}
};
}
更多推荐
已为社区贡献1条内容
所有评论(0)